Class: Clarion::Key
- Inherits:
-
Object
- Object
- Clarion::Key
- Defined in:
- lib/clarion/key.rb
Constant Summary collapse
- CIPHER_ALGO =
'aes-256-gcm'
Instance Attribute Summary collapse
-
#counter ⇒ Object
Returns the value of attribute counter.
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#name ⇒ Object
Returns the value of attribute name.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(handle:, name: nil, public_key: nil, counter: nil) ⇒ Key
constructor
A new instance of Key.
- #to_encrypted_json(public_key, *args) ⇒ Object
- #to_h(all = false) ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(handle:, name: nil, public_key: nil, counter: nil) ⇒ Key
Returns a new instance of Key.
29 30 31 32 33 34 |
# File 'lib/clarion/key.rb', line 29 def initialize(handle:, name: nil, public_key: nil, counter: nil) @handle = handle @name = name @public_key = public_key @counter = counter end |
Instance Attribute Details
#counter ⇒ Object
Returns the value of attribute counter.
37 38 39 |
# File 'lib/clarion/key.rb', line 37 def counter @counter end |
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
36 37 38 |
# File 'lib/clarion/key.rb', line 36 def handle @handle end |
#name ⇒ Object
Returns the value of attribute name.
37 38 39 |
# File 'lib/clarion/key.rb', line 37 def name @name end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
36 37 38 |
# File 'lib/clarion/key.rb', line 36 def public_key @public_key end |
Class Method Details
.from_encrypted_json(private_key, json) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/clarion/key.rb', line 4 def self.from_encrypted_json(private_key, json) payload = JSON.parse(json, symbolize_names: true) encrypted_data = payload.fetch(:data).unpack('m*')[0] encrypted_shared_key = payload.fetch(:key).unpack('m*')[0] shared_key_json = private_key.private_decrypt(encrypted_shared_key, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING) shared_key_info = JSON.parse(shared_key_json, symbolize_names: true) iv = shared_key_info.fetch(:iv).unpack('m*')[0] shared_key = shared_key_info.fetch(:key).unpack('m*')[0] tag = shared_key_info.fetch(:tag).unpack('m*')[0] cipher = OpenSSL::Cipher.new(CIPHER_ALGO).tap do |c| c.decrypt c.key = shared_key c.iv = iv c.auth_data = '' c.auth_tag = tag end key_json = cipher.update(encrypted_data) key_json << cipher.final key = JSON.parse(key_json, symbolize_names: true) new(**key) end |
Instance Method Details
#to_encrypted_json(public_key, *args) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/clarion/key.rb', line 55 def to_encrypted_json(public_key, *args) cipher = OpenSSL::Cipher.new(CIPHER_ALGO) shared_key = OpenSSL::Random.random_bytes(cipher.key_len) cipher.encrypt cipher.key = shared_key cipher.iv = iv = cipher.random_iv cipher.auth_data = '' json = to_json(*args) ciphertext = cipher.update(json) ciphertext << cipher.final encrypted_key = public_key.public_encrypt({ iv: [iv].pack('m*').gsub(/\r?\n/,''), tag: [cipher.auth_tag].pack('m*').gsub(/\r?\n/,''), key: [shared_key].pack('m*').gsub(/\r?\n/,''), }.to_json, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING) {data: [ciphertext].pack('m*'), key: [encrypted_key].pack('m*')}.to_json end |
#to_h(all = false) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/clarion/key.rb', line 39 def to_h(all=false) { handle: handle, }.tap do |h| h[:name] = name if name h[:counter] = counter if counter if all h[:public_key] = public_key if public_key end end end |
#to_json(*args) ⇒ Object
51 52 53 |
# File 'lib/clarion/key.rb', line 51 def to_json(*args) to_h(*args).to_json end |