class Eth::Key::Decrypter

Attributes

data[R]
key[R]
password[R]

Public Class Methods

new(data, password) click to toggle source
# File lib/eth/key/decrypter.rb, line 10
def initialize(data, password)
  @data = JSON.parse(data)
  @password = password
end
perform(data, password) click to toggle source
# File lib/eth/key/decrypter.rb, line 6
def self.perform(data, password)
  new(data, password).perform
end

Public Instance Methods

perform() click to toggle source
# File lib/eth/key/decrypter.rb, line 15
def perform
  derive_key password
  check_macs
  bin_to_hex decrypted_data
end

Private Instance Methods

check_macs() click to toggle source
# File lib/eth/key/decrypter.rb, line 30
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/eth/key/decrypter.rb, line 55
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/eth/key/decrypter.rb, line 51
def cipher_name
  "aes-128-ctr"
end
ciphertext() click to toggle source
# File lib/eth/key/decrypter.rb, line 47
def ciphertext
  hex_to_bin crypto_data['ciphertext']
end
crypto_data() click to toggle source
# File lib/eth/key/decrypter.rb, line 43
def crypto_data
  @crypto_data ||= data['crypto'] || data['Crypto']
end
decrypted_data() click to toggle source
# File lib/eth/key/decrypter.rb, line 39
def decrypted_data
  @decrypted_data ||= cipher.update(ciphertext) + cipher.final
end
derive_key(password) click to toggle source
# File lib/eth/key/decrypter.rb, line 26
def derive_key(password)
  @key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest)
end
digest() click to toggle source
# File lib/eth/key/decrypter.rb, line 79
def digest
  OpenSSL::Digest.new digest_name
end
digest_name() click to toggle source
# File lib/eth/key/decrypter.rb, line 83
def digest_name
  "sha256"
end
iterations() click to toggle source
# File lib/eth/key/decrypter.rb, line 71
def iterations
  crypto_data['kdfparams']['c'].to_i
end
iv() click to toggle source
# File lib/eth/key/decrypter.rb, line 63
def iv
  hex_to_bin crypto_data['cipherparams']['iv']
end
key_length() click to toggle source
# File lib/eth/key/decrypter.rb, line 75
def key_length
  32
end
salt() click to toggle source
# File lib/eth/key/decrypter.rb, line 67
def salt
  hex_to_bin crypto_data['kdfparams']['salt']
end