]> gitweb.factorcode.org Git - factor.git/commitdiff
working on hmac
authorDoug Coleman <erg@jobim.local>
Sun, 17 May 2009 17:45:20 +0000 (12:45 -0500)
committerDoug Coleman <erg@jobim.local>
Sun, 17 May 2009 17:45:20 +0000 (12:45 -0500)
basis/checksums/hmac/hmac-tests.factor
basis/checksums/hmac/hmac.factor
basis/checksums/md5/md5.factor
basis/checksums/sha2/sha2-tests.factor
basis/checksums/sha2/sha2.factor
core/checksums/checksums.factor

index 9541ca2f267fc2e27d8e554e161853e5160e258d..8835bed98102e7f7523e69cdffb3181caa7ac725 100755 (executable)
@@ -1,6 +1,6 @@
 USING: kernel io strings byte-arrays sequences namespaces math
 parser checksums.hmac tools.test checksums.md5 checksums.sha1
-checksums.sha2 ;
+checksums.sha2 checksums ;
 IN: checksums.hmac.tests
 
 [
@@ -39,4 +39,11 @@ IN: checksums.hmac.tests
 ] unit-test
 
 [ "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" ]
-[ HEX: b 20 <string> sha-256 hmac-bytes >string ] unit-test
+[ 20 HEX: b <string> "Hi There" sha-256 hmac-bytes hex-string ] unit-test
+
+[ "167f928588c5cc2eef8e3093caa0e87c9ff566a14794aa61648d81621a2a40c6" ]
+[
+    "JefeJefeJefeJefeJefeJefeJefeJefe"
+    "what do ya want for nothing?" sha-256 hmac-bytes hex-string
+] unit-test
+
index 17b391f2154e4a0e4922640af8ee9b83caaf5cf9..538dfc92c81ef9fdd32162e8513f9ad9498db986 100755 (executable)
@@ -3,48 +3,35 @@
 USING: arrays checksums checksums.md5 checksums.md5.private
 checksums.sha1 combinators fry io io.binary io.encodings.binary
 io.files io.streams.byte-array kernel math math.vectors memoize
-sequences ;
+sequences locals accessors ;
 IN: checksums.hmac
 
 <PRIVATE
 
-/*
-: sha1-hmac ( Ko Ki stream -- hmac )
-    initialize-sha1 process-sha1-block
-    stream>sha1 get-sha1
-    initialize-sha1
-    [ process-sha1-block ]
-    [ process-sha1-block ] bi* get-sha1 ;
-
- : md5-hmac ( Ko Ki stream -- hmac )
-    initialize-md5 process-md5-block
-    stream>md5 get-md5
-    initialize-md5
-    [ process-md5-block ]
-    [ process-md5-block ] bi* get-md5 ;
-*/
-
 : seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
 
-MEMO: opad ( -- seq ) 64 HEX: 5c <array> ;
+: opad ( checksum-state -- seq ) block-size>> HEX: 5c <array> ;
 
-MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
+: ipad ( checksum-state -- seq ) block-size>> HEX: 36 <array> ;
 
-: init-K ( K -- o i )
-    64 0 pad-tail 
-    [ opad seq-bitxor ]
-    [ ipad seq-bitxor ] bi ;
+:: init-K ( K checksum checksum-state -- o i )
+    checksum-state block-size>> K length <
+    [ K checksum checksum-bytes ] [ K ] if
+    checksum-state block-size>> 0 pad-tail 
+    [ checksum-state opad seq-bitxor ]
+    [ checksum-state ipad seq-bitxor ] bi ;
 
 PRIVATE>
 
 :: hmac-stream ( K stream checksum -- value )
-    K init-K :> Ki :> Ko
-    checksum initialize-checksum
-    Ki add-bytes
-    stream add-stream finish-checksum
-    checksum initialize-checksum
-    Ko add-bytes swap add-bytes
-    finish-checksum ;
+    K checksum dup initialize-checksum-state
+        dup :> checksum-state
+        init-K :> Ki :> Ko
+    checksum-state Ki add-checksum-bytes
+    stream add-checksum-stream get-checksum
+    checksum initialize-checksum-state
+    Ko add-checksum-bytes swap add-checksum-bytes
+    get-checksum ;
 
 : hmac-file ( K path checksum -- value )
     [ binary <file-reader> ] dip hmac-stream ;
index 97a263bab5fed4b16e7bd2b14ba88ba60df36c77..026df340125fb31bafc6cfa2422c362a34ac5e25 100644 (file)
@@ -14,10 +14,13 @@ INSTANCE: md5 stream-checksum
 TUPLE: md5-state < checksum-state state old-state ;
 
 : <md5-state> ( -- md5 )
-    64 md5-state new-checksum-state
+    md5-state new-checksum-state
+        64 >>block-size
         { HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
         [ clone >>state ] [ >>old-state ] bi ;
 
+M: md5 initialize-checksum-state drop <md5-state> ;
+
 <PRIVATE
 
 : v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
index c14ea5a98db8a776202d530926ec6c26f33803ee..010ca96d4fee076f8c950205e1d5e08b34e9d667 100644 (file)
@@ -36,7 +36,5 @@ IN: checksums.sha2.tests
 ] unit-test
 
 
-
-
 ! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
 ! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 test-checksum ] unit-test
index 509b047d2ed3d0052241267756ec3d8ff1760b97..8992299db004bf6bed7f14310b475d9df74f990a 100644 (file)
@@ -122,17 +122,23 @@ CONSTANT: K-384
 ALIAS: K-512 K-384
 
 : <sha-224-state> ( -- sha2-state )
-    64 sha-224-state new-checksum-state
+    sha-224-state new-checksum-state
+        64 >>block-size
         K-256 >>K
         initial-H-224 >>H
         4 >>word-size ;
 
 : <sha-256-state> ( -- sha2-state )
-    64 sha-256-state new-checksum-state
+    sha-256-state new-checksum-state
+        64 >>block-size
         K-256 >>K
         initial-H-256 >>H
         4 >>word-size ;
 
+M: sha-224 initialize-checksum-state drop <sha-224-state> ;
+
+M: sha-256 initialize-checksum-state drop <sha-256-state> ;
+
 : s0-256 ( x -- x' )
     [
         [ -7 bitroll-32 ]
index 0910a3efac6deb453b24e9b6fd22e4a01c04e4ad..1d57823e187c3f97a5807e253581892f30db7106 100644 (file)
@@ -8,9 +8,8 @@ MIXIN: checksum
 
 TUPLE: checksum-state bytes-read block-size bytes ;
 
-: new-checksum-state ( block-size class -- checksum-state )
+: new-checksum-state ( class -- checksum-state )
     new
-        swap >>block-size
         0 >>bytes-read
         V{ } clone >>bytes ; inline
 
@@ -18,6 +17,8 @@ M: checksum-state clone
     call-next-method
     [ clone ] change-bytes ;
 
+GENERIC: initialize-checksum-state ( class -- checksum-state )
+
 GENERIC: checksum-block ( bytes checksum -- )
 
 GENERIC: get-checksum ( checksum -- value )