Class: Virgil::SDK::HighLevel::VirgilKey

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(context, private_key) ⇒ VirgilKey

Returns a new instance of VirgilKey.



43
44
45
46
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 43

def initialize(context, private_key)
  @context = context
  @private_key = private_key
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



41
42
43
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 41

def context
  @context
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



41
42
43
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 41

def private_key
  @private_key
end

Instance Method Details

#decrypt(cipher_buffer) ⇒ Object

Decrypts the specified cipher data using Virgil key.

Args:

cipher_buffer: The encrypted data wrapped by VirgilBuffer or
               encrypted data in base64-encoded String
               or Array of bytes of encrypted data

Returns:

A byte array containing the result from performing the operation wrapped by VirgilBuffer.

Raises:

ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer, base64-encoded String or Array of bytes
Recipient with given identifier is not found  if user tries to decrypt cipher data by private key,
  though its public key was not used for encryption


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 63

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, card) ⇒ Object

Decrypts and verifies the data.

Args:

cipher_buffer: The data to be decrypted and verified:
               The encrypted data wrapped by VirgilBuffer or
               encrypted data in base64-encoded String
               or Array of bytes of encrypted data
card: The signer's VirgilCard

Returns:

The decrypted data, which is the original plain text before encryption The decrypted data, wrapped by VirgilBuffer

Raises:

ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer, String or Array of bytes
ArgumentError: recipients is not valid if recipients doesn't have type Array or empty

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 157

def decrypt_then_verify(cipher_buffer, card)

  raise ArgumentError.new("card is not valid") unless card.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

  bytes = context.crypto.decrypt_then_verify(buffer_to_decrypt.bytes, private_key, card.public_key)
  VirgilBuffer.new(bytes)
end

#export(password = nil) ⇒ Object

Exports the VirgilKey to default format, specified in Crypto API.



203
204
205
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 203

def export(password=nil)
  VirgilBuffer.from_bytes(context.crypto.export_private_key(private_key, password))
end

#export_public_keyObject

Exports the Public key value from current VirgilKey.

Returns:

A new VirgilBuffer that contains Public Key value.


212
213
214
215
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 212

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) ⇒ Object

Saves a current VirgilKey in secure storage.

Args:

key_name: The name of the key.
key_password: The key password.

Returns:

An instance of VirgilKey class

Raises:

 KeyEntryAlreadyExistsException: if key storage already has item with such name
ArgumentError: key_name is not valid if key_name is nil
KeyStorageException: Destination folder doesn't exist or you don't have permission to write there

Raises:

  • (ArgumentError)


190
191
192
193
194
195
196
197
198
199
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 190

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) ⇒ Object

Generates a digital signature for specified data using current Virgil key.

Args:

buffer: The data for which the digital signature will be generated.
        buffer can be VirgilBuffer, utf8-encoded String or Array of bytes

Returns:

A new buffer that containing the result from performing the operation.

Raises:

ArgumentError: Buffer has unsupported type if buffer doesn't have type VirgilBuffer, String or Array of bytes


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 92

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) ⇒ Object

Raises:

ArgumentError: Buffer has unsupported type if buffer doesn't have type VirgilBuffer, String or Array of bytes
ArgumentError: recipients is not valid if recipients doesn't have type Array or empty

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/virgil/sdk/high_level/virgil_key.rb', line 122

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