Class: Virgil::SDK::HighLevel::VirgilKey
- Inherits:
-
Object
- Object
- Virgil::SDK::HighLevel::VirgilKey
- Defined in:
- lib/virgil/sdk/high_level/virgil_key.rb
Overview
This class represents a user's high-level Private key which provides a list of methods that allows to store the key and perform cryptographic operations like Decrypt, Sign etc.
Instance Attribute Summary collapse
-
#context ⇒ VirgilContext
readonly
manages the VirgilApi dependencies during run time.
-
#private_key ⇒ Cryptography::Keys::PrivateKey
readonly
private key.
Instance Method Summary collapse
-
#decrypt(cipher_buffer) ⇒ VirgilBuffer
Decrypts the specified cipher data using Virgil key.
-
#decrypt_then_verify(cipher_buffer, *cards) ⇒ VirgilBuffer
Decrypts and verifies the data.
-
#export(password = nil) ⇒ VirgilBuffer
Exports the VirgilKey to default format, specified in Crypto API.
-
#export_public_key ⇒ VirgilBuffer
Exports the Public key value from current VirgilKey.
-
#initialize(context, private_key) ⇒ VirgilKey
constructor
Initializes a new instance of the VirgilKey class.
-
#save(key_name, key_password = nil) ⇒ VirgilKey
Saves a current VirgilKey in secure storage.
-
#sign(buffer) ⇒ VirgilBuffer
Generates a digital signature for specified data using current Virgil key.
-
#sign_then_encrypt(buffer, recipients) ⇒ VirgilBuffer
Encrypts and signs the data.
Constructor Details
#initialize(context, private_key) ⇒ VirgilKey
Initializes a new instance of the Virgil::SDK::HighLevel::VirgilKey class.
52 53 54 55 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 52 def initialize(context, private_key) @context = context @private_key = private_key end |
Instance Attribute Details
#context ⇒ VirgilContext (readonly)
manages the VirgilApi dependencies during run time.
45 46 47 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 45 def context @context end |
#private_key ⇒ Cryptography::Keys::PrivateKey (readonly)
private key
49 50 51 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 49 def private_key @private_key end |
Instance Method Details
#decrypt(cipher_buffer) ⇒ VirgilBuffer
Decrypts the specified cipher data using Virgil key.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 73 def decrypt(cipher_buffer) buffer_to_decrypt = case cipher_buffer.class.name.split("::").last when 'VirgilBuffer' cipher_buffer when 'String' VirgilBuffer.from_base64(cipher_buffer) when 'Array' VirgilBuffer.from_bytes(cipher_buffer) else raise ArgumentError.new("Buffer has unsupported type") end bytes = context.crypto.decrypt(buffer_to_decrypt.bytes, private_key) VirgilBuffer.new(bytes) end |
#decrypt_then_verify(cipher_buffer, *cards) ⇒ VirgilBuffer
Decrypts and verifies the data.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 185 def decrypt_then_verify(cipher_buffer, *cards) raise ArgumentError.new("card is not valid") unless cards.all? { |el| el.is_a? VirgilCard } buffer_to_decrypt = case cipher_buffer.class.name.split("::").last when 'VirgilBuffer' cipher_buffer when 'String' VirgilBuffer.from_base64(cipher_buffer) when 'Array' VirgilBuffer.from_bytes(cipher_buffer) else raise ArgumentError.new("Buffer has unsupported type") end public_keys = cards.map(&:public_key) bytes = context.crypto.decrypt_then_verify(buffer_to_decrypt.bytes, private_key, *public_keys) VirgilBuffer.new(bytes) end |
#export(password = nil) ⇒ VirgilBuffer
Exports the VirgilKey to default format, specified in Crypto API.
240 241 242 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 240 def export(password=nil) VirgilBuffer.from_bytes(context.crypto.export_private_key(private_key, password)) end |
#export_public_key ⇒ VirgilBuffer
Exports the Public key value from current VirgilKey.
247 248 249 250 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 247 def export_public_key public_key = context.crypto.extract_public_key(private_key) VirgilBuffer.from_bytes(context.crypto.export_public_key(public_key)) end |
#save(key_name, key_password = nil) ⇒ VirgilKey
Saves a current VirgilKey in secure storage.
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 217 def save(key_name, key_password=nil) raise ArgumentError.new("key_name is not valid") if key_name.nil? exported_private_key = context.crypto.export_private_key(private_key, key_password) storage_item = Cryptography::Keys::StorageItem.new(key_name, exported_private_key) context.key_storage.store(storage_item) self end |
#sign(buffer) ⇒ VirgilBuffer
Generates a digital signature for specified data using current Virgil key.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 107 def sign(buffer) buffer_to_sign = case buffer.class.name.split("::").last when 'VirgilBuffer' buffer when 'String' VirgilBuffer.from_string(buffer) when 'Array' VirgilBuffer.from_bytes(buffer) else raise ArgumentError.new("Buffer has unsupported type") end VirgilBuffer.new(context.crypto.sign(buffer_to_sign.bytes, private_key).to_s.bytes) end |
#sign_then_encrypt(buffer, recipients) ⇒ VirgilBuffer
Encrypts and signs the data.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 145 def sign_then_encrypt(buffer, recipients) raise ArgumentError.new("recipients is not valid") if (!recipients.is_a?(Array) || recipients.empty?) buffer_to_sign = case buffer.class.name.split("::").last when 'VirgilBuffer' buffer when 'String' VirgilBuffer.from_string(buffer) when 'Array' VirgilBuffer.from_bytes(buffer) else raise ArgumentError.new("Buffer has unsupported type") end public_keys = recipients.map(&:public_key) bytes = context.crypto.sign_then_encrypt(buffer_to_sign.bytes, private_key, *public_keys).to_s.bytes VirgilBuffer.new(bytes) end |