Sha256: 53affc40823b233dfbe4c8fe1a450ebd527ff022263c5b9aff12d675edc9a8dc

Contents?: true

Size: 759 Bytes

Versions: 2

Compression:

Stored size: 759 Bytes

Contents

module Passkit
  class UrlEncrypt
    class << self
      def encrypt(payload)
        string = payload.to_json
        cipher = cypher.encrypt
        cipher.key = encryption_key
        s = cipher.update(string) + cipher.final

        s.unpack1("H*").upcase
      end

      def decrypt(string)
        cipher = cypher.decrypt
        cipher.key = encryption_key
        s = [string].pack("H*").unpack("C*").pack("c*")

        JSON.parse(cipher.update(s) + cipher.final, symbolize_names: true)
      end

      private

      def encryption_key
        key = ENV.fetch("PASSKIT_URL_ENCRYPTION_KEY") { Rails.application.secret_key_base }
        key[0..15]
      end

      def cypher
        OpenSSL::Cipher.new("AES-128-CBC")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
passkit-0.7.0 lib/passkit/url_encrypt.rb
passkit-0.6.1 lib/passkit/url_encrypt.rb