Module: Izokatu::Helpers
- Includes:
- Contracts
- Included in:
- Izokatu, Exporter, FileImporter, FunctionImporter
- Defined in:
- lib/izokatu/helpers.rb
Overview
Izokatu helper methods
Constant Summary collapse
- KEY_SYMBOL =
Default Symbol keys for keys
%i[private_key public_key].freeze
- RBNACL_KEY_CLASSES =
Rbnacl classes for keys
[ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey, RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey ].freeze
- EC_CIPHER =
Verify cipher is included in OpenSSL public key EC ciphers
->(c) { Izokatu::Openssl::PBKEY_EC_CIPHERS.include?(c) }
Instance Method Summary collapse
-
#decode_data(data) ⇒ Hash
Decoding data.
-
#encode_data(data) ⇒ Hash
Encoding data.
-
#export_data(data:, filename:, encode:) ⇒ Hash
Exporting data.
-
#export_decrypted!(decrypted_data:, encode:) ⇒ Hash
Exporting decrypted data.
-
#export_encrypted!(encrypted_data:, decrypter_params:, encode:) ⇒ Hash
Exporting encrypted data and decrypter params.
-
#generate_ec_keypair(cipher = 'secp521r1') ⇒ Hash
Generating EC keypair.
-
#generate_rbnacl_keypair ⇒ Hash
Generating RbNaCl keypair.
-
#generate_rsa_keypair(bit_number = 4096) ⇒ Hash
Generating RSA keypair.
-
#import_data(data:, filename:, delete_imported:, decode:) ⇒ Hash
Importing data.
-
#import_encrypted!(options:, decode:) ⇒ Hash
Importing encrypted data and decrypter params.
-
#import_encrypted_in_options! ⇒ Object
Performing encryption and merging result in options.
Instance Method Details
#decode_data(data) ⇒ Hash
Decoding data
210 211 212 |
# File 'lib/izokatu/helpers.rb', line 210 def decode_data(data) data.transform_values { |v| Base64.strict_decode64(v) if v } end |
#encode_data(data) ⇒ Hash
Encoding data
197 198 199 |
# File 'lib/izokatu/helpers.rb', line 197 def encode_data(data) data.transform_values { |v| Base64.strict_encode64(v) if v } end |
#export_data(data:, filename:, encode:) ⇒ Hash
Returning value even if not using :function exporter
Exporting data
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/izokatu/helpers.rb', line 122 def export_data(data:, filename:, encode:) = { data: data, encode: encode } = { data: data, filename: filename, encode: encode } exporter = [:exporter] = exporter == Izokatu::FileExporter ? : exporter.call(**) end |
#export_decrypted!(decrypted_data:, encode:) ⇒ Hash
Returning value even if not using :function exporter
Exporting decrypted data
101 102 103 104 105 106 107 |
# File 'lib/izokatu/helpers.rb', line 101 def export_decrypted!(decrypted_data:, encode:) export_data( data: decrypted_data, filename: [:decrypted_data_filename], encode: encode ) end |
#export_encrypted!(encrypted_data:, decrypter_params:, encode:) ⇒ Hash
Exporting encrypted data and decrypter params
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/izokatu/helpers.rb', line 74 def export_encrypted!(encrypted_data:, decrypter_params:, encode:) encrypted = export_data( data: encrypted_data, filename: [:encrypted_data_filename], encode: encode ) params = export_data( data: decrypter_params, filename: [:decrypter_params_filename], encode: encode ) encrypted && params ? encrypted.merge(params) : nil end |
#generate_ec_keypair(cipher = 'secp521r1') ⇒ Hash
Generating EC keypair
27 28 29 |
# File 'lib/izokatu/helpers.rb', line 27 def generate_ec_keypair(cipher = 'secp521r1') Izokatu::Openssl::PublicKey::EC::KeysGenerator.call(cipher: cipher) end |
#generate_rbnacl_keypair ⇒ Hash
Generating RbNaCl keypair
49 50 51 |
# File 'lib/izokatu/helpers.rb', line 49 def generate_rbnacl_keypair Izokatu::Rbnacl::PublicKey::KeysGenerator.call end |
#generate_rsa_keypair(bit_number = 4096) ⇒ Hash
Generating RSA keypair
39 40 41 |
# File 'lib/izokatu/helpers.rb', line 39 def generate_rsa_keypair(bit_number = 4096) Izokatu::Openssl::PublicKey::RSA::KeysGenerator.call(bit_number: bit_number) end |
#import_data(data:, filename:, delete_imported:, decode:) ⇒ Hash
Importing data
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/izokatu/helpers.rb', line 176 def import_data(data:, filename:, delete_imported:, decode:) = { data: data, decode: decode } = { filename: filename, delete_imported: delete_imported, decode: decode } importer = [:importer] = importer == Izokatu::FunctionImporter ? : importer.call(**) end |
#import_encrypted!(options:, decode:) ⇒ Hash
Importing encrypted data and decrypter params
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/izokatu/helpers.rb', line 144 def import_encrypted!(options:, decode:) encrypted_data = .select { |k, _v| k == :encrypted_data_string } decrypter_params_default_keys = %i[nonce key auth_data auth_tag] decrypter_params = .select { |k, _v| decrypter_params_default_keys.include?(k) } encrypted = import_data( data: encrypted_data, filename: [:encrypted_data_filename], delete_imported: [:delete_imported], decode: decode ) params = .merge! import_data( data: decrypter_params, filename: [:decrypter_params_filename], delete_imported: [:delete_imported], decode: decode ) [:encrypted_data_string] = encrypted.values[0] .merge!(params) end |
#import_encrypted_in_options! ⇒ Object
Performing encryption and merging result in options
57 58 59 60 61 |
# File 'lib/izokatu/helpers.rb', line 57 def encrypted_data, decrypter_params = encrypt() .merge!(encrypted_data) .merge!(decrypter_params) end |