virgil-sdk 4.2.4 → 4.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,19 +71,27 @@ module Virgil
71
71
  # Encrypts the specified data using recipients Public keys.
72
72
  #
73
73
  # Args:
74
- # buffer: The data to be encrypted.
74
+ # buffer: The data to be encrypted. It can be VirgilBuffer, utf8 String or Array of bytes
75
75
  #
76
76
  # Returns:
77
77
  # Encrypted data for current recipients Public keys
78
78
  #
79
79
  # Raises:
80
- # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer or String
80
+ # ArgumentError: Buffer has unsupported type if buffer doesn't have type VirgilBuffer, String or Array of bytes
81
81
  def encrypt(buffer)
82
-
83
- raise ArgumentError.new("buffer is not valid") if !(buffer.is_a?(VirgilBuffer) || buffer.is_a?(String))
84
-
85
82
  all_public_keys = self.map(&:public_key)
86
- VirgilBuffer.new(crypto.encrypt(buffer.bytes, *all_public_keys))
83
+ buffer_to_encrypt = case buffer.class.name.split("::").last
84
+ when 'VirgilBuffer'
85
+ buffer
86
+ when 'String'
87
+ VirgilBuffer.from_string(buffer)
88
+ when 'Array'
89
+ VirgilBuffer.from_bytes(buffer)
90
+ else
91
+ raise ArgumentError.new("Buffer has unsupported type")
92
+ end
93
+
94
+ VirgilBuffer.new(crypto.encrypt(buffer_to_encrypt.bytes, *all_public_keys))
87
95
  end
88
96
 
89
97
  end
@@ -36,18 +36,20 @@ module Virgil
36
36
  module HighLevel
37
37
  class VirgilContext
38
38
  attr_reader :access_token, :client, :crypto, :credentials,
39
- :cards_service_url, :cards_read_only_service_url,
39
+ :cards_service_url, :cards_read_only_service_url, :ra_service_url,
40
40
  :identity_service_url, :key_storage
41
41
 
