Sha256: 40672dea5f9b0ba9fd245fe87c9dfccbf3c0cc11fdf078ced2651ebfe4706325

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Unidom::Common::Concerns::Aes256Cryptor

  extend ActiveSupport::Concern

  included do |includer|

    def encrypt(message, key: nil)

      raise ArgumentError.new('The message argument is required.') if message.blank?
      raise ArgumentError.new('The key argument is required.')     if key.blank?

      cipher = OpenSSL::Cipher::AES.new(256, 'CBC')
      cipher.encrypt
      cipher.padding = aes_256_padding
      cipher.key     = key

      cipher.update(message)+cipher.final

    end

    def decrypt(encoded, key: nil)

      raise ArgumentError.new('The encoded argument is required.') if encoded.blank?
      raise ArgumentError.new('The key argument is required.')     if key.blank?

      cipher = OpenSSL::Cipher::AES.new(256, 'CBC')
      cipher.decrypt
      cipher.padding = aes_256_padding
      cipher.key     = key

      cipher.update(encoded)+cipher.final

    end

    def aes_256_padding
      respond_to?(:cryption_padding) ? cryption_padding : 9
    end

    def hex_encrypt(message, key: nil)
      Unidom::Common::Numeration.hex encrypt(message, key: key)
    end

    def hex_decrypt(encoded, key: nil)
      Unidom::Common::Numeration.hex decrypt(Unidom::Common::Numeration.rev_hex(encoded), key: key)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unidom-common-1.2 app/models/unidom/common/concerns/aes256_cryptor.rb
unidom-common-1.1 app/models/unidom/common/concerns/aes256_cryptor.rb