Class: Veil::Hasher::PBKDF2
Instance Attribute Summary collapse
-
#hash_function ⇒ Object
readonly
Returns the value of attribute hash_function.
-
#iterations ⇒ Object
readonly
Returns the value of attribute iterations.
-
#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 data with the stored secret and salt.
-
#initialize(opts = {}) ⇒ PBKDF2
constructor
Create a new PBKDF2.
-
#to_hash ⇒ Hash
(also: #to_h)
Return the instance as a Hash.
Constructor Details
#initialize(opts = {}) ⇒ PBKDF2
Create a new PBKDF2
13 14 15 16 17 18 |
# File 'lib/veil/hasher/pbkdf2.rb', line 13 def initialize(opts = {}) @secret = opts[:secret] || SecureRandom.hex(512) @salt = opts[:salt] || SecureRandom.hex(128) @iterations = opts[:iterations] || 100_000 @hash_function = OpenSSL::Digest.const_get((opts[:hash_function] || "SHA512")).new end |
Instance Attribute Details
#hash_function ⇒ Object (readonly)
Returns the value of attribute hash_function.
7 8 9 |
# File 'lib/veil/hasher/pbkdf2.rb', line 7 def hash_function @hash_function end |
#iterations ⇒ Object (readonly)
Returns the value of attribute iterations.
7 8 9 |
# File 'lib/veil/hasher/pbkdf2.rb', line 7 def iterations @iterations end |
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
7 8 9 |
# File 'lib/veil/hasher/pbkdf2.rb', line 7 def salt @salt end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
7 8 9 |
# File 'lib/veil/hasher/pbkdf2.rb', line 7 def secret @secret end |
Instance Method Details
#encrypt(group, name, version) ⇒ String
Hash data with the stored secret and salt
29 30 31 32 33 34 35 36 37 |
# File 'lib/veil/hasher/pbkdf2.rb', line 29 def encrypt(group, name, version) hex_digest(OpenSSL::PKCS5.pbkdf2_hmac( [secret, group, name, version].join, salt, iterations, hash_function.length, hash_function )) end |
#to_hash ⇒ Hash Also known as: to_h
Return the instance as a Hash
42 43 44 45 46 47 48 49 50 |
# File 'lib/veil/hasher/pbkdf2.rb', line 42 def to_hash { type: self.class.name, secret: secret, salt: salt, iterations: iterations, hash_function: hash_function.class.name } end |