Class: Trustworthy::MasterKey
- Inherits:
-
Object
- Object
- Trustworthy::MasterKey
- Defined in:
- lib/trustworthy/master_key.rb
Instance Attribute Summary collapse
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #_cipher ⇒ Object
- #create_key ⇒ Object
- #decrypt(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(slope, intercept) ⇒ MasterKey
constructor
A new instance of MasterKey.
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
#intercept ⇒ Object (readonly)
Returns the value of attribute intercept.
3 4 5 |
# File 'lib/trustworthy/master_key.rb', line 3 def intercept @intercept end |
#slope ⇒ Object (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
.create ⇒ Object
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 |
#_cipher ⇒ Object
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_key ⇒ Object
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 |