Sha256: a9dbba96220640a7b0422fda3b00c905b41381cc2bf738fd25bfd44ef89f1ca2

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# Waxx Copyright (c) 2016 ePark labs Inc. & Daniel J. Fitzpatrick <dan@eparklabs.com> All rights reserved.
# Released under the Apache Version 2 License. See LICENSE.txt.

module Waxx::Encrypt
  extend self
  def encrypt(str, encode:'b64', cipher: Waxx['encryption']['cipher'], key:Waxx['encryption']['key'], iv:Waxx['encryption']['iv'])
    aes = OpenSSL::Cipher.new(cipher)
    aes.encrypt
    aes.key = key
    aes.iv = iv if iv
    case encode.to_sym
      when :b64
        Base64.encode64(aes.update(str.to_s) + aes.final).chomp
      when :url
        Waxx::Http.escape(Base64.encode64(aes.update(str.to_s) + aes.final).chomp)
      when :bin
        aes.update(str.to_s) + aes.final
      else
        throw "Encoding not defined"
    end
  end
  def decrypt(str, encode:'b64', cipher: Waxx['encryption']['cipher'], key:Waxx['encryption']['key'], iv:Waxx['encryption']['iv'])
    aes = OpenSSL::Cipher.new(cipher)
    aes.decrypt
    aes.key = key
    aes.iv = iv if iv
    case encode.to_sym
      when :b64
        aes.update(Base64.decode64(str.to_s + "\n")) + aes.final
      when :url
        aes.update(Base64.decode64(Waxx::Http.unescape(str.to_s) + "\n")) + aes.final
      when :bin
        aes.update(str.to_s) + aes.final
      else
        throw "Encoding not defined"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
waxx-0.2.0 lib/waxx/encrypt.rb
waxx-0.1.4 lib/waxx/encrypt.rb
waxx-0.1.3 lib/waxx/encrypt.rb