Class: SCrypt::Password

Inherits:
String
  • Object
show all
Defined in:
lib/scrypt-ruby.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encrypted_password) ⇒ Password

Returns a new instance of Password.



26
27
28
29
30
# File 'lib/scrypt-ruby.rb', line 26

def initialize(encrypted_password)
  encrypted_password = encrypted_password.to_s
  raise Errors::InvalidHash, "invalid hash" unless valid_hash? encrypted_password
  @cost, @salt, @digest = split_hash(replace(encrypted_password))
end

Instance Attribute Details

#costObject (readonly)

Returns the value of attribute cost.



24
25
26
# File 'lib/scrypt-ruby.rb', line 24

def cost
  @cost
end

#digestObject (readonly)

Returns the value of attribute digest.



24
25
26
# File 'lib/scrypt-ruby.rb', line 24

def digest
  @digest
end

#saltObject (readonly)

Returns the value of attribute salt.



24
25
26
# File 'lib/scrypt-ruby.rb', line 24

def salt
  @salt
end

Class Method Details

.create(plaintext_password, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/scrypt-ruby.rb', line 32

def self.create(plaintext_password, options = {})
  options = Engine::DEFAULTS.merge(options)
  key_len = [[options.delete(:key_len), 16].max, 512].min
  options[:salt_size] = [[options[:salt_size], 8].max, 32].min
  salt = Engine.generate_salt(options)
  hash = Engine.hash_secret(plaintext_password, salt, key_len)
  new(hash)
end

Instance Method Details

#==(plaintext_password) ⇒ Object Also known as: is_password?



41
42
43
# File 'lib/scrypt-ruby.rb', line 41

def ==(plaintext_password)
  self.class.secure_compare(self, Engine.hash_secret(plaintext_password, @cost + @salt, self.digest.length / 2))
end