Sha256: 1738af3dead66025c28ea11bcba8c00f3924d10453c18e55bf8cac571272b7d9

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# https://github.com/benolee/ruby-rc4.git
# == RC4
#
# RC4 is a pure Ruby implementation of the Rc4 algorithm.
#
#  == Usage
#
# First require the gem:
#
#    require 'rc4'


class RC4

  def initialize(str)     #the key
    raise SyntaxError, "RC4: Key supplied is blank"  if str.eql?('')
    @str = str
  end

  def encrypt(text)
    process text
  end

  def decrypt(text)
    process text
  end

  private

  def reset
    @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

  def process(text)
    reset
    out = ""
    0.upto(text.length-1) do |i|
      if RUBY_VERSION >= "1.9.0"
        char = text[i].ord
      else
        char = text[i]
      end
      out += (char ^ round).chr
    end
    out
  end

  def round
    @q1 = (@q1 + 1)%256
    @q2 = (@q2 + @s[@q1])%256
    @s[@q1], @s[@q2] = @s[@q2], @s[@q1]
    @s[(@s[@q1]+@s[@q2])%256]
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_order_groove-3.0.0 lib/rc4.rb