Class: BasicPassword

Inherits:
Object
  • Object
show all
Defined in:
lib/basic-password.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/basic-password.rb', line 5

def hash
  @hash
end

#saltObject (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_hObject



12
13
14
# File 'lib/basic-password.rb', line 12

def to_h
  { salt: @salt, hash: @hash }
end

#to_jsonObject



16
17
18
# File 'lib/basic-password.rb', line 16

def to_json
  JSON.generate to_h
end