Sha256: 3a95250bc2dba0ab7103c80f7f2c0cb15d254d3b39057fe4152b2834975cd652

Contents?: true

Size: 942 Bytes

Versions: 2

Compression:

Stored size: 942 Bytes

Contents

# frozen_string_literal: true

require 'openssl'

class EncryptionUtils
  BLOCK_SIZE = 16
  KEY_SIZE = 32

  def self.encrypt(text, cipher_key)
    cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).encrypt
    cipher.padding = 0

    if text.size % BLOCK_SIZE != 0
      logger = Logger.new(STDOUT)
      logger.level = Logger::WARN
      logger.fatal('data not multiple of block length')
      return nil
    end

    cipher_key = Digest::SHA1.hexdigest cipher_key
    cipher.key = cipher_key.slice(0, BLOCK_SIZE)
    s = cipher.update(text) + cipher.final

    s.unpack('H*')[0].upcase
  end

  def self.decrypt(encrypted, cipher_key)
    cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).decrypt
    cipher.padding = 0

    cipher_key = Digest::SHA1.hexdigest cipher_key
    cipher.key = cipher_key.slice(0, BLOCK_SIZE)
    s = [encrypted].pack('H*').unpack('C*').pack('c*')

    rv = cipher.update(s) + cipher.final
    rv.strip
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
securenative-0.1.17 lib/securenative/utils/encryption_utils.rb
securenative-0.1.16 lib/securenative/utils/encryption_utils.rb