42
- def initialize(access_token:, credentials: nil, key_storage_path: Cryptography::Keys::KeyStorage.default_folder,
42
+ def initialize(access_token: nil, credentials: nil, key_storage_path: Cryptography::Keys::KeyStorage.default_folder,
43
43
  cards_service_url: Client::Card::SERVICE_URL,
44
44
  cards_read_only_service_url: Client::Card::READ_ONLY_SERVICE_URL,
45
+ ra_service_url: Client::Card::RA_SERVICE_URL,
45
46
  identity_service_url: VirgilIdentity::IDENTITY_SERVICE_URL,
47
+ crypto: Cryptography::VirgilCrypto.new,
46
48
  card_verifiers: []
47
49
  )
48
50
  @access_token = access_token
49
- @client = Client::VirgilClient.new(access_token, cards_service_url, cards_read_only_service_url, identity_service_url)
50
- @crypto = Cryptography::VirgilCrypto.new
51
+ @client = Client::VirgilClient.new(access_token, cards_service_url, cards_read_only_service_url, identity_service_url, ra_service_url)
52
+ @crypto = crypto
51
53
  @credentials = credentials
52
54
  @key_storage = Cryptography::Keys::KeyStorage.new(key_storage_path)
53
55
 
@@ -49,18 +49,31 @@ module Virgil
49
49
  # Decrypts the specified cipher data using Virgil key.
50
50
  #
51
51
  # Args:
52
- # cipher_buffer: The encrypted data wrapped by VirgilBuffer.
52
+ # cipher_buffer: The encrypted data wrapped by VirgilBuffer or
53
+ # encrypted data in base64-encoded String
54
+ # or Array of bytes of encrypted data
53
55
  #
54
56
  # Returns:
55
57
  # A byte array containing the result from performing the operation wrapped by VirgilBuffer.
56
58
  #
57
59
  # Raises:
58
- # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer or String
60
+ # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer, base64-encoded String or Array of bytes
59
61
  # Recipient with given identifier is not found if user tries to decrypt cipher data by private key,
60
62
  # though its public key was not used for encryption
61
63
  def decrypt(cipher_buffer)
62
- VirgilBuffer.validate_buffer_param(cipher_buffer, "cipher buffer")
63
- bytes = context.crypto.decrypt(cipher_buffer.bytes, private_key)
64
+
65
+ buffer_to_decrypt = case cipher_buffer.class.name.split("::").last
66
+ when 'VirgilBuffer'
67
+ cipher_buffer
68
+ when 'String'
69
+ VirgilBuffer.from_base64(cipher_buffer)
70
+ when 'Array'
71
+ VirgilBuffer.from_bytes(cipher_buffer)
72
+ else
73
+ raise ArgumentError.new("Buffer has unsupported type")
74
+ end
75
+
76
+ bytes = context.crypto.decrypt(buffer_to_decrypt.bytes, private_key)
64
77
  VirgilBuffer.new(bytes)
65
78
  end
66
79
 
@@ -68,17 +81,27 @@ module Virgil
68
81
  # Generates a digital signature for specified data using current Virgil key.
69
82
  #
70
83
  # Args:
71
- # buffer: The data wrapped by VirgilBuffer for which the digital signature will be generated.
84
+ # buffer: The data for which the digital signature will be generated.
85
+ # buffer can be VirgilBuffer, utf8-encoded String or Array of bytes
72
86
  #
73
87
  # Returns:
74
88
  # A new buffer that containing the result from performing the operation.
75
89
  #
76
90
  # Raises:
77
- # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer or String
91
+ # ArgumentError: Buffer has unsupported type if buffer doesn't have type VirgilBuffer, String or Array of bytes
78
92
  def sign(buffer)
79
- VirgilBuffer.validate_buffer_param(buffer)
80
- bytes = context.crypto.sign(buffer.bytes, private_key)
81
- VirgilBuffer.new(bytes)
93
+ buffer_to_sign = case buffer.class.name.split("::").last
94
+ when 'VirgilBuffer'
95
+ buffer
96
+ when 'String'
97
+ VirgilBuffer.from_string(buffer)
98
+ when 'Array'
99
+ VirgilBuffer.from_bytes(buffer)
100
+ else
101
+ raise ArgumentError.new("Buffer has unsupported type")
102
+ end
103
+
104
+ VirgilBuffer.new(context.crypto.sign(buffer_to_sign.bytes, private_key).to_s.bytes)
82
105
  end
83
106
 
84
107
 
@@ -86,21 +109,31 @@ module Virgil
86
109
  #
87
110
  # Args:
88
111
  # buffer: The data wrapped by VirgilBuffer to be encrypted and signed
89
- # recipients: The list of VirgilCard recipients.
112
+ # recipients: The list of VirgilCard recipients.
113
+ # buffer can be VirgilBuffer, utf8-encoded String or Array of bytes
90
114
  #
91
115
  # Returns:
92
116
  # A new buffer that containing the encrypted and signed data
93
117
  #
94
118
 
95
119
  # Raises:
96
- # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer or String
120
+ # ArgumentError: Buffer has unsupported type if buffer doesn't have type VirgilBuffer, String or Array of bytes
97
121
  # ArgumentError: recipients is not valid if recipients doesn't have type Array or empty
98
122
  def sign_then_encrypt(buffer, recipients)
99
123
 
100
- VirgilBuffer.validate_buffer_param(buffer)
101
124
  raise ArgumentError.new("recipients is not valid") if (!recipients.is_a?(Array) || recipients.empty?)
125
+ buffer_to_sign = case buffer.class.name.split("::").last
126
+ when 'VirgilBuffer'
127
+ buffer
128
+ when 'String'
129
+ VirgilBuffer.from_string(buffer)
130
+ when 'Array'
131
+ VirgilBuffer.from_bytes(buffer)
132
+ else
133
+ raise ArgumentError.new("Buffer has unsupported type")
134
+ end
102
135
  public_keys = recipients.map(&:public_key)
103
- bytes = context.crypto.sign_then_encrypt(buffer.bytes, private_key, *public_keys)
136
+ bytes = context.crypto.sign_then_encrypt(buffer_to_sign.bytes, private_key, *public_keys).to_s.bytes
104
137
  VirgilBuffer.new(bytes)
105
138
 
106
139
  end
@@ -109,21 +142,34 @@ module Virgil
109
142
  # Decrypts and verifies the data.
110
143
  #
111
144
  # Args:
112
- # cipher_buffer: The data to be decrypted and verified
145
+ # cipher_buffer: The data to be decrypted and verified:
146
+ # The encrypted data wrapped by VirgilBuffer or
147
+ # encrypted data in base64-encoded String
148
+ # or Array of bytes of encrypted data
113
149
  # card: The signer's VirgilCard
114
150
  #
115
151
  # Returns:
116
152
  # The decrypted data, which is the original plain text before encryption The decrypted data, wrapped by VirgilBuffer
117
153
  #
118
154
  # Raises:
119
- # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer or String
155
+ # ArgumentError: buffer is not valid if buffer doesn't have type VirgilBuffer, String or Array of bytes
120
156
  # ArgumentError: recipients is not valid if recipients doesn't have type Array or empty
121
157
  def decrypt_then_verify(cipher_buffer, card)
122
158
 
123
- VirgilBuffer.validate_buffer_param(cipher_buffer, "cipher buffer")
124
159
  raise ArgumentError.new("card is not valid") unless card.is_a?(VirgilCard)
125
160
 
126
- bytes = context.crypto.decrypt_then_verify(cipher_buffer.bytes, private_key, card.public_key)
161
+ buffer_to_decrypt = case cipher_buffer.class.name.split("::").last
162
+ when 'VirgilBuffer'
163
+ cipher_buffer
164
+ when 'String'
165
+ VirgilBuffer.from_base64(cipher_buffer)
166
+ when 'Array'
167
+ VirgilBuffer.from_bytes(cipher_buffer)
168
+ else
169
+ raise ArgumentError.new("Buffer has unsupported type")
170
+ end
171
+
172
+ bytes = context.crypto.decrypt_then_verify(buffer_to_decrypt.bytes, private_key, card.public_key)
127
173
  VirgilBuffer.new(bytes)
128
174
  end
129
175
 
@@ -153,6 +199,12 @@ module Virgil
153
199
  end
154
200
 
155
201
 
202
+ # Exports the VirgilKey to default format, specified in Crypto API.
203
+ def export(password=nil)
204
+ VirgilBuffer.from_bytes(context.crypto.export_private_key(private_key, password))
205
+ end
206
+
207
+
156
208
  # Exports the Public key value from current VirgilKey.
157
209
  #
158
210
  # Returns:
@@ -74,6 +74,16 @@ module Virgil
74
74
 
75
75
  end
76
76
 
77
+ # Imports the VirgilKey from buffer.
78
+ #
79
+ # Args:
80
+ # buffer: The buffer with Key
81
+ # password: The Key password
82
+ def import(buffer, password=nil)
83
+ private_key = context.crypto.import_private_key(buffer.bytes, password)
84
+ VirgilKey.new(context, private_key)
85
+ end
86
+
77
87
 
78
88
  # Remove the VirgilKey from current storage by specified key name.
79
89
  #
@@ -1,5 +1,5 @@
1
1
  module Virgil
2
2
  module SDK
3
- VERSION = "4.2.4"
3
+ VERSION = "4.2.5"
4
4
  end
5
5
  end
data/virgil-sdk.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.required_ruby_version = '>= 2.2.0'
23
- spec.add_runtime_dependency "virgil-crypto", ">= 2.0.6r4"
23
+ spec.add_runtime_dependency "virgil-crypto", ">= 2.0.7"
24
24
  spec.add_runtime_dependency "faraday", "~> 0.10.0"
25
25
  spec.add_runtime_dependency "faraday_middleware", "~> 0.10.0"
26
26
  spec.add_development_dependency "bundler", "~> 1.12"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virgil-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.4
4
+ version: 4.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitriy Dudkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-02 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virgil-crypto
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.0.6r4
19
+ version: 2.0.7
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.0.6r4
26
+ version: 2.0.7
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +119,7 @@ files:
119
119
  - ".gitignore"
120
120
  - Gemfile
121
121
  - README.md
122
+ - README_LOW_LEVEL.md
122
123
  - Rakefile
123
124
  - bin/console
124
125
  - bin/setup
@@ -139,13 +140,14 @@ files:
139
140
  - lib/virgil/sdk/client/http/request.rb
140
141
  - lib/virgil/sdk/client/request_signer.rb
141
142
  - lib/virgil/sdk/client/requests.rb
143
+ - lib/virgil/sdk/client/requests/add_relation_request.rb
142
144
  - lib/virgil/sdk/client/requests/confirm_identity_request.rb
143
145
  - lib/virgil/sdk/client/requests/create_card_request.rb
146
+ - lib/virgil/sdk/client/requests/delete_relation_request.rb
144
147
  - lib/virgil/sdk/client/requests/revoke_card_request.rb
145
148
  - lib/virgil/sdk/client/requests/signable_request.rb
146
149
  - lib/virgil/sdk/client/requests/verify_identity_request.rb
147
150
  - lib/virgil/sdk/client/search_criteria.rb
148
- - lib/virgil/sdk/client/signatures_base64.rb
149
151
  - lib/virgil/sdk/client/virgil_client.rb
150
152
  - lib/virgil/sdk/cryptography.rb
151
153
  - lib/virgil/sdk/cryptography/hashes.rb
@@ -1,25 +0,0 @@
1
- module Virgil
2
- module SDK
3
- module Client
4
- module SignaturesBase64
5
-
6
- def signatures_to_base64(signatures_bytes)
7
- encoded_signatures = {}
8
- signatures_bytes.each do |key, val|
9
- encoded_signatures[key] = Base64.strict_encode64(Virgil::Crypto::Bytes.new(val).to_s) #TODO
10
- end
11
- encoded_signatures
12
- end
13
-
14
-
15
- def signatures_from_base64(signatures_base64)
16
- decoded_signatures = {}
17
- signatures_base64.each do |key, val|
18
- decoded_signatures[key] = Virgil::Crypto::Bytes.from_base64(val)
19
- end
20
- decoded_signatures
21
- end
22
- end
23
- end
24
- end
25
- end