Sha256: abbe5edc08bce7aa1c6a314dce349bd5858e037453444322d52851050d7fc122

Contents?: true

Size: 986 Bytes

Versions: 2

Compression:

Stored size: 986 Bytes

Contents

require 'openssl'

module Encryption
  class Symmetric
    
    def initialize
      @configuration = Encryption::Configuration::Symmetric.new
    end

    def method_missing(name, *args, &block)
      if @configuration.respond_to? name
        return @configuration.send(name, *args, &block)
      end
      super
    end

    def respond_to?(name)
      return true if @configuration.respond_to? name
      super
    end

    def encrypt(data)
      cipher_init(:cipher, :encrypt)
      @cipher.update(data) + @cipher.final
    end

    def decrypt(data)
      cipher_init(:decipher, :decrypt)
      @decipher.update(data) + @decipher.final
    end

    private

    def cipher_init(name, mode)
      obj = instance_variable_get((name = '@' + name.to_s))
      if obj.nil?
        obj = instance_variable_set(name, OpenSSL::Cipher.new(@configuration.cipher))
        obj.send mode
      end
      
      obj.key = @configuration.key
      obj.iv = @configuration.iv
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
encryption-1.1.4 lib/modules/symmetric.rb
encryption-1.1.3 lib/modules/symmetric.rb