Class: PubnubCrypto
- Inherits:
-
Object
- Object
- PubnubCrypto
- Defined in:
- lib/pubnub_crypto.rb
Instance Method Summary collapse
- #decrypt(cipher_text) ⇒ Object
- #encrypt(message) ⇒ Object
-
#initialize(cipher_key) ⇒ PubnubCrypto
constructor
A new instance of PubnubCrypto.
Constructor Details
#initialize(cipher_key) ⇒ PubnubCrypto
Returns a new instance of PubnubCrypto.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/pubnub_crypto.rb', line 4 def initialize(cipher_key) @alg = "AES-256-CBC" sha256_key = Digest::SHA256.hexdigest(cipher_key) @key = sha256_key.slice(0,32) #puts("\nraw sha cipher_key is: #{cipher_key}") #puts("raw sha cipher_key is: #{sha256_key}") #puts("padded cipher_key is: #{@key}\n") @iv = '0123456789012345' end |
Instance Method Details
#decrypt(cipher_text) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pubnub_crypto.rb', line 33 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" end return Yajl.load(plain_text) end |
#encrypt(message) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pubnub_crypto.rb', line 17 def encrypt() aes = OpenSSL::Cipher::Cipher.new(@alg) aes.encrypt aes.key = @key aes.iv = @iv = Yajl.dump() cipher = aes.update() cipher << aes.final Base64.strict_encode64(cipher) end |