module Izokatu::Helpers

Izokatu helper methods

Constants

EC_CIPHER

Verify cipher is included in OpenSSL public key EC ciphers

KEY_SYMBOL

Default Symbol keys for keys

RBNACL_KEY_CLASSES

Rbnacl classes for keys

Public Instance Methods

decode_data(data) click to toggle source

Decoding data

@param data [Hash] Hash with data for decoding

@return [Hash] decoded data

@since 0.1.0

# 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) click to toggle source

Encoding data

@param data [Hash] Hash with data for encoding

@return [Hash] encoded data

@since 0.1.0

# 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:) click to toggle source

Exporting data

@param data [Hash] Hash with data for export @param filename [String] Name of file for export @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data

@return [Hash] exported data

@note Returning value even if not using :function exporter

@since 0.1.0

# 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:) click to toggle source

Exporting decrypted data

@param decrypted_data [Hash] Hash with encrypted data for export @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data

@return [Hash] decrypted data

@note Returning value even if not using :function exporter

@since 0.1.0

# 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:) click to toggle source

Exporting encrypted data and decrypter params

@param encrypted_data [Hash] Hash with encrypted data for export @param decrypter_params [Hash] Hash with decrypter params for export @param encode [TrueClass || FalseClass] Enable/disable encoding of exported data

@return [Hash] merged encrypted data and decrypter params

@since 0.1.0

# 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') click to toggle source

Generating EC keypair

@param cipher [String] cipher for keys encryption

@return [Hash] keypair of EC public and private key

@since 0.1.0

# 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() click to toggle source

Generating RbNaCl keypair

@return [Hash] keypair of RbNaCl public and private key

@since 0.1.0

# File lib/izokatu/helpers.rb, line 49
def generate_rbnacl_keypair
  Izokatu::Rbnacl::PublicKey::KeysGenerator.call
end
generate_rsa_keypair(bit_number = 4096) click to toggle source

Generating RSA keypair

@param bit_number [Integer] number of key bits

@return [Hash] keypair of RSA public and private key

@since 0.1.0

# 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:) click to toggle source

Importing data

@param data [Hash] Hash with data to import from @param filename [String] Name of file to import from @param delete_imported [TrueClass, FalseClass] Enable/disable deleting file after import @param decode [TrueClass || FalseClass] Enable/disable decoding of imported data

@return [Hash] imported data

@since 0.1.0

# 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:) click to toggle source

Importing encrypted data and decrypter params

@param options [Hash] Hash with options @param decode [TrueClass || FalseClass] Enable/disable decoding of imported data

@return [Hash] Hash with updated options

@since 0.1.0

# 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!() click to toggle source

Performing encryption and merging result in options

@since 0.1.0

# 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