class Firebug::Crypto

Class for encrypting and decrypting Pyro cookies.

Public Class Methods

new(key) click to toggle source

@param [String] key

# File lib/firebug/crypto.rb, line 11
def initialize(key)
  @key = Digest::MD5.hexdigest(key)
end

Public Instance Methods

add_noise(data) click to toggle source

Pyro adds “noise” to the results of the encryption by adding the ordinal value of each character with a value in the key. The plaintext key is hashed with MD5 then SHA1.

@param [String] data

# File lib/firebug/crypto.rb, line 43
def add_noise(data)
  noise(data, :+)
end
decrypt(data) click to toggle source

Decrypts data using the Rijndael 256 cipher.

@param [String] data @return [String]

# File lib/firebug/crypto.rb, line 31
def decrypt(data)
  data = remove_noise(data)
  # The first 32 bytes of the data is the original IV
  iv = data[0..31]
  cipher = FirebugMcrypt.new(:rijndael_256, :cbc, @key, iv, :zeros)
  cipher.decrypt(data[32..-1])
end
encrypt(data) click to toggle source

Encrypts data using the Rijndael 256 cipher.

@param [String] data @return [String]

# File lib/firebug/crypto.rb, line 19
def encrypt(data)
  # Create a random 32 byte string to act as the initialization vector.
  iv = SecureRandom.random_bytes(32)
  # Pyro pads the data with zeros
  cipher = FirebugMcrypt.new(:rijndael_256, :cbc, @key, iv, :zeros)
  add_noise(iv + cipher.encrypt(data))
end
remove_noise(data) click to toggle source

The “noise” is removed by subtracting the ordinals.

@see add_noise

@param [String] data @return [String]

# File lib/firebug/crypto.rb, line 53
def remove_noise(data)
  noise(data, :-)
end

Private Instance Methods

noise(data, operator) click to toggle source

@param [String] data @param [Symbol] operator @return [String]

# File lib/firebug/crypto.rb, line 62
def noise(data, operator)
  key = Digest::SHA1.hexdigest(@key)
  Array.new(data.size) { |i| (data[i].ord.send(operator, key[i % key.size].ord) % 256).chr }.join
end