Class: Firebug::Crypto

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

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Crypto

Returns a new instance of Crypto.

Parameters:

  • key (String)


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

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

Instance Method Details

#add_noise(data) ⇒ Object

CodeIgniter 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.

Parameters:

  • data (String)


38
39
40
# File 'lib/firebug/crypto.rb', line 38

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

#decrypt(data) ⇒ String

Parameters:

  • data (String)

Returns:

  • (String)


26
27
28
29
30
31
32
# File 'lib/firebug/crypto.rb', line 26

def decrypt(data)
  data = remove_noise(data)
  # The first 32 bytes of the data is the original IV
  iv = data[0..31]
  cipher = Mcrypt.new(:rijndael_256, :cbc, @key, iv, :zeros)
  cipher.decrypt(data[32..-1])
end

#encrypt(data) ⇒ String

Parameters:

  • data (String)

Returns:

  • (String)


16
17
18
19
20
21
22
# File 'lib/firebug/crypto.rb', line 16

def encrypt(data)
  # Create a random 32 byte string to act as the initialization vector.
  iv = SecureRandom.random_bytes(32)
  # CodeIgniter pads the data with zeros
  cipher = Mcrypt.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.

Parameters:

  • data (String)

Returns:

  • (String)


46
47
48
# File 'lib/firebug/crypto.rb', line 46

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