class SCrypt::Password

Attributes

cost[R]
digest[R]
salt[R]

Public Class Methods

create(plaintext_password, options = {}) click to toggle source
# 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
new(encrypted_password) click to toggle source
# 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

Private Class Methods

secure_compare(x, y) click to toggle source
# File lib/scrypt-ruby.rb, line 48
def self.secure_compare(x, y)
 x.bytesize == y.bytesize && x.bytes.zip(y.bytes).inject(0) { |res, b| res |= b.inject(:^) } == 0
end

Public Instance Methods

==(plaintext_password) click to toggle source
# 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
Also aliased as: is_password?
is_password?(plaintext_password)
Alias for: ==

Private Instance Methods

split_hash(h) click to toggle source
# File lib/scrypt-ruby.rb, line 52
def split_hash(h)
  n, v, r, salt, hash = h.to_s.split('$')
 [[n, v, r].join('$') + "$", salt, hash]
end
valid_hash?(h) click to toggle source
# File lib/scrypt-ruby.rb, line 57
def valid_hash?(h)
  !!h.match(/^[0-9a-z]+\$[0-9a-z]+\$[0-9a-z]+\$[A-Za-z0-9]{16,64}\$[A-Za-z0-9]{32,1024}$/)
end