Class: CryptKeeperProviders::MysqlAes

Inherits:
Object
  • Object
show all
Defined in:
lib/crypt_keeper_providers/mysql_aes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MysqlAes

Public: Initializes the encryptor

options - A hash, :key is required


11
12
13
14
15
# File 'lib/crypt_keeper_providers/mysql_aes.rb', line 11

def initialize(options = {})
  @key = options.fetch(:key) do
    raise ArgumentError, "Missing :key"
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/crypt_keeper_providers/mysql_aes.rb', line 6

def key
  @key
end

Instance Method Details

#decrypt(value) ⇒ Object

Public: Decrypts a string

Returns a plaintext string



29
30
31
32
33
# File 'lib/crypt_keeper_providers/mysql_aes.rb', line 29

def decrypt(value)
  return if value.nil?
  escape_and_execute_sql(
    ["SELECT AES_DECRYPT(?, ?)", Base64.decode64(value), key]).first
end

#encrypt(value) ⇒ Object

Public: Encrypts a string

Returns an encrypted string



20
21
22
23
24
# File 'lib/crypt_keeper_providers/mysql_aes.rb', line 20

def encrypt(value)
  return if value.nil?
  Base64.encode64 escape_and_execute_sql(
    ["SELECT AES_ENCRYPT(?, ?)", value, key]).first
end