class SharedSettings::Utilities::Encryption
Constants
- AES_BLOCK_SIZE
- ENCRYPTION_ALGORITHM
- ENCRYPTION_KEY_SIZE
- INIT_VEC_SIZE
Public Class Methods
generate_aes_key(size = ENCRYPTION_KEY_SIZE)
click to toggle source
# 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
new(key)
click to toggle source
# File lib/shared_settings/utilities/encryption.rb, line 18 def initialize(key) @key = key end
Public Instance Methods
decrypt(init_vec, cipher_text)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
build_cipher(cipher, init_vec)
click to toggle source
# File lib/shared_settings/utilities/encryption.rb, line 47 def build_cipher(cipher, init_vec) cipher.iv = Base16.string_to_bytes(init_vec) cipher.key = Base16.string_to_bytes(@key) cipher.padding = AES_BLOCK_SIZE cipher end
decryption_cipher(init_vec)
click to toggle source
# File lib/shared_settings/utilities/encryption.rb, line 43 def decryption_cipher(init_vec) build_cipher(OpenSSL::Cipher.new(ENCRYPTION_ALGORITHM).decrypt, init_vec) end
encryption_cipher(init_vec)
click to toggle source
# File lib/shared_settings/utilities/encryption.rb, line 39 def encryption_cipher(init_vec) build_cipher(OpenSSL::Cipher.new(ENCRYPTION_ALGORITHM).encrypt, init_vec) end