Parent

Class/Module Index [+]

Quicksearch

Origami::Encryption::ARC4

Pure Ruby implementation of the aRC4 symmetric algorithm

Public Class Methods

decrypt(key, data) click to toggle source

Decrypts data using the given key

# File lib/origami/encryption.rb, line 563
def ARC4.decrypt(key, data)
  ARC4.new(key).decrypt(data)
end
encrypt(key, data) click to toggle source

Encrypts data using the given key

# File lib/origami/encryption.rb, line 556
def ARC4.encrypt(key, data)
  ARC4.new(key).encrypt(data)
end
new(key) click to toggle source

Creates and initialises a new aRC4 generator using given key

# File lib/origami/encryption.rb, line 570
def initialize(key)
  if Origami::OPTIONS[:use_openssl]
    @key = key
  else
    @state = init(key)
  end
end

Public Instance Methods

cipher(data) click to toggle source

Encrypt/decrypt data with the aRC4 encryption algorithm

# File lib/origami/encryption.rb, line 581
def cipher(data)
  return "" if data.empty?

  if Origami::OPTIONS[:use_openssl]
    rc4 = OpenSSL::Cipher::RC4.new.encrypt
    rc4.key_len = @key.length
    rc4.key = @key

    output = rc4.update(data) << rc4.final
  else
    output = ""
    i, j = 0, 0
    data.each_byte do |byte|
      i = i.succ & 0xFF
      j = (j + @state[i]) & 0xFF
      
      @state[i], @state[j] = @state[j], @state[i]
      
      output << (@state[@state[i] + @state[j] & 0xFF] ^ byte).chr
    end
  end

  output
end
Also aliased as: encrypt, decrypt
decrypt(data) click to toggle source
Alias for: cipher
encrypt(data) click to toggle source
Alias for: cipher

[Validate]

Generated with the Darkfish Rdoc Generator 2.