Fixture: <% SALT = "NaCl" unless defined?(SALT) %>

I came across the following code in Agile Dev with Rails 4th Edition,
in fixture used to test login authentication.

<% SALT = “NaCl” unless defined?(SALT) %>

Is it saying assign “NaCl” string to local variable SALT unless SALT has
been assigned another value?

If yes, that makes me confused because where can SALT possibly be
defined.

Also, after this line, the fixture was

one:
name: dave
hashed_password: <%= User.encrypt_password(‘secret’, SALT) %>
salt: <%= SALT %>

two:
name: MyString
hashed_password: MyString
salt: MyString

So why not just go salt: NaCl in one, the same way as in two?

On a side note, is the reason for using NaCl as value a chemistry joke
since sodium chloride is a common salt?

I think the reason for this is SALT is meant to be a Constant.

So why not just go salt: NaCl in one, the same way as in two?

  • If you do it this way then when you want to change your salt then you
    need to change it for every code that is using SALT.

If yes, that makes me confused because where can SALT possibly be
defined.

  • SALT may have been defined in your environment.rb or
    environment/xxxxxx.rb
    file on the book you are reading.

Thanks!

You might want to look into Bcrypt. It’s a stronger solution to
password encryption and you can include it in your testing as well doing
something similar to:

<% require ‘bcrypt’ %>

testuser:
name: Test User
email: [email protected]
password_digest: <%= BCrypt::Password.create(“secret”) %>
password_reset_code: mysecretkey

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs