daemonza
daemonza

Reputation: 527

Comparing bcrypt hashes

for some reason the, authentication fails. However if i print the hashes to stdout, it's identical. Anyone have any ideas what's wrong?

if File.exists?(passwdFile)  
    File.open("#{passwdFile}",'r').each do |account|
      @account = account.split(':')
      usernameCheck = @account[0] # Stored username
      passwordCheck = @account[1] # Stored hashed password
      if username == usernameCheck
        # Create hash for given password to compare againts our stored one.
        password_hash = BCrypt::Engine.hash_secret(password, password_salt)
        puts "#{password_hash}"
        puts "#{passwordCheck}"
        if passwordCheck == password_hash
          @log.info 'Successfull authentication'
          return true

The password file looks like this :

bob:$2a$10$1xNjCZxjv0R06qUm1lQEEeZ1uKuZ9ELQIkT2h5/TxCI9eHDurr.dW

Upvotes: 0

Views: 311

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

The lines returned by File#each include the newline at the end of the line, so your passwordCheck variable has a trailing newline but the bcrypt generated hash doesn't.

You can remove the newline with chomp

Upvotes: 1

Related Questions