]> gitweb.factorcode.org Git - factor.git/commitdiff
crypto -- remove duplicate code
authorerg <erg@trifocus.net>
Thu, 10 Aug 2006 04:27:21 +0000 (04:27 +0000)
committererg <erg@trifocus.net>
Thu, 10 Aug 2006 04:27:21 +0000 (04:27 +0000)
contrib/crypto/common.factor
contrib/crypto/md5.factor
contrib/crypto/sha1.factor

index f9c0ba2f48d8e7130717d87d84461203b08b6d02..dfde878ab2ab5f51e002ede1f74f0d289cacd47c 100644 (file)
@@ -10,23 +10,15 @@ USING: kernel io strings sequences namespaces math parser ;
 : update-old-new ( old new -- )
     [ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline
     
-! calculate pad length.  leave 8 bytes for length after padding
-: zero-pad-length ( length -- pad-length )
-    dup 56 < 55 119 ? swap - ; ! one less for first byte of padding 0x80
+: calculate-pad-length ( length -- pad-length )
+    dup 56 < 55 119 ? swap - ;
 
 ! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
-: pad-string-md5 ( string  -- padded-string )
-    [
+: preprocess-plaintext ( string big-endian? -- padded-string )
+    swap [
         dup % HEX: 80 ,
-        dup length HEX: 3f bitand zero-pad-length 0 <string> %
-        dup length 3 shift 8 >le %
-    ] "" make nip ;
-
-: pad-string-sha1 ( string  -- padded-string )
-    [
-        dup % HEX: 80 ,
-        dup length HEX: 3f bitand zero-pad-length 0 <string> %
-        dup length 3 shift 8 >be %
+        dup length HEX: 3f bitand calculate-pad-length 0 <string> %
+        dup length 3 shift 8 >r rot r> swap [ >be ] [ >le ] if %
     ] "" make nip ;
 
 : num-blocks ( length -- num ) -6 shift ;
index d78e7bb11d10df54a0ce1abfc2f8c468dd295968..f199c27258f2ef62c805364d13a5cfd67b00d2aa 100644 (file)
@@ -156,7 +156,7 @@ SYMBOL: old-d
 IN: crypto
 : string>md5 ( string -- md5 )
     [
-        initialize-md5 pad-string-md5
+        initialize-md5 f preprocess-plaintext
         dup length num-blocks [ 2dup get-block process-md5-block ] repeat
         drop get-md5
     ] with-scope ;
index a3d62d75a2e68403d4575fa744e8611b05bdfe87..76bfd790d00e38ea186a308604e7f1da90f0a18b 100644 (file)
@@ -130,7 +130,7 @@ SYMBOL: K
 IN: crypto
 : string>sha1 ( string -- sha1 )
     [
-        initialize-sha1 pad-string-sha1
+        initialize-sha1 t preprocess-plaintext
         dup length num-blocks [ reset-w 2dup get-block process-sha1-block ] repeat
         drop get-sha1
     ] with-scope ;