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
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
# 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 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 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
# 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 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
# File lib/universa_tools/crypto_record.rb, line 41 def encrypt_with_key(key, plaintext) @ciphertext = key.etaEncrypt(plaintext).freeze end
# File lib/universa_tools/crypto_record.rb, line 45 def serialized_params raise "implement serialized params" end
Private Instance Methods
# 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