Class: Virgil::SDK::Cryptography::VirgilCrypto
- Inherits:
-
Object
- Object
- Virgil::SDK::Cryptography::VirgilCrypto
- Includes:
- Crypto
- Defined in:
- lib/virgil/sdk/cryptography/virgil_crypto.rb
Overview
Wrapper for cryptographic operations.
Class provides a cryptographic operations in applications, such as hashing, signature generation and verification, and encryption and decryption
Defined Under Namespace
Classes: SignatureIsNotValid
Constant Summary collapse
- CUSTOM_PARAM_KEY_SIGNATURE =
Crypto::Bytes.from_string( 'VIRGIL-DATA-SIGNATURE' )
- CUSTOM_PARAM_KEY_SIGNER_ID =
Crypto::Bytes.from_string( 'VIRGIL-DATA-SIGNER-ID' )
Instance Attribute Summary collapse
-
#key_pair_type ⇒ Object
Returns the value of attribute key_pair_type.
Instance Method Summary collapse
-
#calculate_fingerprint(bytes) ⇒ Hashes::Fingerprint
Calculates the fingerprint.
-
#compute_hash(bytes, algorithm) ⇒ Crypto::Bytes
Computes the hash of specified data.
-
#compute_public_key_hash(public_key) ⇒ Crypto::Bytes
Computes the hash of specified public key using SHA256 algorithm.
-
#decrypt(cipher_bytes, private_key) ⇒ Crypto::Bytes
Decrypts the specified bytes using Private key.
-
#decrypt_stream(input_stream, output_stream, private_key) ⇒ Object
Decrypts the specified stream using Private key.
-
#decrypt_then_verify(bytes, private_key, *public_keys) ⇒ Crypto::Bytes
Decrypts and verifies the data.
-
#encrypt(bytes, *recipients) ⇒ Crypto::Bytes
Encrypts the specified bytes using recipients Public keys.
-
#encrypt_stream(input_stream, output_stream, *recipients) ⇒ Crypto::Bytes
Encrypts the specified stream using recipients Public keys.
-
#export_private_key(private_key, password = nil) ⇒ Crypto::Bytes
Exports the Private key into material representation.
-
#export_public_key(public_key) ⇒ Crypto::Bytes
Exports the Public key into material representation.
-
#extract_public_key(private_key) ⇒ Keys::PublicKey
Extracts the Public key from Private key.
-
#generate_keys(keys_type = @key_pair_type) ⇒ Keys::KeyPair
Generates asymmetric key pair that is comprised of both public and private keys by specified type.
-
#import_private_key(key_bytes, password = nil) ⇒ Keys::PrivateKey
Imports the Private key from material representation.
-
#import_public_key(key_bytes) ⇒ Keys::PublicKey
Imports the Public key from material representation.
-
#initialize(key_pair_type = Keys::KeyPairType::Default) ⇒ VirgilCrypto
constructor
A new instance of VirgilCrypto.
-
#sign(bytes, private_key) ⇒ Crypto::Bytes
Signs the specified data using Private key.
-
#sign_stream(input_stream, private_key) ⇒ Crypto::Bytes
Signs the specified stream using Private key.
-
#sign_then_encrypt(bytes, private_key, *recipients) ⇒ Crypto::Bytes
Signs and encrypts the data.
-
#verify(bytes, signature, signer_public_key) ⇒ Boolean
Verifies the specified signature using original data and signer's public key.
-
#verify_stream(input_stream, signature, signer_public_key) ⇒ Boolean
Verifies the specified signature using original stream and signer's Public key.
Constructor Details
#initialize(key_pair_type = Keys::KeyPairType::Default) ⇒ VirgilCrypto
Returns a new instance of VirgilCrypto
47 48 49 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 47 def initialize(key_pair_type=Keys::KeyPairType::Default) @key_pair_type = key_pair_type end |
Instance Attribute Details
#key_pair_type ⇒ Object
Returns the value of attribute key_pair_type
45 46 47 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 45 def key_pair_type @key_pair_type end |
Instance Method Details
#calculate_fingerprint(bytes) ⇒ Hashes::Fingerprint
Calculates the fingerprint.
429 430 431 432 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 429 def calculate_fingerprint(bytes) hash_bytes = self.compute_hash(bytes, Hashes::HashAlgorithm::SHA256) Hashes::Fingerprint.new(hash_bytes) end |
#compute_hash(bytes, algorithm) ⇒ Crypto::Bytes
Computes the hash of specified data.
439 440 441 442 443 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 439 def compute_hash(bytes, algorithm) native_algorithm = Hashes::HashAlgorithm.convert_to_native(algorithm) native_hasher = Crypto::Native::VirgilHash.new(native_algorithm) wrap_bytes(native_hasher.hash(bytes)) end |
#compute_public_key_hash(public_key) ⇒ Crypto::Bytes
Computes the hash of specified public key using SHA256 algorithm.
448 449 450 451 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 448 def compute_public_key_hash(public_key) public_key_der = Crypto::Native::VirgilKeyPair.public_key_to_der(public_key) self.compute_hash(public_key_der, Hashes::HashAlgorithm::SHA256) end |
#decrypt(cipher_bytes, private_key) ⇒ Crypto::Bytes
Decrypts the specified bytes using Private key.
224 225 226 227 228 229 230 231 232 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 224 def decrypt(cipher_bytes, private_key) cipher = Crypto::Native::VirgilCipher.new decrypted_bytes = cipher.decrypt_with_key( cipher_bytes, private_key.receiver_id, private_key.value ) wrap_bytes(decrypted_bytes) end |
#decrypt_stream(input_stream, output_stream, private_key) ⇒ Object
Decrypts the specified stream using Private key.
389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 389 def decrypt_stream(input_stream, output_stream, private_key) cipher = Crypto::Native::VirgilChunkCipher.new source = Crypto::VirgilStreamDataSource.new(input_stream) sink = Crypto::VirgilStreamDataSink.new(output_stream) cipher.decrypt_with_key( source, sink, private_key.receiver_id, private_key.value ) end |
#decrypt_then_verify(bytes, private_key, *public_keys) ⇒ Crypto::Bytes
Decrypts and verifies the data.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 294 def decrypt_then_verify(bytes, private_key, *public_keys) cipher = Crypto::Native::VirgilCipher.new decrypted_bytes = cipher.decrypt_with_key( bytes, private_key.receiver_id, private_key.value ) signature = cipher.custom_params.get_data(CUSTOM_PARAM_KEY_SIGNATURE) signer_public_key = public_keys.first if public_keys.count > 1 signer_id = cipher.custom_params.get_data(CUSTOM_PARAM_KEY_SIGNER_ID) signer_public_key = public_keys.find{|public_key| public_key.receiver_id == signer_id} end is_valid = self.verify(decrypted_bytes, signature, signer_public_key) unless is_valid raise SignatureIsNotValid.new end wrap_bytes(decrypted_bytes) end |
#encrypt(bytes, *recipients) ⇒ Crypto::Bytes
Encrypts the specified bytes using recipients Public keys.
205 206 207 208 209 210 211 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 205 def encrypt(bytes, *recipients) cipher = Crypto::Native::VirgilCipher.new recipients.each do |public_key| cipher.add_key_recipient(public_key.receiver_id, public_key.value) end wrap_bytes(cipher.encrypt(bytes)) end |
#encrypt_stream(input_stream, output_stream, *recipients) ⇒ Crypto::Bytes
Encrypts the specified stream using recipients Public keys.
365 366 367 368 369 370 371 372 373 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 365 def encrypt_stream(input_stream, output_stream, *recipients) cipher = Crypto::Native::VirgilChunkCipher.new recipients.each do |public_key| cipher.add_key_recipient(public_key.receiver_id, public_key.value) end source = Crypto::VirgilStreamDataSource.new(input_stream) sink = Crypto::VirgilStreamDataSink.new(output_stream) cipher.encrypt(source, sink) end |
#export_private_key(private_key, password = nil) ⇒ Crypto::Bytes
Exports the Private key into material representation.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 144 def export_private_key(private_key, password=nil) unless password return Crypto::Native::VirgilKeyPair.private_key_to_der( private_key.value ) end password_bytes = Crypto::Bytes.from_string(password) private_key_bytes = Crypto::Native::VirgilKeyPair.encrypt_private_key( private_key.value, password_bytes ) wrap_bytes( Crypto::Native::VirgilKeyPair.private_key_to_der( private_key_bytes, password_bytes ) ) end |
#export_public_key(public_key) ⇒ Crypto::Bytes
Exports the Public key into material representation.
171 172 173 174 175 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 171 def export_public_key(public_key) wrap_bytes( Crypto::Native::VirgilKeyPair.public_key_to_der(public_key.value) ) end |
#extract_public_key(private_key) ⇒ Keys::PublicKey
Extracts the Public key from Private key.
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 180 def extract_public_key(private_key) public_key_bytes = Crypto::Native::VirgilKeyPair.extract_public_key( private_key.value, [] ) Keys::PublicKey.new( private_key.receiver_id, wrap_bytes( Crypto::Native::VirgilKeyPair.public_key_to_der(public_key_bytes) ) ) end |
#generate_keys(keys_type = @key_pair_type) ⇒ Keys::KeyPair
Generates asymmetric key pair that is comprised of both public and private keys by specified type.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 73 def generate_keys(keys_type=@key_pair_type) native_type = Keys::KeyPairType.convert_to_native(keys_type) native_key_pair = Crypto::Native::VirgilKeyPair.generate(native_type) key_pair_id = self.compute_public_key_hash(native_key_pair.public_key) private_key = Keys::PrivateKey.new( key_pair_id, wrap_bytes( Crypto::Native::VirgilKeyPair.private_key_to_der( native_key_pair.private_key ) ) ) public_key = Keys::PublicKey.new( key_pair_id, wrap_bytes( Crypto::Native::VirgilKeyPair.public_key_to_der( native_key_pair.public_key ) ) ) return Keys::KeyPair.new(private_key, public_key) end |
#import_private_key(key_bytes, password = nil) ⇒ Keys::PrivateKey
Imports the Private key from material representation.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 103 def import_private_key(key_bytes, password=nil) decrypted_private_key = if !password Crypto::Native::VirgilKeyPair.private_key_to_der(key_bytes) else Crypto::Native::VirgilKeyPair.decrypt_private_key( key_bytes, Crypto::Bytes.from_string(password) ) end public_key_bytes = Crypto::Native::VirgilKeyPair.extract_public_key( decrypted_private_key, [] ) key_pair_id = self.compute_public_key_hash(public_key_bytes) private_key_bytes = Crypto::Native::VirgilKeyPair.private_key_to_der( decrypted_private_key ) return Keys::PrivateKey.new(key_pair_id, wrap_bytes(private_key_bytes)) end |
#import_public_key(key_bytes) ⇒ Keys::PublicKey
Imports the Public key from material representation.
129 130 131 132 133 134 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 129 def import_public_key(key_bytes) key_pair_id = self.compute_public_key_hash(key_bytes) public_key_bytes = Crypto::Native::VirgilKeyPair.public_key_to_der(key_bytes) Keys::PublicKey.new(key_pair_id, wrap_bytes(public_key_bytes)) end |
#sign(bytes, private_key) ⇒ Crypto::Bytes
Signs the specified data using Private key.
330 331 332 333 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 330 def sign(bytes, private_key) signer = Crypto::Native::VirgilSigner.new wrap_bytes(signer.sign(bytes, private_key.value)) end |
#sign_stream(input_stream, private_key) ⇒ Crypto::Bytes
Signs the specified stream using Private key.
405 406 407 408 409 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 405 def sign_stream(input_stream, private_key) signer = Crypto::Native::VirgilStreamSigner.new source = Crypto::VirgilStreamDataSource.new(input_stream) wrap_bytes(signer.sign(source, private_key.value)) end |
#sign_then_encrypt(bytes, private_key, *recipients) ⇒ Crypto::Bytes
Signs and encrypts the data.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 253 def sign_then_encrypt(bytes, private_key, *recipients) signer = Crypto::Native::VirgilSigner.new signature = signer.sign(bytes, private_key.value) cipher = Crypto::Native::VirgilCipher.new custom_bytes = cipher.custom_params custom_bytes.set_data( CUSTOM_PARAM_KEY_SIGNATURE, signature ) public_key = extract_public_key(private_key) custom_bytes.set_data( CUSTOM_PARAM_KEY_SIGNER_ID, wrap_bytes(public_key.receiver_id) ) recipients.each do |public_key| cipher.add_key_recipient(public_key.receiver_id, public_key.value) end wrap_bytes(cipher.encrypt(bytes)) end |
#verify(bytes, signature, signer_public_key) ⇒ Boolean
Verifies the specified signature using original data and signer's public key.
347 348 349 350 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 347 def verify(bytes, signature, signer_public_key) signer = Crypto::Native::VirgilSigner.new signer.verify(bytes, signature, signer_public_key.value) end |
#verify_stream(input_stream, signature, signer_public_key) ⇒ Boolean
Verifies the specified signature using original stream and signer's Public key.
416 417 418 419 420 |
# File 'lib/virgil/sdk/cryptography/virgil_crypto.rb', line 416 def verify_stream(input_stream, signature, signer_public_key) signer = Crypto::Native::VirgilStreamSigner.new source = Crypto::VirgilStreamDataSource.new(input_stream) signer.verify(source, signature, signer_public_key.value) end |