Class: Obfuskit::Obfuscator
- Inherits:
-
Object
- Object
- Obfuskit::Obfuscator
- Defined in:
- lib/obfuskit/obfuscator.rb
Instance Attribute Summary collapse
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
Instance Method Summary collapse
-
#initialize(salt) ⇒ Obfuscator
constructor
Initializes the obfuscator with a string.
-
#obfuscate(value) ⇒ Object
Obfuscates a string.
-
#reveal(value) ⇒ Object
Reverses the obfuscation of an array of bytes.
Constructor Details
#initialize(salt) ⇒ Obfuscator
Initializes the obfuscator with a string. The string is converted to an array of bytes and stored in @c. The size of the array is stored in @l.
13 14 15 16 17 |
# File 'lib/obfuskit/obfuscator.rb', line 13 def initialize(salt) @salt = salt || "" @salt_bytes = (salt.bytes || []) if salt.is_a? String @salt_length = @salt_bytes.size end |
Instance Attribute Details
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
8 9 10 |
# File 'lib/obfuskit/obfuscator.rb', line 8 def salt @salt end |
Instance Method Details
#obfuscate(value) ⇒ Object
Obfuscates a string. The string is converted to an array of bytes and each element is XORed with an element from @c. The index of the element from @c is the index of the element from the string modulo @l.
22 23 24 |
# File 'lib/obfuskit/obfuscator.rb', line 22 def obfuscate(value) value.bytes.map.with_index { |b, i| b ^ @salt_bytes[i % @salt_length] } end |
#reveal(value) ⇒ Object
Reverses the obfuscation of an array of bytes. Each element is XORed with an element from @c and the result is converted back to a string. The index of the element from @c is the index of the element from the array modulo @l.
29 30 31 |
# File 'lib/obfuskit/obfuscator.rb', line 29 def reveal(value) value.map.with_index { |b, i| b ^ @salt_bytes[i % @salt_length] }.pack('C*').force_encoding('utf-8') end |