Advertisement
Guest User

Untitled

a guest
May 8th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. require 'bcrypt'
  2. require 'pp'
  3. require 'base64'
  4. require 'yaml'
  5. require 'ostruct'
  6.  
  7. SECRET = 'my password'
  8.  
  9. puts '********** Base64 (not safe because it produces the same string every time)**********'
  10. enc_64_string = "bXkgcGFzc3dvcmQ=\n"
  11. enc = Base64.encode64(SECRET)
  12. puts ['Same?', enc.to_s == enc_64_string].join(' ')
  13. pp enc
  14. puts plain = Base64.decode64(enc)
  15.  
  16. puts '********** create database record **********'
  17. BCrypt::Engine.cost = 4 # default is 10
  18. bcrypt_from_password = BCrypt::Password.create(SECRET)
  19.  
  20. # salt includes information on version and cost
  21. # raw_hash = salt + checksum
  22. row = { password: SECRET,
  23. version: bcrypt_from_password.version,
  24. cost: bcrypt_from_password.cost,
  25. salt: bcrypt_from_password.salt,
  26. checksum: bcrypt_from_password.checksum,
  27. raw_hash: bcrypt_from_password.to_s }
  28.  
  29. # save results
  30. results = YAML::load_file('./results.yml') rescue Array.new
  31. results << row
  32. File.open('./results.yml', 'w') { |f| f.puts results.to_yaml }
  33.  
  34. # simulate DB record
  35. db_record = OpenStruct.new(:username => 'user',
  36. salt: results[rand(results.size - 1)][:salt],
  37. raw_hash: results[rand(results.size - 1)][:raw_hash])
  38. pp db_record
  39.  
  40. puts '********** scenario 1 (password match) **********'
  41. user_input = OpenStruct.new(:password => 'my password', :username => 'user')
  42. password = BCrypt::Password.new(db_record.raw_hash)
  43. puts ['match:', BCrypt::Password.new(password).is_password?(user_input.password)].join(' ')
  44.  
  45. puts '********** scenario 2 (password not match) **********'
  46. user_input = OpenStruct.new(:password => 'wrong password', :username => 'user')
  47. password = BCrypt::Password.new(db_record.raw_hash)
  48. puts ['match:', BCrypt::Password.new(password).is_password?(user_input.password)].join(' ')
  49.  
  50. # scenario 3 and 4 are comparing raw hash. It's not recommended because one needs to store the salt and the raw hash to the database.
  51.  
  52. puts '********** scenario 3 **********'
  53. user_input = OpenStruct.new(:password => 'my password', :username => 'user')
  54. hash_secret = BCrypt::Engine.hash_secret(user_input.password, db_record.salt)
  55. puts ['User Hash Secret:', hash_secret].join(' ')
  56. puts ['DB Hash Secret:', db_record.raw_hash].join(' ')
  57. puts ['match:', hash_secret == db_record.raw_hash].join(' ')
  58.  
  59. puts '********** scenario 4 **********'
  60. user_input = OpenStruct.new(:password => 'wrong password', :username => 'user')
  61. hash_secret = BCrypt::Engine.hash_secret(user_input.password, db_record.salt)
  62. puts ['User Hash Secret:', hash_secret].join(' ')
  63. puts ['DB Hash Secret:', db_record.raw_hash].join(' ')
  64. puts ['match:', hash_secret == db_record.raw_hash].join(' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement