Implementation of the “ARCFOUR” algorithm (“alleged RC4 (tm)”). Implemented as described at: www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt
“RC4” is a trademark of RSA Data Security, Inc.
Copyright August 2009, Brad Ediger. All Rights Reserved.
This is free software. Please see the LICENSE and COPYING files for details.
# File lib/prawn/arcfour.rb, line 12
12: def initialize(key)
13: # Convert string key to Array of integers
14: key = key.unpack('c*') if key.is_a?(String)
15:
16: # 1. Allocate an 256 element array of 8 bit bytes to be used as an S-box
17: # 2. Initialize the S-box. Fill each entry first with it's index
18: @sbox = (0..255).to_a
19:
20: # 3. Fill another array of the same size (256) with the key, repeating
21: # bytes as necessary.
22: s2 = []
23: while s2.length < 256
24: s2 += key
25: end
26: s2 = s2[0, 256]
27:
28: # 4. Set j to zero and initialize the S-box
29: j = 0
30: (0..255).each do |i|
31: j = (j + @sbox[i] + s2[i]) % 256
32: @sbox[i], @sbox[j] = @sbox[j], @sbox[i]
33: end
34:
35: @i = @j = 0
36: end
Produces the next byte of key material in the stream (3.2 Stream Generation)
# File lib/prawn/arcfour.rb, line 45
45: def key_byte
46: @i = (@i + 1) % 256
47: @j = (@j + @sbox[@i]) % 256
48: @sbox[@i], @sbox[@j] = @sbox[@j], @sbox[@i]
49: @sbox[(@sbox[@i] + @sbox[@j]) % 256]
50: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.