class Trustworthy::MasterKey

Attributes

intercept[R]
slope[R]

Public Class Methods

create() click to toggle source
# File lib/trustworthy/master_key.rb, line 5
def self.create
  slope = Trustworthy::Random.number
  intercept = Trustworthy::Random.number
  new(slope, intercept)
end
create_from_keys(key1, key2) click to toggle source
# File lib/trustworthy/master_key.rb, line 11
def self.create_from_keys(key1, key2)
  slope = (key2.y - key1.y) / (key2.x - key1.x)
  intercept = key1.y - (slope * key1.x)
  new(slope, intercept)
end
new(slope, intercept) click to toggle source
# File lib/trustworthy/master_key.rb, line 17
def initialize(slope, intercept)
  @slope = slope
  @intercept = intercept
end

Public Instance Methods

==(other) click to toggle source
# File lib/trustworthy/master_key.rb, line 22
def ==(other)
  @slope == other.slope && @intercept == other.intercept
end
_cipher() click to toggle source
# File lib/trustworthy/master_key.rb, line 47
def _cipher
  secret = @intercept.to_s('F')
  hkdf = HKDF.new(secret)
  key = hkdf.next_bytes(Trustworthy::Cipher.key_len)
  Trustworthy::Cipher.new(key)
end
create_key() click to toggle source
# File lib/trustworthy/master_key.rb, line 26
def create_key
  Trustworthy::Key.create(@slope, @intercept)
end
decrypt(ciphertext) click to toggle source
# File lib/trustworthy/master_key.rb, line 39
def decrypt(ciphertext)
  nonce, ciphertext = ciphertext.split('--').map do |field|
    Base64.decode64(field)
  end

  _cipher.decrypt(nonce, '', ciphertext)
end
encrypt(plaintext) click to toggle source
# File lib/trustworthy/master_key.rb, line 30
def encrypt(plaintext)
  nonce = Trustworthy::Cipher.generate_nonce
  ciphertext = _cipher.encrypt(nonce, '', plaintext)

  [nonce, ciphertext].map do |field|
    Base64.strict_encode64(field)
  end.join('--')
end