Sha256: ffff80821da449ff03eb3ac005ba5f4de897a99dfafb0c02497dc4e495ae729e

Contents?: true

Size: 1.76 KB

Versions: 13

Compression:

Stored size: 1.76 KB

Contents

# Toplevel Pubnub module.
module Pubnub
  # Internal Crypto class used for message encryption and decryption
  class Crypto
    def initialize(cipher_key, use_random_iv)
      @alg = 'AES-256-CBC'
      sha256_key = Digest::SHA256.hexdigest(cipher_key.to_s)
      @key = sha256_key.slice(0, 32)
      @using_random_iv = use_random_iv
      @iv = @using_random_iv == true ? random_iv : '0123456789012345'
    end

    def encrypt(message)
      aes = OpenSSL::Cipher.new(@alg)
      aes.encrypt
      aes.key = @key
      aes.iv = @iv

      json_message = message.to_json
      cipher = @using_random_iv == true ? @iv : ''
      cipher << aes.update(json_message)
      cipher << aes.final

      Base64.strict_encode64(cipher)
    end

    def decrypt(cipher_text)
      undecoded_text = Base64.decode64(cipher_text)
      iv = @iv

      if cipher_text.length > 16 && @using_random_iv == true
        iv = undecoded_text.slice!(0..15)
      end

      decode_cipher = OpenSSL::Cipher.new(@alg).decrypt
      decode_cipher.key = @key
      decode_cipher.iv = iv

      plain_text = decryption(undecoded_text, decode_cipher)
      load_json(plain_text)

      Pubnub.logger.debug('Pubnub') { 'Finished decrypting' }

      plain_text
    end

    private

    def decryption(cipher_text, decode_cipher)
      plain_text = decode_cipher.update(cipher_text)
      plain_text << decode_cipher.final
    rescue StandardError => e
      Pubnub.error('Pubnub') { "DECRYPTION ERROR #{e}" }
      '"DECRYPTION ERROR"'
    end

    def load_json(plain_text)
      JSON.load(plain_text)
    rescue JSON::ParserError
      JSON.load("[#{plain_text}]")[0]
    end

    private

    def random_iv
      random_bytes = Random.new.bytes(16).unpack('NnnnnN')
      format('%08x%04x%04x', *random_bytes)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
pubnub-5.2.2 lib/pubnub/crypto.rb
pubnub-5.2.1 lib/pubnub/crypto.rb
pubnub-5.2.0 lib/pubnub/crypto.rb
pubnub-5.1.2 lib/pubnub/crypto.rb
pubnub-5.1.1 lib/pubnub/crypto.rb
pubnub-5.1.0 lib/pubnub/crypto.rb
pubnub-5.0.0 lib/pubnub/crypto.rb
pubnub-4.8.0 lib/pubnub/crypto.rb
pubnub-4.7.1 lib/pubnub/crypto.rb
pubnub-4.7.0 lib/pubnub/crypto.rb
pubnub-4.6.2 lib/pubnub/crypto.rb
pubnub-4.6.1 lib/pubnub/crypto.rb
pubnub-4.6.0 lib/pubnub/crypto.rb