Class: BasicPassword
- Inherits:
-
Object
- Object
- BasicPassword
- Defined in:
- lib/basic-password.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
Class Method Summary collapse
Instance Method Summary collapse
- #check(value) ⇒ Object
-
#initialize(value, salt = SecureRandom.base64) ⇒ BasicPassword
constructor
A new instance of BasicPassword.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(value, salt = SecureRandom.base64) ⇒ BasicPassword
Returns a new instance of BasicPassword.
7 8 9 10 |
# File 'lib/basic-password.rb', line 7 def initialize(value, salt = SecureRandom.base64) @salt = salt @hash = Digest::SHA2.new(512).base64digest(value + salt) end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
5 6 7 |
# File 'lib/basic-password.rb', line 5 def hash @hash end |
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
5 6 7 |
# File 'lib/basic-password.rb', line 5 def salt @salt end |
Class Method Details
.check(value, salt, hash) ⇒ Object
28 29 30 |
# File 'lib/basic-password.rb', line 28 def self.check (value, salt, hash) hash == BasicPassword.new(value, salt).hash end |
.digest(value, salt = SecureRandom.base64) ⇒ Object
24 25 26 |
# File 'lib/basic-password.rb', line 24 def self.digest(value, salt = SecureRandom.base64) BasicPassword.new(value, salt).to_h end |
Instance Method Details
#check(value) ⇒ Object
20 21 22 |
# File 'lib/basic-password.rb', line 20 def check(value) hash == BasicPassword.new(value, salt).hash end |
#to_h ⇒ Object
12 13 14 |
# File 'lib/basic-password.rb', line 12 def to_h { salt: @salt, hash: @hash } end |
#to_json ⇒ Object
16 17 18 |
# File 'lib/basic-password.rb', line 16 def to_json JSON.generate to_h end |