class Platon::Key::Encrypter
Attributes
derived_key[R]
encrypted_key[R]
key[R]
options[R]
Public Class Methods
new(key, options = {})
click to toggle source
# File lib/platon/key/encrypter.rb, line 11 def initialize(key, options = {}) @key = key @options = options end
perform(key, password, options = {})
click to toggle source
# File lib/platon/key/encrypter.rb, line 7 def self.perform(key, password, options = {}) new(key, options).perform(password) end
Public Instance Methods
data()
click to toggle source
# File lib/platon/key/encrypter.rb, line 23 def data { crypto: { cipher: cipher_name, cipherparams: { iv: bin_to_hex(iv), }, ciphertext: bin_to_hex(encrypted_key), kdf: "pbkdf2", kdfparams: { c: iterations, dklen: 32, prf: prf, salt: bin_to_hex(salt), }, mac: bin_to_hex(mac), }, id: id, version: 3, }.tap do |data| data[:address] = address unless options[:skip_address] end end
id()
click to toggle source
# File lib/platon/key/encrypter.rb, line 47 def id @id ||= options[:id] || SecureRandom.uuid end
perform(password)
click to toggle source
# File lib/platon/key/encrypter.rb, line 16 def perform(password) derive_key password encrypt data.to_json end
Private Instance Methods
address()
click to toggle source
# File lib/platon/key/encrypter.rb, line 124 def address Platon::Key.new(priv: key).bech32_address(hrp: options[:hrp] ? options[:hrp]:"atp") end
cipher()
click to toggle source
# File lib/platon/key/encrypter.rb, line 56 def cipher @cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher| cipher.encrypt cipher.iv = iv cipher.key = derived_key[0, (key_length/2)] end end
cipher_name()
click to toggle source
# File lib/platon/key/encrypter.rb, line 80 def cipher_name "aes-128-ctr" end
derive_key(password)
click to toggle source
# File lib/platon/key/encrypter.rb, line 68 def derive_key(password) @derived_key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest) end
digest()
click to toggle source
# File lib/platon/key/encrypter.rb, line 64 def digest @digest ||= OpenSSL::Digest.new digest_name end
digest_name()
click to toggle source
# File lib/platon/key/encrypter.rb, line 84 def digest_name "sha256" end
encrypt()
click to toggle source
# File lib/platon/key/encrypter.rb, line 72 def encrypt @encrypted_key = cipher.update(hex_to_bin key) + cipher.final end
iterations()
click to toggle source
# File lib/platon/key/encrypter.rb, line 104 def iterations options[:iterations] || 262_144 end
iv()
click to toggle source
# File lib/platon/key/encrypter.rb, line 116 def iv @iv ||= if options[:iv] hex_to_bin options[:iv] else SecureRandom.random_bytes(iv_length) end end
iv_length()
click to toggle source
# File lib/platon/key/encrypter.rb, line 100 def iv_length 16 end
key_length()
click to toggle source
# File lib/platon/key/encrypter.rb, line 92 def key_length 32 end
mac()
click to toggle source
# File lib/platon/key/encrypter.rb, line 76 def mac keccak256(derived_key[(key_length/2), key_length] + encrypted_key) end
prf()
click to toggle source
# File lib/platon/key/encrypter.rb, line 88 def prf "hmac-#{digest_name}" end
salt()
click to toggle source
# File lib/platon/key/encrypter.rb, line 108 def salt @salt ||= if options[:salt] hex_to_bin options[:salt] else SecureRandom.random_bytes(salt_length) end end
salt_length()
click to toggle source
# File lib/platon/key/encrypter.rb, line 96 def salt_length 32 end