Class: Pubnub::Crypto::CryptoModule

Inherits:
CryptoProvider show all
Defined in:
lib/pubnub/modules/crypto/crypto_module.rb

Overview

Crypto module for data processing.

The PubNub client uses a module to encrypt and decrypt sent data in a way that's compatible with previous versions (if additional cryptors have been registered).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default, cryptors) ⇒ CryptoModule

Create crypto module.

Parameters:

  • default (Cryptor)

    Default cryptor used to encrypt and decrypt data.

  • cryptors (Array<Cryptor>, nil)

    Additional cryptors which will be used to decrypt data encrypted by previously used cryptors.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pubnub/modules/crypto/crypto_module.rb', line 60

def initialize(default, cryptors)
  if default.nil?
    raise ArgumentError, {
      message: '\'default\' cryptor required for data encryption.'
    }
  end

  @default = default
  @cryptors = cryptors&.each_with_object({}) do |value, hash|
    hash[value.identifier] = value
  end || {}
  super()
end

Class Method Details

.new_aes_cbc(cipher_key, use_random_iv) ⇒ Object

AES-CBC cryptor based module.

Data encryption and decryption will be done by default using the AesCbcCryptor. In addition to the AesCbcCryptor for data decryption, the LegacyCryptor will be registered for backward-compatibility.

Parameters:

  • cipher_key (String)

    Key for data encryption and decryption.

  • use_random_iv (Boolean)

    Whether random IV should be used for data decryption.

Raises:



21
22
23
24
25
26
27
28
29
# File 'lib/pubnub/modules/crypto/crypto_module.rb', line 21

def self.new_aes_cbc(cipher_key, use_random_iv)
  if cipher_key.nil? || cipher_key.empty?
    raise ArgumentError, {
      message: '\'cipher_key\' is missing or empty.'
    }
  end

  CryptoModule.new AesCbcCryptor.new(cipher_key), [LegacyCryptor.new(cipher_key, use_random_iv)]
end

.new_legacy(cipher_key, use_random_iv) ⇒ Object

Legacy AES-CBC cryptor based module.

Data encryption and decryption will be done by default using the LegacyCrypto. In addition to the LegacyCrypto for data decryption, the AesCbcCryptor will be registered for future-compatibility (which will help with gradual application updates).

Parameters:

  • cipher_key (String)

    Key for data encryption and decryption.

  • use_random_iv (Boolean)

    Whether random IV should be used for data decryption.

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/pubnub/modules/crypto/crypto_module.rb', line 44

def self.new_legacy(cipher_key, use_random_iv)
  if cipher_key.nil? || cipher_key.empty?
    raise ArgumentError, {
      message: '\'cipher_key\' is missing or empty.'
    }
  end

  CryptoModule.new LegacyCryptor.new(cipher_key, use_random_iv), [AesCbcCryptor.new(cipher_key)]
end

Instance Method Details

#decrypt(data) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pubnub/modules/crypto/crypto_module.rb', line 84

def decrypt(data)
  if data.nil? || data.empty?
    puts 'Pubnub :: DECRYPTION ERROR: Empty data for decryption'
    return nil
  end

  header = Crypto::CryptorHeader.parse(data)
  return nil if header.nil?

  cryptor_identifier = header.identifier || '\x00\x00\x00\x00'
  cryptor = cryptor cryptor_identifier

  # Check whether there is a cryptor to decrypt data or not.
  if cryptor.nil?
    identifier = header.identifier || 'UNKN'
    raise UnknownCryptorError, {
      message: "Decrypting data created by unknown cryptor. Please make sure to register
#{identifier} or update SDK."
    }
  end

  encrypted_data = data[header.length..-1]
   =  encrypted_data, header.data_size

  # Check whether there is still some data for processing or not.
  return nil if encrypted_data.nil? || encrypted_data.empty?

  cryptor.decrypt(EncryptedData.new(encrypted_data, ))
end

#encrypt(data) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/pubnub/modules/crypto/crypto_module.rb', line 74

def encrypt(data)
  # Encrypting provided data.
  encrypted_data = default_cryptor.encrypt(data)
  return nil if encrypted_data.nil?

  payload = Crypto::CryptorHeader.new(default_cryptor.identifier, encrypted_data.).to_s
  payload << encrypted_data. unless encrypted_data..nil?
  payload << encrypted_data.data
end