Module: Waxx::Encrypt

Extended by:
Encrypt
Included in:
Encrypt
Defined in:
waxx/encrypt.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#decrypt(str, encode: 'b64', cipher: Waxx['encryption']['cipher'], key: Waxx['encryption']['key'], iv: Waxx['encryption']['iv']) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'waxx/encrypt.rb', line 22

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

#encrypt(str, encode: 'b64', cipher: Waxx['encryption']['cipher'], key: Waxx['encryption']['key'], iv: Waxx['encryption']['iv']) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'waxx/encrypt.rb', line 6

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