izokatu 0.1.1

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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +2 -0
  4. data/.gitignore +19 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +42 -0
  7. data/.yardopts +1 -0
  8. data/Gemfile +8 -0
  9. data/Gemfile.lock +86 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +152 -0
  12. data/Rakefile +8 -0
  13. data/bin/console +15 -0
  14. data/bin/setup +8 -0
  15. data/certs/mongalless.pem +26 -0
  16. data/izokatu.gemspec +47 -0
  17. data/lib/izokatu.rb +423 -0
  18. data/lib/izokatu/action_call_options_selector.rb +214 -0
  19. data/lib/izokatu/action_call_selector.rb +132 -0
  20. data/lib/izokatu/callable.rb +13 -0
  21. data/lib/izokatu/ciphers.rb +18 -0
  22. data/lib/izokatu/decrypter.rb +62 -0
  23. data/lib/izokatu/encrypter.rb +67 -0
  24. data/lib/izokatu/exporter.rb +36 -0
  25. data/lib/izokatu/exporter/file_exporter.rb +36 -0
  26. data/lib/izokatu/exporter/function_exporter.rb +16 -0
  27. data/lib/izokatu/exporter/stdout_exporter.rb +18 -0
  28. data/lib/izokatu/helpers.rb +213 -0
  29. data/lib/izokatu/importer/file_importer.rb +47 -0
  30. data/lib/izokatu/importer/function_importer.rb +36 -0
  31. data/lib/izokatu/izokatu_elements_requires.rb +46 -0
  32. data/lib/izokatu/keys_generator.rb +53 -0
  33. data/lib/izokatu/openssl/private_key/auth/ccm/decrypter.rb +67 -0
  34. data/lib/izokatu/openssl/private_key/auth/ccm/encrypter.rb +56 -0
  35. data/lib/izokatu/openssl/private_key/auth/decrypter.rb +79 -0
  36. data/lib/izokatu/openssl/private_key/auth/encrypter.rb +69 -0
  37. data/lib/izokatu/openssl/private_key/default/decrypter.rb +75 -0
  38. data/lib/izokatu/openssl/private_key/default/encrypter.rb +75 -0
  39. data/lib/izokatu/openssl/public_key/ec/decrypter.rb +105 -0
  40. data/lib/izokatu/openssl/public_key/ec/encrypter.rb +106 -0
  41. data/lib/izokatu/openssl/public_key/ec/keys_generator.rb +77 -0
  42. data/lib/izokatu/openssl/public_key/rsa/decrypter.rb +53 -0
  43. data/lib/izokatu/openssl/public_key/rsa/encrypter.rb +55 -0
  44. data/lib/izokatu/openssl/public_key/rsa/keys_generator.rb +64 -0
  45. data/lib/izokatu/rbnacl/decrypter.rb +42 -0
  46. data/lib/izokatu/rbnacl/encrypter.rb +45 -0
  47. data/lib/izokatu/rbnacl/private_key/decrypter.rb +56 -0
  48. data/lib/izokatu/rbnacl/private_key/encrypter.rb +61 -0
  49. data/lib/izokatu/rbnacl/public_key/decrypter.rb +51 -0
  50. data/lib/izokatu/rbnacl/public_key/encrypter.rb +61 -0
  51. data/lib/izokatu/rbnacl/public_key/keys_generator.rb +33 -0
  52. data/lib/izokatu/version.rb +6 -0
  53. metadata +315 -0
  54. metadata.gz.sig +0 -0
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu selector of action class to be called
5
+ class ActionCallSelector
6
+ extend Callable
7
+
8
+ include Contracts
9
+
10
+ # @return [Symbol] library used for encryption/decryption
11
+ attr_reader :via
12
+ # @return [Symbol] mode of encryption/decryption
13
+ attr_reader :mode
14
+ # @return [Symbol] action to execute
15
+ attr_reader :action
16
+ # @return [Symbol] OpenSSL public key cipher type
17
+ attr_reader :asym_cipher_type
18
+ # @return [Bool] equality of cipher mode to CCM
19
+ attr_reader :ccm_cipher
20
+ # @return [Bool] status of cipher as authenticated
21
+ attr_reader :auth_cipher
22
+
23
+ Contract Contracts::HashOf[Symbol, Or[Symbol, Bool]] => Any
24
+ # Initializing options for selection of action class
25
+ #
26
+ # @param via (#via)
27
+ # @param mode (#mode)
28
+ # @param action (#action)
29
+ # @param asym_cipher_type (#asym_cipher_type)
30
+ # @param ccm_cipher (#ccm_cipher)
31
+ # @param auth_cipher (#auth_cipher)
32
+ #
33
+ # @since 0.1.0
34
+ def initialize(via:, mode:, action:, asym_cipher_type:, ccm_cipher:, auth_cipher:)
35
+ @via = via
36
+ @mode = mode
37
+ @action = action
38
+ @asym_cipher_type = asym_cipher_type
39
+ @ccm_cipher = ccm_cipher
40
+ @auth_cipher = auth_cipher
41
+ end
42
+
43
+ Contract None => Class
44
+ # Selecting action class for keys generation or encryption/decryption
45
+ #
46
+ # @return [Class] selected class for keys generation
47
+ #
48
+ # @since 0.1.0
49
+ def perform
50
+ action == :keys_generation ? select_keys_generation_action_call : select_default_action_call
51
+ end
52
+
53
+ private
54
+
55
+ Contract None => Class
56
+ # Selecting action class for keys generation
57
+ #
58
+ # @return [Class] selected class for keys generation
59
+ #
60
+ # @since 0.1.0
61
+ def select_keys_generation_action_call
62
+ case [via, mode, action, asym_cipher_type, ccm_cipher, auth_cipher]
63
+ in [:rbnacl, :public_key, :keys_generation, _, _, _]
64
+ Izokatu::Rbnacl::PublicKey::KeysGenerator
65
+ in [:openssl, :public_key, :keys_generation, :rsa, _, _]
66
+ Izokatu::Openssl::PublicKey::RSA::KeysGenerator
67
+ in [:openssl, :public_key, :keys_generation, :ec, _, _]
68
+ Izokatu::Openssl::PublicKey::EC::KeysGenerator
69
+ end
70
+ end
71
+
72
+ Contract None => Class
73
+ # Selecting action class for encryption/decryption
74
+ #
75
+ # @return [Class] selected class for encryption/decryption
76
+ #
77
+ # @since 0.1.0
78
+ def select_default_action_call
79
+ via == :rbnacl ? select_rbnacl_action_call : select_openssl_action_call
80
+ end
81
+
82
+ Contract None => Class
83
+ # Selecting action class for Rbnacl encryption/decryption
84
+ #
85
+ # @return [Class] selected class for Rbnal encryption/decryption
86
+ #
87
+ # @since 0.1.0
88
+ def select_rbnacl_action_call
89
+ case [via, mode, action, asym_cipher_type, ccm_cipher, auth_cipher]
90
+ in [:rbnacl, :private_key, :encryption, _, _, _]
91
+ Izokatu::Rbnacl::PrivateKey::Encrypter
92
+ in [:rbnacl, :private_key, :decryption, _, _, _]
93
+ Izokatu::Rbnacl::PrivateKey::Decrypter
94
+ in [:rbnacl, :public_key, :encryption, _, _, _]
95
+ Izokatu::Rbnacl::PublicKey::Encrypter
96
+ in [:rbnacl, :public_key, :decryption, _, _, _]
97
+ Izokatu::Rbnacl::PublicKey::Decrypter
98
+ end
99
+ end
100
+
101
+ Contract None => Class
102
+ # Selecting action class for Openssl encryption/decryption
103
+ #
104
+ # @return [Class] selected class for encryption/decryption
105
+ #
106
+ # @since 0.1.0
107
+ def select_openssl_action_call
108
+ case [via, mode, action, asym_cipher_type, ccm_cipher, auth_cipher]
109
+ in [:openssl, :private_key, :encryption, _, false, false]
110
+ Izokatu::Openssl::PrivateKey::Default::Encrypter
111
+ in [:openssl, :private_key, :decryption, _, false, false]
112
+ Izokatu::Openssl::PrivateKey::Default::Decrypter
113
+ in [:openssl, :private_key, :encryption, _, false, true]
114
+ Izokatu::Openssl::PrivateKey::Auth::Encrypter
115
+ in [:openssl, :private_key, :decryption, _, false, true]
116
+ Izokatu::Openssl::PrivateKey::Auth::Decrypter
117
+ in [:openssl, :private_key, :encryption, _, true, _]
118
+ Izokatu::Openssl::PrivateKey::Auth::CCM::Encrypter
119
+ in [:openssl, :private_key, :decryption, _, true, _]
120
+ Izokatu::Openssl::PrivateKey::Auth::CCM::Decrypter
121
+ in [:openssl, :public_key, :encryption, :rsa, _, _]
122
+ Izokatu::Openssl::PublicKey::RSA::Encrypter
123
+ in [:openssl, :public_key, :decryption, :rsa, _, _]
124
+ Izokatu::Openssl::PublicKey::RSA::Decrypter
125
+ in [:openssl, :public_key, :encryption, :ec, _, _]
126
+ Izokatu::Openssl::PublicKey::EC::Encrypter
127
+ in [:openssl, :public_key, :decryption, :ec, _, _]
128
+ Izokatu::Openssl::PublicKey::EC::Decrypter
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Shared call method for classes
5
+ module Callable
6
+ # Public call method for classes
7
+ #
8
+ # @since 0.1.0
9
+ def call(...)
10
+ new(...).perform
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Namespace for OpenSSL classes
5
+ module Openssl
6
+ # List of OpenSSL private key ciphers
7
+ PKEY_CIPHERS = OpenSSL::Cipher.ciphers.map(&:upcase)
8
+
9
+ # Wrap cipher are not working in Openssl. Here text of error:
10
+ # OpenSSL::Cipher::CipherError: wrap mode not allowed
11
+ # Or straight from openssl 1.1.1d
12
+ # error:0607B0AA:digital envelope routines:EVP_CipherInit_ex:wrap mode not allowed:../crypto/evp/evp_enc.c:161
13
+ PKEY_CIPHERS.delete_if { |name| name.include?('WRAP') }
14
+
15
+ # List of OpenSSL public key EC ciphers
16
+ PBKEY_EC_CIPHERS = OpenSSL::PKey::EC.builtin_curves.map! { |cipher_name, _| cipher_name }
17
+ end
18
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Abstract class for decrypters
5
+ #
6
+ # @abstract Subclasses are containing implementation of {#decrypt_data!}
7
+ class Decrypter
8
+ extend Callable
9
+
10
+ include Contracts
11
+
12
+ # @return decrypter instance
13
+ attr_reader :decrypter
14
+ # @return [String] encrypted data for decryption
15
+ attr_reader :encrypted_data
16
+ # @return [Hash] decrypted data for export
17
+ attr_reader :decrypted_data
18
+
19
+ Contract Contracts::HashOf[Symbol, String] => Any
20
+ # Initializing option for decryption
21
+ #
22
+ # @param encrypted_data (#encrypted_data)
23
+ #
24
+ # @since 0.1.0
25
+ def initialize(encrypted_data:)
26
+ import_encrypted_data!(encrypted_data)
27
+ end
28
+
29
+ Contract String => String
30
+ # Importing encrypted data from param
31
+ #
32
+ # @param encrypted_data (#encrypted_data)
33
+ #
34
+ # @return [String] (#encrypted_data)
35
+ #
36
+ # @since 0.1.0
37
+ def import_encrypted_data!(encrypted_data)
38
+ @encrypted_data = encrypted_data
39
+ end
40
+
41
+ Contract None => Contracts::HashOf[Symbol, String]
42
+ # Performing data decryption
43
+ #
44
+ # @return [Hash] decrypted data
45
+ #
46
+ # @since 0.1.0
47
+ def perform
48
+ decrypt_data!
49
+ end
50
+
51
+ private
52
+
53
+ # Decrypting data
54
+ #
55
+ # @raise RuntimeError
56
+ #
57
+ # @since 0.1.0
58
+ def decrypt_data!
59
+ raise 'Not implemented!'
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Abstract class for encrypters
5
+ #
6
+ # @abstract Subclasses are containing implementation of {#encrypt_data!}
7
+ class Encrypter
8
+ extend Callable
9
+
10
+ include Contracts
11
+
12
+ # @return encrypter instance
13
+ attr_reader :encrypter
14
+ # @return [String] clear data for encryption
15
+ attr_reader :clear_data
16
+ # @return [Hash] encrypted data for export
17
+ attr_reader :encrypted_data
18
+
19
+ # Default options for Izokatu::Encrypter
20
+ DEFAULT_OPTIONS = {
21
+ clear_data_string: 'Testing, testing...'
22
+ }.freeze
23
+
24
+ Contract Contracts::HashOf[Symbol, String] => Any
25
+ # Initializing option for encryption
26
+ #
27
+ # @param clear_data (#clear_data)
28
+ #
29
+ # @since 0.1.0
30
+ def initialize(clear_data:)
31
+ import_clear_data!(clear_data)
32
+ end
33
+
34
+ Contract String => String
35
+ # Imporing clear data from options
36
+ #
37
+ # @param clear_data (#clear_data)
38
+ #
39
+ # @return [String] (#clear_data)
40
+ #
41
+ # @since 0.1.0
42
+ def import_clear_data!(clear_data)
43
+ @clear_data = clear_data || DEFAULT_OPTIONS[:clear_data_string]
44
+ end
45
+
46
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, Maybe[String]]]
47
+ # Performing encryption
48
+ #
49
+ # @return [Hash] encrypted data with params
50
+ #
51
+ # @since 0.1.0
52
+ def perform
53
+ encrypt_data!
54
+ end
55
+
56
+ private
57
+
58
+ # Encrypting data`
59
+ #
60
+ # @raise RuntimeError
61
+ #
62
+ # @since 0.1.0
63
+ def encrypt_data!
64
+ raise 'Not implemented!'
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Abstract class for exporters
5
+ #
6
+ # @abstract Subclasses are containing implementation of {#perform}
7
+ class Exporter
8
+ extend Callable
9
+
10
+ include Contracts
11
+ include Izokatu::Helpers
12
+
13
+ # @return [String] data for export
14
+ attr_reader :data
15
+
16
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Maybe[String]], Bool]] => Any
17
+ # Initializing data for export
18
+ #
19
+ # @param data (#data)
20
+ # @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data
21
+ #
22
+ # @since 0.1.0
23
+ def initialize(data:, encode:)
24
+ @data = encode ? encode_data(data) : data
25
+ end
26
+
27
+ # Performing export of initialized data
28
+ #
29
+ # @raise RuntimeError
30
+ #
31
+ # @since 0.1.0
32
+ def perform
33
+ raise 'Not implemented!'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu exporter for exporting data in some file
5
+ class FileExporter < Izokatu::Exporter
6
+ # @return [String] name of file to export
7
+ attr_reader :filename
8
+
9
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Maybe[String]], String, Bool]] => Any
10
+ # Initializing data for file export
11
+ #
12
+ # @param data (#data)
13
+ # @param filename (#filename)
14
+ # @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data
15
+ #
16
+ # @since 0.1.0
17
+ def initialize(data:, filename:, encode:)
18
+ super(data: data, encode: encode)
19
+ @filename = filename
20
+ end
21
+
22
+ Contract None => Contracts::HashOf[Symbol, Maybe[String]]
23
+ # Performing export of initialized data
24
+ #
25
+ # @return [Hash] exported data
26
+ #
27
+ # @since 0.1.0
28
+ def perform
29
+ File.open(filename, 'w') do |f|
30
+ data.each do |_k, v|
31
+ f.puts(v)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu exporter for exporting data in some function
5
+ class FunctionExporter < Izokatu::Exporter
6
+ Contract None => Contracts::HashOf[Symbol, Maybe[String]]
7
+ # Performing export of initialized data
8
+ #
9
+ # @return [Hash] exported data
10
+ #
11
+ # @since 0.1.0
12
+ def perform
13
+ data
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu exporter for exporting data in STDOUT
5
+ class StdoutExporter < Izokatu::Exporter
6
+ Contract None => Contracts::HashOf[Symbol, String]
7
+ # Performing export of initialized data
8
+ #
9
+ # @return [Hash] exported data
10
+ #
11
+ # @since 0.1.0
12
+ def perform
13
+ data.each do |_k, v|
14
+ puts v
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu helper methods
5
+ module Helpers
6
+ include Contracts
7
+
8
+ # Default Symbol keys for keys
9
+ KEY_SYMBOL = %i[private_key public_key].freeze
10
+ # Rbnacl classes for keys
11
+ RBNACL_KEY_CLASSES = [
12
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey,
13
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey
14
+ ].freeze
15
+
16
+ # Verify cipher is included in OpenSSL public key EC ciphers
17
+ EC_CIPHER = ->(c) { Izokatu::Openssl::PBKEY_EC_CIPHERS.include?(c) }
18
+
19
+ Contract EC_CIPHER => Contracts::HashOf[Or[*KEY_SYMBOL], String]
20
+ # Generating EC keypair
21
+ #
22
+ # @param cipher [String] cipher for keys encryption
23
+ #
24
+ # @return [Hash] keypair of EC public and private key
25
+ #
26
+ # @since 0.1.0
27
+ def generate_ec_keypair(cipher = 'secp521r1')
28
+ Izokatu::Openssl::PublicKey::EC::KeysGenerator.call(cipher: cipher)
29
+ end
30
+
31
+ Contract Pos => Contracts::HashOf[Or[*KEY_SYMBOL], String]
32
+ # Generating RSA keypair
33
+ #
34
+ # @param bit_number [Integer] number of key bits
35
+ #
36
+ # @return [Hash] keypair of RSA public and private key
37
+ #
38
+ # @since 0.1.0
39
+ def generate_rsa_keypair(bit_number = 4096)
40
+ Izokatu::Openssl::PublicKey::RSA::KeysGenerator.call(bit_number: bit_number)
41
+ end
42
+
43
+ Contract None => Contracts::HashOf[Or[*KEY_SYMBOL], Or[*RBNACL_KEY_CLASSES]]
44
+ # Generating RbNaCl keypair
45
+ #
46
+ # @return [Hash] keypair of RbNaCl public and private key
47
+ #
48
+ # @since 0.1.0
49
+ def generate_rbnacl_keypair
50
+ Izokatu::Rbnacl::PublicKey::KeysGenerator.call
51
+ end
52
+
53
+ Contract None => Contracts::HashOf[Symbol, Or[String, Symbol, Bool]]
54
+ # Performing encryption and merging result in options
55
+ #
56
+ # @since 0.1.0
57
+ def import_encrypted_in_options!
58
+ encrypted_data, decrypter_params = encrypt(options)
59
+ options.merge!(encrypted_data)
60
+ options.merge!(decrypter_params)
61
+ end
62
+
63
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Maybe[String]], String, Bool]] =>
64
+ Contracts::HashOf[Symbol, Maybe[String]]
65
+ # Exporting encrypted data and decrypter params
66
+ #
67
+ # @param encrypted_data [Hash] Hash with encrypted data for export
68
+ # @param decrypter_params [Hash] Hash with decrypter params for export
69
+ # @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data
70
+ #
71
+ # @return [Hash] merged encrypted data and decrypter params
72
+ #
73
+ # @note Returning merged value even if not using :function exporter
74
+ #
75
+ # @since 0.1.0
76
+ def export_encrypted!(encrypted_data:, decrypter_params:, encode:)
77
+ export_data(
78
+ data: encrypted_data,
79
+ filename: options[:encrypted_data_filename],
80
+ encode: encode
81
+ ).merge!(
82
+ export_data(
83
+ data: decrypter_params,
84
+ filename: options[:decrypter_params_filename],
85
+ encode: encode
86
+ )
87
+ )
88
+ end
89
+
90
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, String], String, Bool]] =>
91
+ Contracts::HashOf[Symbol, String]
92
+ # Exporting decrypted data
93
+ #
94
+ # @param decrypted_data [Hash] Hash with encrypted data for export
95
+ # @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data
96
+ #
97
+ # @return [Hash] decrypted data
98
+ #
99
+ # @note Returning value even if not using :function exporter
100
+ #
101
+ # @since 0.1.0
102
+ def export_decrypted!(decrypted_data:, encode:)
103
+ export_data(
104
+ data: decrypted_data,
105
+ filename: options[:decrypted_data_filename],
106
+ encode: encode
107
+ )
108
+ end
109
+
110
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Maybe[String]], Maybe[String], Bool]] =>
111
+ Contracts::HashOf[Symbol, Maybe[String]]
112
+ # Exporting data
113
+ #
114
+ # @param data [Hash] Hash with data for export
115
+ # @param filename [String] Name of file for export
116
+ # @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data
117
+ #
118
+ # @return [Hash] exported data
119
+ #
120
+ # @note Returning value even if not using :function exporter
121
+ #
122
+ # @since 0.1.0
123
+ def export_data(data:, filename:, encode:)
124
+ other_options = { data: data, encode: encode }
125
+ file_options = {
126
+ data: data,
127
+ filename: filename,
128
+ encode: encode
129
+ }
130
+ exporter = options[:exporter]
131
+ exporter_options = exporter == Izokatu::FileExporter ? file_options : other_options
132
+ exporter.call(**exporter_options)
133
+ end
134
+
135
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Any], Bool]] =>
136
+ Contracts::HashOf[Symbol, Any]
137
+ # Importing encrypted data and decrypter params
138
+ #
139
+ # @param options [Hash] Hash with options
140
+ # @param decode [TrueClass || FalseClass] Enable/disable decoding of imported data
141
+ #
142
+ # @return [Hash] Hash with updated options
143
+ #
144
+ # @since 0.1.0
145
+ def import_encrypted!(options:, decode:)
146
+ encrypted_data = options.select { |k, _v| k == :encrypted_data_string }
147
+ decrypter_params_default_keys = %i[nonce key auth_data auth_tag]
148
+ decrypter_params = options.select { |k, _v| decrypter_params_default_keys.include?(k) }
149
+ options.merge! import_data(
150
+ data: encrypted_data,
151
+ filename: options[:encrypted_data_filename],
152
+ delete_imported: options[:delete_imported],
153
+ decode: decode
154
+ )
155
+ options.merge! import_data(
156
+ data: decrypter_params,
157
+ filename: options[:decrypter_params_filename],
158
+ delete_imported: options[:delete_imported],
159
+ decode: decode
160
+ )
161
+ end
162
+
163
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, String], Any]] =>
164
+ Contracts::HashOf[Symbol, Maybe[String]]
165
+ # Importing data
166
+ #
167
+ # @param data [Hash] Hash with data to import from
168
+ # @param filename [String] Name of file to import from
169
+ # @param delete_imported [TrueClass, FalseClass] Enable/disable deleting file after import
170
+ # @param decode [TrueClass || FalseClass] Enable/disable decoding of imported data
171
+ #
172
+ # @return [Hash] imported data
173
+ #
174
+ # @since 0.1.0
175
+ def import_data(data:, filename:, delete_imported:, decode:)
176
+ function_options = { data: data, decode: decode }
177
+ file_options = {
178
+ filename: filename,
179
+ delete_imported: delete_imported,
180
+ decode: decode
181
+ }
182
+ importer = options[:importer]
183
+ importer_options = importer == Izokatu::FunctionImporter ? function_options : file_options
184
+ importer.call(**importer_options)
185
+ end
186
+
187
+ Contract Contracts::HashOf[Symbol, Maybe[String]] =>
188
+ Contracts::HashOf[Symbol, Maybe[String]]
189
+ # Encoding data
190
+ #
191
+ # @param data [Hash] Hash with data for encoding
192
+ #
193
+ # @return [Hash] encoded data
194
+ #
195
+ # @since 0.1.0
196
+ def encode_data(data)
197
+ data.transform_values { |v| Base64.strict_encode64(v) if v }
198
+ end
199
+
200
+ Contract Contracts::HashOf[Symbol, Maybe[String]] =>
201
+ Contracts::HashOf[Symbol, Maybe[String]]
202
+ # Decoding data
203
+ #
204
+ # @param data [Hash] Hash with data for decoding
205
+ #
206
+ # @return [Hash] decoded data
207
+ #
208
+ # @since 0.1.0
209
+ def decode_data(data)
210
+ data.transform_values { |v| Base64.strict_decode64(v) if v }
211
+ end
212
+ end
213
+ end