Class: Klay::Key::Encrypter
- Inherits:
-
Object
- Object
- Klay::Key::Encrypter
- Defined in:
- lib/klay/key/encrypter.rb
Overview
The Encrypter class to handle PBKDF2-SHA-256 encryption.
Defined Under Namespace
Classes: EncrypterError
Class Method Summary collapse
-
.perform(key, password, options = {}) ⇒ JSON
Class method Encrypter.perform to performa an key-store encryption.
Instance Method Summary collapse
-
#data ⇒ Hash
Output containing the encrypted key and other identifying data.
-
#initialize(key, options = {}) ⇒ Encrypter
constructor
Constructor of the Encrypter class for secret key encryption.
-
#perform(password) ⇒ String
Encrypt the key with a given password.
Constructor Details
#initialize(key, options = {}) ⇒ Encrypter
Constructor of the Klay::Key::Encrypter class for secret key encryption. Should not be used; use perform instead.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/klay/key/encrypter.rb', line 54 def initialize(key, = {}) key = Key.new(priv: key) if key.is_a? String @key = key @options = # the key derivation functions default to pbkdf2 if no option is specified # however, if an option is given then it must be either pbkdf2 or scrypt if kdf != "scrypt" && kdf != "pbkdf2" raise EncrypterError, "Unsupported key derivation function: #{kdf}!" end end |
Class Method Details
.perform(key, password, options = {}) ⇒ JSON
Class method perform to performa an key-store encryption.
37 38 39 |
# File 'lib/klay/key/encrypter.rb', line 37 def self.perform(key, password, = {}) new(key, ).perform(password) end |
Instance Method Details
#data ⇒ Hash
Output containing the encrypted key and other identifying data
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/klay/key/encrypter.rb', line 80 def data # default to pbkdf2 kdfparams = if kdf == "scrypt" { dklen: 32, n: iterations, p: parallelization, r: block_size, salt: Util.bin_to_hex(salt), } else { c: iterations, dklen: 32, prf: prf, salt: Util.bin_to_hex(salt), } end { crypto: { cipher: cipher_name, cipherparams: { iv: Util.bin_to_hex(iv), }, ciphertext: Util.bin_to_hex(encrypted_key), kdf: kdf, kdfparams: kdfparams, mac: Util.bin_to_hex(mac), }, id: id, version: 3, } end |
#perform(password) ⇒ String
Encrypt the key with a given password.
70 71 72 73 74 |
# File 'lib/klay/key/encrypter.rb', line 70 def perform(password) derive_key password encrypt data.to_json end |