Sha256: f713230c0cab6494e5f06611c027a1e1257c9a36ede05630304e4ef5563fa2f4
Contents?: true
Size: 1.13 KB
Versions: 4
Compression:
Stored size: 1.13 KB
Contents
module Pubnub class Crypto def initialize(cipher_key) @alg = 'AES-256-CBC' sha256_key = Digest::SHA256.hexdigest(cipher_key) @key = sha256_key.slice(0,32) @iv = '0123456789012345' end def encrypt(message) aes = OpenSSL::Cipher::Cipher.new(@alg) aes.encrypt aes.key = @key aes.iv = @iv json_message = message.to_json cipher = aes.update(json_message) cipher << aes.final Base64.strict_encode64(cipher) end 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 end end
Version data entries
4 entries across 4 versions & 2 rubygems
Version | Path |
---|---|
pubnub-ruby-3.4.1 | lib/pubnub/crypto.rb |
pubnub-3.4.1 | lib/pubnub/crypto.rb |
pubnub-ruby-3.4 | lib/pubnub/crypto.rb |
pubnub-3.4 | lib/pubnub/crypto.rb |