PageRenderTime 23ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/gems/bcrypt-3.1.11/lib/bcrypt/password.rb

https://gitlab.com/gag2502/EP3OO_Teste
Ruby | 87 lines | 38 code | 8 blank | 41 comment | 3 complexity | d0cd00d878eeb548c6029c6e338dcaa1 MD5 | raw file
  1. module BCrypt
  2. # A password management class which allows you to safely store users' passwords and compare them.
  3. #
  4. # Example usage:
  5. #
  6. # include BCrypt
  7. #
  8. # # hash a user's password
  9. # @password = Password.create("my grand secret")
  10. # @password #=> "$2a$10$GtKs1Kbsig8ULHZzO1h2TetZfhO4Fmlxphp8bVKnUlZCBYYClPohG"
  11. #
  12. # # store it safely
  13. # @user.update_attribute(:password, @password)
  14. #
  15. # # read it back
  16. # @user.reload!
  17. # @db_password = Password.new(@user.password)
  18. #
  19. # # compare it after retrieval
  20. # @db_password == "my grand secret" #=> true
  21. # @db_password == "a paltry guess" #=> false
  22. #
  23. class Password < String
  24. # The hash portion of the stored password hash.
  25. attr_reader :checksum
  26. # The salt of the store password hash (including version and cost).
  27. attr_reader :salt
  28. # The version of the bcrypt() algorithm used to create the hash.
  29. attr_reader :version
  30. # The cost factor used to create the hash.
  31. attr_reader :cost
  32. class << self
  33. # Hashes a secret, returning a BCrypt::Password instance. Takes an optional <tt>:cost</tt> option, which is a
  34. # logarithmic variable which determines how computational expensive the hash is to calculate (a <tt>:cost</tt> of
  35. # 4 is twice as much work as a <tt>:cost</tt> of 3). The higher the <tt>:cost</tt> the harder it becomes for
  36. # attackers to try to guess passwords (even if a copy of your database is stolen), but the slower it is to check
  37. # users' passwords.
  38. #
  39. # Example:
  40. #
  41. # @password = BCrypt::Password.create("my secret", :cost => 13)
  42. def create(secret, options = {})
  43. cost = options[:cost] || BCrypt::Engine.cost
  44. raise ArgumentError if cost > 31
  45. Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost)))
  46. end
  47. def valid_hash?(h)
  48. h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/
  49. end
  50. end
  51. # Initializes a BCrypt::Password instance with the data from a stored hash.
  52. def initialize(raw_hash)
  53. if valid_hash?(raw_hash)
  54. self.replace(raw_hash)
  55. @version, @cost, @salt, @checksum = split_hash(self)
  56. else
  57. raise Errors::InvalidHash.new("invalid hash")
  58. end
  59. end
  60. # Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
  61. def ==(secret)
  62. super(BCrypt::Engine.hash_secret(secret, @salt))
  63. end
  64. alias_method :is_password?, :==
  65. private
  66. # Returns true if +h+ is a valid hash.
  67. def valid_hash?(h)
  68. self.class.valid_hash?(h)
  69. end
  70. # call-seq:
  71. # split_hash(raw_hash) -> version, cost, salt, hash
  72. #
  73. # Splits +h+ into version, cost, salt, and hash and returns them in that order.
  74. def split_hash(h)
  75. _, v, c, mash = h.split('$')
  76. return v.to_str, c.to_i, h[0, 29].to_str, mash[-31, 31].to_str
  77. end
  78. end
  79. end