Module: Secrets::CipherHandler

Included in:
Extensions::InstanceMethods
Defined in:
lib/secrets/cipher_handler.rb

Overview

CipherHandler contains cipher-related utilities necessary to create ciphers, and seed them with the salt or iV vector,

Defined Under Namespace

Modules: ClassMethods Classes: CipherStruct

Constant Summary collapse

CREATE_CIPHER =
->(name) { ::OpenSSL::Cipher.new(name) }

Instance Method Summary collapse

Instance Method Details

#create_cipher(direction:, cipher_name:, iv: nil, salt: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/secrets/cipher_handler.rb', line 15

def create_cipher(direction:,
                  cipher_name:,
                  iv: nil,
                  salt: nil)

  cipher = new_cipher(cipher_name)
  cipher.send(direction)
  iv        ||= cipher.random_iv
  cipher.iv = iv
  CipherStruct.new(cipher, iv, salt)
end

#new_cipher(cipher_name) ⇒ Object



27
28
29
# File 'lib/secrets/cipher_handler.rb', line 27

def new_cipher(cipher_name)
  CREATE_CIPHER.call(cipher_name)
end

#update_cipher(cipher, value) ⇒ Object



31
32
33
34
35
# File 'lib/secrets/cipher_handler.rb', line 31

def update_cipher(cipher, value)
  data = cipher.update(value)
  data << cipher.final
  data
end