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'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, salt) ⇒ Key

Returns a new instance of Key.



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#saltObject (readonly)

Returns the value of attribute salt.



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

def salt
  @salt
end

Class Method Details

.generate(password, salt_length = DEFAULT_SALT_LENGTH) ⇒ Object



18
19
20
21
22
# File 'lib/easycrypto/key.rb', line 18

def self.generate(password, salt_length = DEFAULT_SALT_LENGTH)
  salt = OpenSSL::Random.random_bytes(salt_length)

  generate_with_salt(password, salt)
end

.generate_with_salt(password, salt) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easycrypto/key.rb', line 24

def self.generate_with_salt(password, salt)
  key = OpenSSL::PKCS5.pbkdf2_hmac(
    password,
    salt,
    Key::ITERATION_COUNT,
    Key::KEY_LENGTH,
    Key::HASH_ALGO
  )

  new(key, salt)
end