Class: Growthbook::DecryptionUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/growthbook/decryption_util.rb

Overview

Utils for working with encrypted feature payloads.

Class Method Summary collapse

Class Method Details

.decrypt(payload, key:) ⇒ String?

Returns The decrypted payload, or nil if it fails to decrypt.

Returns:

  • (String, nil)

    The decrypted payload, or nil if it fails to decrypt



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/growthbook/decryption_util.rb', line 10

def self.decrypt(payload, key:)
  return nil if payload.nil?
  return nil unless payload.include?('.')

  parts = payload.split('.')
  return nil if parts.length != 2

  iv = parts[0]
  decoded_iv = Base64.strict_decode64(iv)
  decoded_key = Base64.strict_decode64(key)

  cipher_text = parts[1]
  decoded_cipher_text = Base64.strict_decode64(cipher_text)

  cipher = OpenSSL::Cipher.new('aes-128-cbc')

  cipher.decrypt
  cipher.key = decoded_key
  cipher.iv = decoded_iv

  cipher.update(decoded_cipher_text) + cipher.final
rescue StandardError
  nil
end