class Platon::Key::Decrypter
Attributes
data[R]
key[R]
password[R]
Public Class Methods
new(data, password)
click to toggle source
# File lib/platon/key/decrypter.rb, line 11 def initialize(data, password) @data = JSON.parse(data) @password = password end
perform(data, password)
click to toggle source
# File lib/platon/key/decrypter.rb, line 7 def self.perform(data, password) new(data, password).perform end
Public Instance Methods
perform()
click to toggle source
# File lib/platon/key/decrypter.rb, line 16 def perform derive_key password check_macs bin_to_hex decrypted_data end
Private Instance Methods
check_macs()
click to toggle source
# File lib/platon/key/decrypter.rb, line 39 def check_macs mac1 = keccak256(key[(key_length/2), key_length] + ciphertext) mac2 = hex_to_bin crypto_data['mac'] if mac1 != mac2 raise "Message Authentications Codes do not match!" end end
cipher()
click to toggle source
# File lib/platon/key/decrypter.rb, line 64 def cipher @cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher| cipher.decrypt cipher.key = key[0, (key_length/2)] cipher.iv = iv end end
cipher_name()
click to toggle source
# File lib/platon/key/decrypter.rb, line 60 def cipher_name "aes-128-ctr" end
ciphertext()
click to toggle source
# File lib/platon/key/decrypter.rb, line 56 def ciphertext hex_to_bin crypto_data['ciphertext'] end
crypto_data()
click to toggle source
# File lib/platon/key/decrypter.rb, line 52 def crypto_data @crypto_data ||= data['crypto'] || data['Crypto'] end
decrypted_data()
click to toggle source
# File lib/platon/key/decrypter.rb, line 48 def decrypted_data @decrypted_data ||= cipher.update(ciphertext) + cipher.final end
derive_key(password)
click to toggle source
# File lib/platon/key/decrypter.rb, line 27 def derive_key(password) case kdf when 'pbkdf2' @key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest) when 'scrypt' # OpenSSL 1.1 inclues OpenSSL::KDF.scrypt, but it is not available usually, otherwise we could do: OpenSSL::KDF.scrypt(password, salt: salt, N: n, r: r, p: p, length: key_length) @key = SCrypt::Engine.scrypt(password, salt, n, r, p, key_length) else raise "Unsupported key derivation function: #{kdf}!" end end
digest()
click to toggle source
# File lib/platon/key/decrypter.rb, line 104 def digest OpenSSL::Digest.new digest_name end
digest_name()
click to toggle source
# File lib/platon/key/decrypter.rb, line 108 def digest_name "sha256" end
iterations()
click to toggle source
# File lib/platon/key/decrypter.rb, line 80 def iterations crypto_data['kdfparams']['c'].to_i end
iv()
click to toggle source
# File lib/platon/key/decrypter.rb, line 72 def iv hex_to_bin crypto_data['cipherparams']['iv'] end
kdf()
click to toggle source
# File lib/platon/key/decrypter.rb, line 84 def kdf crypto_data['kdf'] end
key_length()
click to toggle source
# File lib/platon/key/decrypter.rb, line 88 def key_length crypto_data['kdfparams']['dklen'].to_i end
n()
click to toggle source
# File lib/platon/key/decrypter.rb, line 92 def n crypto_data['kdfparams']['n'].to_i end
p()
click to toggle source
# File lib/platon/key/decrypter.rb, line 100 def p crypto_data['kdfparams']['p'].to_i end
r()
click to toggle source
# File lib/platon/key/decrypter.rb, line 96 def r crypto_data['kdfparams']['r'].to_i end
salt()
click to toggle source
# File lib/platon/key/decrypter.rb, line 76 def salt hex_to_bin crypto_data['kdfparams']['salt'] end