]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/checksums/hmac/hmac.factor
use radix literals
[factor.git] / basis / checksums / hmac / hmac.factor
old mode 100755 (executable)
new mode 100644 (file)
index 17b391f..d6fc24d
@@ -1,53 +1,38 @@
 ! Copyright (C) 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-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 ;
+USING: accessors arrays checksums combinators fry io io.binary
+io.encodings.binary io.files io.streams.byte-array kernel
+locals math math.vectors memoize sequences ;
 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>> 0x5c <array> ;
 
-MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
+: ipad ( checksum-state -- seq ) block-size>> 0x36 <array> ;
 
-: init-K ( K -- o i )
-    64 0 pad-tail 
-    [ opad seq-bitxor ]
-    [ ipad seq-bitxor ] bi ;
+:: init-key ( checksum key checksum-state -- o i )
+    checksum-state block-size>> key length <
+    [ key checksum checksum-bytes ] [ key ] 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 ;
+:: hmac-stream ( stream key checksum -- value )
+    checksum initialize-checksum-state :> checksum-state
+    checksum key checksum-state init-key :> ( Ko Ki )
+    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 ;
+: hmac-file ( path key checksum -- value )
+    [ binary <file-reader> ] 2dip hmac-stream ;
 
-: hmac-bytes ( K seq checksum -- value )
-    [ binary <byte-reader> ] dip hmac-stream ;
+: hmac-bytes ( seq key checksum -- value )
+    [ binary <byte-reader> ] 2dip hmac-stream ;