Class: Pubnub::Crypto
Instance Method Summary collapse
- #decrypt(cipher_text) ⇒ Object
- #encrypt(message) ⇒ Object
-
#initialize(cipher_key) ⇒ Crypto
constructor
A new instance of Crypto.
Constructor Details
#initialize(cipher_key) ⇒ Crypto
Returns a new instance of Crypto.
3 4 5 6 7 8 9 |
# File 'lib/pubnub/crypto.rb', line 3 def initialize(cipher_key) @alg = 'AES-256-CBC' sha256_key = Digest::SHA256.hexdigest(cipher_key) @key = sha256_key.slice(0,32) @iv = '0123456789012345' end |
Instance Method Details
#decrypt(cipher_text) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pubnub/crypto.rb', line 28 def decrypt(cipher_text) decode_cipher = OpenSSL::Cipher::Cipher.new(@alg) decode_cipher.decrypt decode_cipher.key = @key decode_cipher.iv = @iv plain_text = '' begin undecoded_text = Base64.decode64(cipher_text) plain_text = decode_cipher.update(undecoded_text) plain_text << decode_cipher.final rescue => e return "DECRYPTION_ERROR #{e}" end begin JSON.load(plain_text) rescue JSON::ParserError JSON.load("[#{plain_text}]")[0] # srsly ruby? srsly? rescue return 'PARSE DECRYPTION MESSAGE ERROR' end end |
#encrypt(message) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pubnub/crypto.rb', line 12 def encrypt() aes = OpenSSL::Cipher::Cipher.new(@alg) aes.encrypt aes.key = @key aes.iv = @iv = .to_json cipher = aes.update() cipher << aes.final Base64.strict_encode64(cipher) end |