Method: BCrypt::Engine.hash_secret

Defined in:
lib/bcrypt/engine.rb

.hash_secret(secret, salt, _ = nil) ⇒ Object

Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates a bcrypt() password hash. Secrets longer than 72 bytes are truncated.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bcrypt/engine.rb', line 55

def self.hash_secret(secret, salt, _ = nil)
  unless _.nil?
    warn "[DEPRECATION] Passing the third argument to " \
         "`BCrypt::Engine.hash_secret` is deprecated. " \
         "Please do not pass the third argument which " \
         "is currently not used."
  end

  if valid_secret?(secret)
    if valid_salt?(salt)
      if RUBY_PLATFORM == "java"
        Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s.to_java_bytes, salt.to_s)
      else
        secret = secret.to_s
        secret = secret.byteslice(0, MAX_SECRET_BYTESIZE) if secret && secret.bytesize > MAX_SECRET_BYTESIZE
        __bc_crypt(secret, salt)
      end
    else
      raise Errors::InvalidSalt.new("invalid salt")
    end
  else
    raise Errors::InvalidSecret.new("invalid secret")
  end
end