Class: ActiveEncryption::EncryptionSetting::Key

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

Overview

The ActiveEncryption::EncryptionSetting::Key class wraps around ActiveSupport::KeyGenerator

Constant Summary collapse

DEFAULT_SALT =
'ActiveEncryption default key salt'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret, salt: nil, cipher: nil, iterations: nil) ⇒ Key

Returns a new instance of Key.



15
16
17
18
19
20
21
22
23
24
# File 'lib/active_encryption/encryption_setting/key.rb', line 15

def initialize(secret, salt: nil, cipher: nil, iterations: nil)
  salt ||= DEFAULT_SALT
  @salt       = salt
  @cipher     = cipher
  @iterations = iterations
  @generator  = ActiveSupport::KeyGenerator.new(
    secret,
    iterations: iterations
  )
end

Instance Attribute Details

#iterationsObject (readonly)

Returns the value of attribute iterations.



13
14
15
# File 'lib/active_encryption/encryption_setting/key.rb', line 13

def iterations
  @iterations
end

#saltObject (readonly)

Returns the value of attribute salt.



13
14
15
# File 'lib/active_encryption/encryption_setting/key.rb', line 13

def salt
  @salt
end

Instance Method Details

#cipherObject



30
31
32
# File 'lib/active_encryption/encryption_setting/key.rb', line 30

def cipher
  @cipher ||= ActiveSupport::MessageEncryptor.default_cipher
end

#lengthObject



34
35
36
# File 'lib/active_encryption/encryption_setting/key.rb', line 34

def length
  ActiveSupport::MessageEncryptor.key_len(cipher)
end

#valueObject



26
27
28
# File 'lib/active_encryption/encryption_setting/key.rb', line 26

def value
  @generator.generate_key(salt, length)
end