Class: Izokatu::Openssl::PrivateKey::Default::Encrypter

Inherits:
Encrypter
  • Object
show all
Defined in:
lib/izokatu/openssl/private_key/default/encrypter.rb

Overview

OpenSSL private key encrypter for non-authenticated ciphers

Direct Known Subclasses

Auth::Encrypter

Constant Summary collapse

DEFAULT_OPTIONS =

Default Openssl::PrivateKey::Default::Encrypter option

{
  cipher: 'AES256'
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Encrypter

#clear_data, #encrypted_data, #encrypter

Instance Method Summary collapse

Methods inherited from Encrypter

#import_clear_data!, #perform

Methods included from Callable

#call

Constructor Details

#initialize(clear_data:, cipher:) ⇒ Encrypter

Initializing options for OpenSSL EC encryption

Parameters:

Since:

  • 0.1.0



28
29
30
31
32
33
34
35
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 28

def initialize(clear_data:, cipher:)
  super(clear_data: clear_data)
  @cipher = cipher || DEFAULT_OPTIONS[:cipher]
  create_encrypter!
  @key = encrypter.random_key
  @nonce = encrypter.random_iv
  initialize_encrypter_params!
end

Instance Attribute Details

#cipherString (readonly)

Returns OpenSSL private key cipher.

Returns:

  • (String)

    OpenSSL private key cipher



10
11
12
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 10

def cipher
  @cipher
end

#keyString (readonly)

Returns key for private key encryption/decryption.

Returns:

  • (String)

    key for private key encryption/decryption



12
13
14
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 12

def key
  @key
end

#nonceString (readonly)

Returns initialization vector for one-time use.

Returns:

  • (String)

    initialization vector for one-time use



14
15
16
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 14

def nonce
  @nonce
end

Instance Method Details

#create_encrypter!OpenSSL::Cipher (private)

Initializing encrypter

Returns:

  • (OpenSSL::Cipher)

    encrypter instance

Since:

  • 0.1.0



45
46
47
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 45

def create_encrypter!
  @encrypter = OpenSSL::Cipher.new(cipher).encrypt
end

#decrypter_paramsHash (private)

Returning decrypter params

Returns:

  • (Hash)

    decrypter params

Since:

  • 0.1.1



78
79
80
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 78

def decrypter_params
  { nonce: nonce, key: key }
end

#encrypt_data!Array (private)

Encrypting data

Returns:

  • (Array)

    encrypted data with decrypter params

Since:

  • 0.1.0



65
66
67
68
69
70
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 65

def encrypt_data!
  [
    { encrypted_data_string: encrypter.update(clear_data) + encrypter.final },
    decrypter_params
  ]
end

#initialize_encrypter_params!Object (private)

Initializing encrypter params

Since:

  • 0.1.0



53
54
55
56
57
# File 'lib/izokatu/openssl/private_key/default/encrypter.rb', line 53

def initialize_encrypter_params!
  # OpenSSL::Cipher instances has only key=, iv= and auth_data= methods
  encrypter.key = key
  encrypter.iv = nonce
end