Class: SharedSettings::Utilities::Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/shared_settings/utilities/encryption.rb

Constant Summary collapse

INIT_VEC_SIZE =
16
AES_BLOCK_SIZE =
16
ENCRYPTION_KEY_SIZE =
32
ENCRYPTION_ALGORITHM =
'AES-256-CBC'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Encryption

Returns a new instance of Encryption.



18
19
20
# File 'lib/shared_settings/utilities/encryption.rb', line 18

def initialize(key)
  @key = key
end

Class Method Details

.generate_aes_key(size = ENCRYPTION_KEY_SIZE) ⇒ Object



13
14
15
16
# File 'lib/shared_settings/utilities/encryption.rb', line 13

def self.generate_aes_key(size = ENCRYPTION_KEY_SIZE)
  # .upcase is to match the Elixir implementation
  SecureRandom.hex(size).upcase
end

Instance Method Details

#decrypt(init_vec, cipher_text) ⇒ Object



30
31
32
33
34
35
# File 'lib/shared_settings/utilities/encryption.rb', line 30

def decrypt(init_vec, cipher_text)
  cipher_data = Base16.string_to_bytes(cipher_text)
  cipher_instance = decryption_cipher(init_vec)

  cipher_instance.update(cipher_data) + cipher_instance.final
end

#encrypt(clear_text) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/shared_settings/utilities/encryption.rb', line 22

def encrypt(clear_text)
  init_vec = Encryption.generate_aes_key(INIT_VEC_SIZE)
  cipher_instance = encryption_cipher(init_vec)
  encrypted_data = cipher_instance.update(clear_text) + cipher_instance.final

  [init_vec, Base16.bytes_to_string(encrypted_data)]
end