Class: CryptKeeper::Provider::ActiveSupport

Inherits:
Base
  • Object
show all
Defined in:
lib/crypt_keeper/provider/active_support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dump, #load

Constructor Details

#initialize(options = {}) ⇒ ActiveSupport

Public: Initializes the encryptor

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

Returns nothing.


13
14
15
16
17
18
19
# File 'lib/crypt_keeper/provider/active_support.rb', line 13

def initialize(options = {})
  key  = options.fetch(:key)
  salt = options.fetch(:salt)

  @encryptor = ::ActiveSupport::MessageEncryptor.new \
    ::ActiveSupport::KeyGenerator.new(key).generate_key(salt, 32)
end

Instance Attribute Details

#encryptorObject (readonly)

Returns the value of attribute encryptor


6
7
8
# File 'lib/crypt_keeper/provider/active_support.rb', line 6

def encryptor
  @encryptor
end

Instance Method Details

#decrypt(value) ⇒ Object

Public: Decrypts a string

value - Cipher text

Returns a plaintext string


35
36
37
# File 'lib/crypt_keeper/provider/active_support.rb', line 35

def decrypt(value)
  encryptor.decrypt_and_verify(value)
end

#encrypt(value) ⇒ Object

Public: Encrypts a string

value - Plaintext value

Returns an encrypted string


26
27
28
# File 'lib/crypt_keeper/provider/active_support.rb', line 26

def encrypt(value)
  encryptor.encrypt_and_sign(value)
end

#search(records, field, criteria) ⇒ Object

Public: Searches the table

records - ActiveRecord::Relation field - Field name to match criteria - Value to match

Returns an Enumerable


46
47
48
# File 'lib/crypt_keeper/provider/active_support.rb', line 46

def search(records, field, criteria)
  records.select { |record| record[field] == criteria }
end