class CryptoRecord

Universa KeyRecord. This structure is intended to hold small encrypted entity, usually a key to other, big encrypted entity. KeyRecord maximum plaintext size is small, so everything that could be large than say 128 bytes should be encrypted using a key stored as plaintext in the KR.

This class is not directly instantiable, use one of its ancestors, like {Pbkdf2CryptoRecord}, or decode packed binary record.

Public Class Methods

from_packed(packed) click to toggle source

Unpack single KeyRecord packed into binary form.

# File lib/universa_tools/crypto_record.rb, line 10
def self.from_packed packed
  decode_array(Boss.load(packed))
end
new(code, ciphertext) click to toggle source
# File lib/universa_tools/crypto_record.rb, line 36
def initialize(code, ciphertext)
  @code, @ciphertext = code, ciphertext
  @code or raise ArgumentError, "code can't be nil"
end
pack_all(records) click to toggle source

Pack many key records into single binary packed form. To unpack it ise {unpack_all}

@param [Array(CryptoRecord)] records to pack @return [Binary] binary packed string.

# File lib/universa_tools/crypto_record.rb, line 29
def self.pack_all(records)
  # hack: we call private method, we do not want to make it public
  Boss.pack(records.map{ |x| x.send :serialized})
end
unpack_all(packed_records) click to toggle source

Unpack array of key records

# File lib/universa_tools/crypto_record.rb, line 15
def self.unpack_all(packed_records)
  Boss.load(packed_records).map { |array| decode_array(array) }
end

Private Class Methods

decode_array(packed_array) click to toggle source
# File lib/universa_tools/crypto_record.rb, line 56
def self.decode_array(packed_array)
  code, *params, encpypted_key = *packed_array
  case code
    when 1
      Pbkdf2CryptoRecord.new(params, encpypted_key)
    else
      raise ArgumentError, "unknown KR code #{code}"
  end
end

Public Instance Methods

pack() click to toggle source

Pack a self in binary form, this is different from pack_all/unpack_all. @return [Binary] stinrg with encoded key record

# File lib/universa_tools/crypto_record.rb, line 21
def pack
  Boss.pack(serialized)
end

Protected Instance Methods

encrypt_with_key(key, plaintext) click to toggle source
# File lib/universa_tools/crypto_record.rb, line 41
def encrypt_with_key(key, plaintext)
  @ciphertext = key.etaEncrypt(plaintext).freeze
end
serialized_params() click to toggle source
# File lib/universa_tools/crypto_record.rb, line 45
def serialized_params
  raise "implement serialized params"
end

Private Instance Methods

serialized() click to toggle source
# File lib/universa_tools/crypto_record.rb, line 51
def serialized
  @ciphertext or raise IllegalStateError, "empty ciphertext, encrypt something first"
  [@code, serialized_params, @ciphertext].flatten
end