Class: Trustworthy::MasterKey

Inherits:
Object
  • Object
show all
Defined in:
lib/trustworthy/master_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope, intercept) ⇒ MasterKey

Returns a new instance of MasterKey.



17
18
19
20
# File 'lib/trustworthy/master_key.rb', line 17

def initialize(slope, intercept)
  @slope = slope
  @intercept = intercept
end

Instance Attribute Details

#interceptObject (readonly)

Returns the value of attribute intercept.



3
4
5
# File 'lib/trustworthy/master_key.rb', line 3

def intercept
  @intercept
end

#slopeObject (readonly)

Returns the value of attribute slope.



3
4
5
# File 'lib/trustworthy/master_key.rb', line 3

def slope
  @slope
end

Class Method Details

.createObject



5
6
7
8
9
# 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) ⇒ Object



11
12
13
14
15
# 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

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/trustworthy/master_key.rb', line 22

def ==(other)
  @slope == other.slope && @intercept == other.intercept
end

#_cipherObject



47
48
49
50
51
52
# 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_keyObject



26
27
28
# File 'lib/trustworthy/master_key.rb', line 26

def create_key
  Trustworthy::Key.create(@slope, @intercept)
end

#decrypt(ciphertext) ⇒ Object



39
40
41
42
43
44
45
# 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) ⇒ Object



30
31
32
33
34
35
36
37
# 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