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

Instance Method Details

#decode_data(data) ⇒ Hash

Decoding data

Parameters:

  • data (Hash)

    Hash with data for decoding

Returns:

  • (Hash)

    decoded data

Since:

  • 0.1.0



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

Parameters:

  • data (Hash)

    Hash with data for encoding

Returns:

  • (Hash)

    encoded data

Since:

  • 0.1.0



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

Note:

Returning value even if not using :function exporter

Exporting data

Parameters:

  • data (Hash)

    Hash with data for export

  • filename (String)

    Name of file for export

  • encode (TrueClass || FalseClass)

    Enable/disable encoding of exported data

Returns:

  • (Hash)

    exported data

Since:

  • 0.1.0



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/izokatu/helpers.rb', line 122

def export_data(data:, filename:, encode:)
  other_options = { data: data, encode: encode }
  file_options = {
    data: data,
    filename: filename,
    encode: encode
  }
  exporter = options[:exporter]
  exporter_options = exporter == Izokatu::FileExporter ? file_options : other_options
  exporter.call(**exporter_options)
end

#export_decrypted!(decrypted_data:, encode:) ⇒ Hash

Note:

Returning value even if not using :function exporter

Exporting decrypted data

Parameters:

  • decrypted_data (Hash)

    Hash with encrypted data for export

  • encode (TrueClass || FalseClass)

    Enable/disable encoding of exported data

Returns:

  • (Hash)

    decrypted data

Since:

  • 0.1.0



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: options[:decrypted_data_filename],
    encode: encode
  )
end

#export_encrypted!(encrypted_data:, decrypter_params:, encode:) ⇒ Hash

Exporting encrypted data and decrypter params

Parameters:

  • encrypted_data (Hash)

    Hash with encrypted data for export

  • decrypter_params (Hash)

    Hash with decrypter params for export

  • encode (TrueClass || FalseClass)

    Enable/disable encoding of exported data

Returns:

  • (Hash)

    merged encrypted data and decrypter params

Since:

  • 0.1.0



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: options[:encrypted_data_filename],
    encode: encode
  )
  params = export_data(
    data: decrypter_params,
    filename: options[:decrypter_params_filename],
    encode: encode
  )

  encrypted && params ? encrypted.merge(params) : nil
end

#generate_ec_keypair(cipher = 'secp521r1') ⇒ Hash

Generating EC keypair

Parameters:

  • cipher (String) (defaults to: 'secp521r1')

    cipher for keys encryption

Returns:

  • (Hash)

    keypair of EC public and private key

Since:

  • 0.1.0



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_keypairHash

Generating RbNaCl keypair

Returns:

  • (Hash)

    keypair of RbNaCl public and private key

Since:

  • 0.1.0



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

Parameters:

  • bit_number (Integer) (defaults to: 4096)

    number of key bits

Returns:

  • (Hash)

    keypair of RSA public and private key

Since:

  • 0.1.0



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

Parameters:

  • data (Hash)

    Hash with data to import from

  • filename (String)

    Name of file to import from

  • delete_imported (TrueClass, FalseClass)

    Enable/disable deleting file after import

  • decode (TrueClass || FalseClass)

    Enable/disable decoding of imported data

Returns:

  • (Hash)

    imported data

Since:

  • 0.1.0



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:)
  function_options = { data: data, decode: decode }
  file_options = {
    filename: filename,
    delete_imported: delete_imported,
    decode: decode
  }
  importer = options[:importer]
  importer_options = importer == Izokatu::FunctionImporter ? function_options : file_options
  importer.call(**importer_options)
end

#import_encrypted!(options:, decode:) ⇒ Hash

Importing encrypted data and decrypter params

Parameters:

  • options (Hash)

    Hash with options

  • decode (TrueClass || FalseClass)

    Enable/disable decoding of imported data

Returns:

  • (Hash)

    Hash with updated options

Since:

  • 0.1.0



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 = options.select { |k, _v| k == :encrypted_data_string }
  decrypter_params_default_keys = %i[nonce key auth_data auth_tag]
  decrypter_params = options.select { |k, _v| decrypter_params_default_keys.include?(k) }
  encrypted = import_data(
    data: encrypted_data,
    filename: options[:encrypted_data_filename],
    delete_imported: options[:delete_imported],
    decode: decode
  )
  params = options.merge! import_data(
    data: decrypter_params,
    filename: options[:decrypter_params_filename],
    delete_imported: options[:delete_imported],
    decode: decode
  )
  options[:encrypted_data_string] = encrypted.values[0]
  options.merge!(params)
end

#import_encrypted_in_options!Object

Performing encryption and merging result in options

Since:

  • 0.1.0



57
58
59
60
61
# File 'lib/izokatu/helpers.rb', line 57

def import_encrypted_in_options!
  encrypted_data, decrypter_params = encrypt(options)
  options.merge!(encrypted_data)
  options.merge!(decrypter_params)
end