]> gitweb.factorcode.org Git - factor.git/blob - libs/crypto/rc4.factor
24f523189f22e7cc591f164cbe17ee33b441f64a
[factor.git] / libs / crypto / rc4.factor
1 USING: kernel math sequences namespaces math-contrib ;
2 IN: crypto-internals
3
4 ! http://en.wikipedia.org/wiki/RC4_%28cipher%29
5
6 SYMBOL: i
7 SYMBOL: j
8 SYMBOL: s
9 SYMBOL: key
10 SYMBOL: l
11
12
13 ! key scheduling algorithm, initialize s
14 : ksa ( -- )
15     256 [ ] map s set
16     0 j set
17     256 [
18         dup s get nth j get + over l get mod key get nth + 255 bitand j set
19         dup j get s get exchange
20     ] repeat ;
21
22 : generate ( -- n )
23     i get 1+ 255 bitand i set
24     j get i get s get nth + 255 bitand j set
25     i get j get s get exchange
26     i get s get nth j get s get nth + 255 bitand s get nth ;
27
28 IN: crypto
29
30 : rc4 ( key -- )
31     [ key set ] keep
32     length l set
33     ksa
34     0 i set
35     0 j set ;
36