factor/extra/crypto/rc4/rc4.factor

40 lines
727 B
Factor
Raw Normal View History

2007-10-07 00:01:26 -04:00
USING: kernel math sequences namespaces ;
IN: crypto.rc4
2007-09-20 18:09:08 -04:00
! http://en.wikipedia.org/wiki/RC4_%28cipher%29
2007-10-07 00:01:26 -04:00
<PRIVATE
2007-09-20 18:09:08 -04:00
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
2007-10-07 00:01:26 -04:00
dup j get s get exchange drop
] each ;
2007-09-20 18:09:08 -04:00
: generate ( -- n )
i get 1+ 255 bitand i set
j get i get s get nth + 255 bitand j set
i get j get s get exchange
i get s get nth j get s get nth + 255 bitand s get nth ;
2007-10-07 00:01:26 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: rc4 ( key -- )
2007-10-07 00:01:26 -04:00
[
[ key set ] keep
length l set
ksa
0 i set
0 j set
] with-scope ;
2007-09-20 18:09:08 -04:00