factor/libs/crypto/rc4.factor

37 lines
693 B
Factor
Raw Permalink Normal View History

2006-01-30 03:25:03 -05:00
USING: kernel math sequences namespaces math-contrib ;
IN: crypto-internals
! http://en.wikipedia.org/wiki/RC4_%28cipher%29
SYMBOL: i
SYMBOL: j
SYMBOL: s
SYMBOL: key
SYMBOL: l
! key scheduling algorithm, initialize s
: ksa ( -- )
256 [ ] map s set
0 j set
256 [
dup s get nth j get + over l get mod key get nth + 255 bitand j set
2006-09-07 16:15:41 -04:00
dup j get s get exchange
2006-01-30 03:25:03 -05:00
] repeat ;
: generate ( -- n )
i get 1+ 255 bitand i set
j get i get s get nth + 255 bitand j set
2006-09-07 16:15:41 -04:00
i get j get s get exchange
2006-01-30 03:25:03 -05:00
i get s get nth j get s get nth + 255 bitand s get nth ;
IN: crypto
: rc4 ( key -- )
[ key set ] keep
length l set
ksa
0 i set
0 j set ;