Class: CryptKeeper::Provider::MysqlAesNew

Inherits:
Base
  • Object
show all
Includes:
Helper::DigestPassphrase, Helper::SQL
Defined in:
lib/crypt_keeper/provider/mysql_aes_new.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::DigestPassphrase

#digest_passphrase

Methods inherited from Base

#dump, #load

Constructor Details

#initialize(options = {}) ⇒ MysqlAesNew

Public: Initializes the encryptor

options - A hash, :key and :salt are required

14
15
16
17
# File 'lib/crypt_keeper/provider/mysql_aes_new.rb', line 14

def initialize(options = {})
  ::ActiveSupport.run_load_hooks(:crypt_keeper_mysql_aes_log, self)
  @key = digest_passphrase(options[:key], options[:salt])
end

Instance Attribute Details

#keyObject

Returns the value of attribute key


9
10
11
# File 'lib/crypt_keeper/provider/mysql_aes_new.rb', line 9

def key
  @key
end

Instance Method Details

#decrypt(value) ⇒ Object

Public: Decrypts a string

Returns a plaintext string


30
31
32
33
# File 'lib/crypt_keeper/provider/mysql_aes_new.rb', line 30

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

#encrypt(value) ⇒ Object

Public: Encrypts a string

Returns an encrypted string


22
23
24
25
# File 'lib/crypt_keeper/provider/mysql_aes_new.rb', line 22

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

#search(records, field, criteria) ⇒ Object

Public: Searches the table

Returns an Enumerable


38
39
40
# File 'lib/crypt_keeper/provider/mysql_aes_new.rb', line 38

def search(records, field, criteria)
  records.where("#{field} = ?", encrypt(criteria))
end