Class: Izokatu::Rbnacl::PrivateKey::Encrypter

Inherits:
Encrypter show all
Defined in:
lib/izokatu/rbnacl/private_key/encrypter.rb

Overview

RbNaCl private key encrypter

Constant Summary

Constants inherited from Encrypter

Encrypter::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Encrypter

#nonce

Attributes inherited from Encrypter

#clear_data, #encrypted_data, #encrypter

Instance Method Summary collapse

Methods inherited from Encrypter

#generate_nonce!

Methods inherited from Encrypter

#import_clear_data!, #perform

Methods included from Callable

#call

Constructor Details

#initialize(clear_data:, auth_data:) ⇒ Encrypter

Initializing option for encryption

Parameters:

Since:

  • 0.1.0



20
21
22
23
24
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 20

def initialize(clear_data:, auth_data:)
  generate_key!
  super(clear_data: clear_data)
  @auth_data = auth_data
end

Instance Attribute Details

#auth_dataString (readonly)

Returns authenticated data.

Returns:

  • (String)

    authenticated data



11
12
13
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 11

def auth_data
  @auth_data
end

#keyString (readonly)

Returns key for private key encryption/decryption.

Returns:

  • (String)

    key for private key encryption/decryption



9
10
11
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 9

def key
  @key
end

Instance Method Details

#create_encrypter!Object

Generating encrypter instance from key

Since:

  • 0.1.0



38
39
40
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 38

def create_encrypter!
  @encrypter = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key)
end

#decrypter_paramsHash

Returning decrypter params

Returns:

  • (Hash)

    decrypter params

Since:

  • 0.1.1



61
62
63
64
65
66
67
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 61

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

#encrypt_data!Array

Encrypting data

Returns:

  • (Array)

    encrypted data with decrypter params

Since:

  • 0.1.0



48
49
50
51
52
53
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 48

def encrypt_data!
  [
    { encrypted_data_string: encrypter.encrypt(nonce, clear_data, auth_data) },
    decrypter_params
  ]
end

#generate_key!Object

Generating key

Since:

  • 0.1.0



30
31
32
# File 'lib/izokatu/rbnacl/private_key/encrypter.rb', line 30

def generate_key!
  @key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
end