Sha256: 5643f98dfc2c8b17a78123beb0d9b7c3a2c4b32e325d8605f6e22f58b57703c7

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

require 'aes'
require 'armor'

module CryptKeeper
  module Provider
    class AesNew < Base
      include CryptKeeper::Helper::DigestPassphrase

      # Public: The encryption key
      attr_accessor :key

      # Public: Initializes the class
      #
      #   options - A hash of options. :key and :salt are required
      def initialize(options = {})
        @key = digest_passphrase(options[:key], options[:salt])
      end

      # Public: Encrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES.
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the encrypted string
      #
      # Returns a String
      def encrypt(value)
        AES.encrypt(value, key)
      end

      # Public: Decrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES (and thus cannot be decrypted).
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the decrypted string
      #
      # Returns a String
      def decrypt(value)
        AES.decrypt(value, key)
      end

      # Public: Search for a record
      #
      # record   - An ActiveRecord collection
      # field    - The field to search
      # criteria - A string to search with
      #
      # Returns an Enumerable
      def search(records, field, criteria)
        records.select { |record| record[field] == criteria }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
crypt_keeper-1.1.1 lib/crypt_keeper/provider/aes_new.rb
crypt_keeper-1.1.0 lib/crypt_keeper/provider/aes_new.rb
crypt_keeper-1.0.1 lib/crypt_keeper/provider/aes_new.rb
crypt_keeper-1.0.0 lib/crypt_keeper/provider/aes_new.rb
crypt_keeper-1.0.0.beta1 lib/crypt_keeper/provider/aes_new.rb