]> gitweb.factorcode.org Git - factor.git/commitdiff
Added rc4
authorDoug Coleman <erg@trifocus.net>
Mon, 30 Jan 2006 08:25:03 +0000 (08:25 +0000)
committerDoug Coleman <erg@trifocus.net>
Mon, 30 Jan 2006 08:25:03 +0000 (08:25 +0000)
contrib/crypto/load.factor
contrib/crypto/rc4.factor [new file with mode: 0644]

index be2a25e20b519ee09c27e87e5036b1a9dac44045..fb605cf05c029dc4acf6b969ac6bbaf4023b6944 100644 (file)
@@ -10,4 +10,5 @@ USING: kernel parser sequences words compiler ;
     "md5"
     "sha1"
     "rsa"
+    "rc4"
 } [ "/contrib/crypto/" swap ".factor" append3 run-resource ] each
diff --git a/contrib/crypto/rc4.factor b/contrib/crypto/rc4.factor
new file mode 100644 (file)
index 0000000..993f480
--- /dev/null
@@ -0,0 +1,43 @@
+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
+
+
+: swap-ij ( i j seq -- )
+    [
+        s set j set i set
+        i get s get nth j get s get nth i get s get set-nth j get s get set-nth
+    ] with-scope ;
+
+! 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
+        dup j get s get swap-ij
+    ] repeat ;
+
+: 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 swap-ij
+    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 ;
+