Sha256: 987da22c7e9d3fccd5b51ee505fbd18f39143c064dcb29abe82c242f8a6e56d3
Contents?: true
Size: 1.83 KB
Versions: 1
Compression:
Stored size: 1.83 KB
Contents
require 'digest/sha2' require 'openssl' require 'base64' module CryptKeeper module Provider class Aes # A value to split the iv and cipher text with SEPARATOR = ":crypt_keeper:" # Public: The encryption key attr_accessor :key # Public: An instance of OpenSSL::Cipher::Cipher attr_accessor :aes # Public: Initializes the class # # options - A hash of options. :key is required def initialize(options = {}) @aes = ::OpenSSL::Cipher::Cipher.new("AES-256-CBC") @aes.padding = 1 key = options.fetch(:key) do raise ArgumentError, "Missing :key" end @key = Digest::SHA256.digest(key) 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 def encrypt(value) aes.encrypt aes.key = key Base64::encode64("#{aes.random_iv}#{SEPARATOR}#{aes.update(value.to_s) + aes.final}") 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 def decrypt(value) iv, value = Base64::decode64(value.to_s).split(SEPARATOR) aes.decrypt aes.key = key aes.iv = iv aes.update(value) + aes.final end # Public: Search for a record # # record - An ActiveRecord collection # field - The field to search # criteria - A string to search with def search(records, field, criteria) records.select { |record| record[field] == criteria } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
crypt_keeper-0.15.0.pre | lib/crypt_keeper/provider/aes.rb |