Class: Firebug::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/firebug/crypto.rb

Overview

Class for encrypting and decrypting Pyro cookies.

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Crypto



11
12
13
# File 'lib/firebug/crypto.rb', line 11

def initialize(key)
  @key = Digest::MD5.hexdigest(key)
end

Instance Method Details

#add_noise(data) ⇒ Object

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.



43
44
45
# File 'lib/firebug/crypto.rb', line 43

def add_noise(data)
  noise(data, :+)
end

#decrypt(data) ⇒ String

Decrypts data using the Rijndael 256 cipher.



31
32
33
34
35
36
37
# 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) ⇒ String

Encrypts data using the Rijndael 256 cipher.



19
20
21
22
23
24
25
# 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) ⇒ String

The “noise” is removed by subtracting the ordinals.

See Also:



53
54
55
# File 'lib/firebug/crypto.rb', line 53

def remove_noise(data)
  noise(data, :-)
end