Class: RC4
- Inherits:
-
Object
- Object
- RC4
- Defined in:
- lib/rc4.rb
Instance Method Summary collapse
- #encrypt(text) ⇒ Object (also: #decrypt)
- #encrypt!(text) ⇒ Object
-
#initialize(str) ⇒ RC4
constructor
A new instance of RC4.
Constructor Details
#initialize(str) ⇒ RC4
Returns a new instance of RC4.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rc4.rb', line 3 def initialize(str) begin raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('') @q1, @q2 = 0, 0 @key = [] str.each_byte {|elem| @key << elem} while @key.size < 256 @key.slice!(256..@key.size-1) if @key.size >= 256 @s = (0..255).to_a j = 0 0.upto(255) do |i| j = (j + @s[i] + @key[i] )%256 @s[i], @s[j] = @s[j], @s[i] end end end |
Instance Method Details
#encrypt(text) ⇒ Object Also known as: decrypt
24 25 26 |
# File 'lib/rc4.rb', line 24 def encrypt(text) process text.dup end |
#encrypt!(text) ⇒ Object
20 21 22 |
# File 'lib/rc4.rb', line 20 def encrypt!(text) process text end |