Class: Firebug::Crypto
- Inherits:
-
Object
- Object
- Firebug::Crypto
- Defined in:
- lib/firebug/crypto.rb
Instance Method Summary collapse
-
#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.
- #decrypt(data) ⇒ String
- #encrypt(data) ⇒ String
-
#initialize(key) ⇒ Crypto
constructor
A new instance of Crypto.
-
#remove_noise(data) ⇒ String
The “noise” is removed by subtracting the ordinals.
Constructor Details
#initialize(key) ⇒ Crypto
Returns a new instance of Crypto.
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.
38 39 40 |
# File 'lib/firebug/crypto.rb', line 38 def add_noise(data) noise(data, :+) end |
#decrypt(data) ⇒ 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
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.
46 47 48 |
# File 'lib/firebug/crypto.rb', line 46 def remove_noise(data) noise(data, :-) end |