Class: Veil::Hasher::BCrypt
Instance Attribute Summary collapse
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
-
#encrypt(group, name, version) ⇒ String
Hash the credential group, name and version with the stored secret and salt.
-
#initialize(opts = {}) ⇒ BCrypt
constructor
Create a new BCrypt.
-
#to_hash ⇒ Hash<Symbol,String>
(also: #to_h)
Return the instance as a Hash.
Constructor Details
#initialize(opts = {}) ⇒ BCrypt
Create a new BCrypt
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/veil/hasher/bcrypt.rb', line 14 def initialize(opts = {}) if opts[:secret] && opts[:salt] if ::BCrypt::Engine.valid_secret?(opts[:secret]) && ::BCrypt::Engine.valid_salt?(opts[:salt]) @secret = opts.delete(:secret) @salt = opts.delete(:salt) elsif ::BCrypt::Engine.valid_secret?(opts[:secret]) raise Veil::InvalidSalt.new("#{opts[:salt]} is not valid salt") else raise Veil::InvalidSecret.new("#{opts[:secret]} is not valid secret") end else @secret = SecureRandom.hex(512) @salt = ::BCrypt::Engine.generate_salt(opts[:cost] || 10) end end |
Instance Attribute Details
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
8 9 10 |
# File 'lib/veil/hasher/bcrypt.rb', line 8 def salt @salt end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
8 9 10 |
# File 'lib/veil/hasher/bcrypt.rb', line 8 def secret @secret end |
Instance Method Details
#encrypt(group, name, version) ⇒ String
Hash the credential group, name and version with the stored secret and salt
42 43 44 |
# File 'lib/veil/hasher/bcrypt.rb', line 42 def encrypt(group, name, version) hex_digest(::BCrypt::Engine.hash_secret(hex_digest([secret, group, name, version].join), salt)) end |
#to_hash ⇒ Hash<Symbol,String> Also known as: to_h
Return the instance as a Hash
49 50 51 52 53 54 55 |
# File 'lib/veil/hasher/bcrypt.rb', line 49 def to_hash { type: self.class.name, secret: secret, salt: salt, } end |