Class: EasyCrypto::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/easycrypto/key.rb

Constant Summary collapse

ITERATION_COUNT =
10_000
KEY_LENGTH =
32
HASH_ALGO =
'sha256'
DEFAULT_SALT_LENGTH =
12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, salt) ⇒ Key

Returns a new instance of Key.



14
15
16
17
# File 'lib/easycrypto/key.rb', line 14

def initialize(key, salt)
  @key = key
  @salt = salt
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/easycrypto/key.rb', line 12

def key
  @key
end

#saltObject (readonly)

Returns the value of attribute salt.



12
13
14
# File 'lib/easycrypto/key.rb', line 12

def salt
  @salt
end

Class Method Details

.generate(password, salt_length = DEFAULT_SALT_LENGTH) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easycrypto/key.rb', line 19

def self.generate(password, salt_length = DEFAULT_SALT_LENGTH)
  salt = OpenSSL::Random.random_bytes(salt_length)
  key = OpenSSL::KDF.pbkdf2_hmac(
    password,
    salt: salt,
    iterations: Key::ITERATION_COUNT,
    length: Key::KEY_LENGTH,
    hash: Key::HASH_ALGO
  )

  new(key, salt)
end