class Itamae::Secrets::Decryptor

Constants

ALGORITHM

Attributes

auth_tag[R]
ciphertext[R]
iv[R]
key[RW]
key_name[R]

Public Class Methods

load_json(json, key = nil) click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 8
def self.load_json(json, key = nil)
  data = JSON.parse(json)

  raise ArgumentError, "unknown version #{data['version'].inspect}" if data['version'] != 1
  raise ArgumentError, "unknown version #{data['algorithm'].inspect}" if data['algorithm'] != ALGORITHM

  new(
    data['ciphertext'],
    data['auth_tag'],
    data['iv'],
    data['key_name'],
    key
  )
end
new(ciphertext, auth_tag, iv, key_name, key = nil) click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 23
def initialize(ciphertext, auth_tag, iv, key_name, key = nil)
  ensure_algorithm_key_compatiblity!(key) if key
  @ciphertext = ciphertext
  @auth_tag = auth_tag
  @iv = iv
  @key_name = key_name
  @key = key
end

Public Instance Methods

algorithm() click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 52
def algorithm
  ALGORITHM
end
cipher() click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 56
def cipher
  @cipher ||= OpenSSL::Cipher.new(algorithm).tap do |c|
    raise 'key is required to proceed' unless key
    c.decrypt
    c.key = key.to_s
    c.iv = iv.unpack('m*')[0]
    c.auth_data = ''
    c.auth_tag = auth_tag.unpack('m*')[0]
  end
end
key=(other) click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 35
def key=(other)
  raise "can't overwrite" if @key
  ensure_algorithm_key_compatiblity!(other)
  @key = other
end
plaintext() click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 41
def plaintext
  @plaintext ||= begin
    txt = cipher.update(ciphertext.unpack('m*')[0])
    txt << cipher.final
  end
end
version() click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 48
def version
  1
end

Private Instance Methods

ensure_algorithm_key_compatiblity!(key) click to toggle source
# File lib/itamae/secrets/decryptor.rb, line 69
def ensure_algorithm_key_compatiblity!(key)
  unless key.algorithm_compatible?(algorithm)
    raise ArgumentError, "#{key.type} is not compatible"
  end
end