working on checksums

db4
Doug Coleman 2009-05-11 11:37:21 -05:00
parent 6dabec9ed8
commit bee3c05fe9
2 changed files with 32 additions and 15 deletions

View File

@ -8,6 +8,7 @@ IN: checksums.hmac
<PRIVATE
/*
: sha1-hmac ( Ko Ki stream -- hmac )
initialize-sha1 process-sha1-block
stream>sha1 get-sha1
@ -15,12 +16,13 @@ IN: checksums.hmac
[ process-sha1-block ]
[ process-sha1-block ] bi* get-sha1 ;
: md5-hmac ( Ko Ki stream -- hmac )
: 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 ;
@ -35,12 +37,14 @@ MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
PRIVATE>
: hmac ( K stream checksum -- value )
;
:: hmac-stream ( K stream checksum -- value )
K init-K :> i :> o
stream checksum checksum-stream ;
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-file ( K path checksum -- value )
[ binary <file-reader> ] dip hmac-stream ;

View File

@ -13,7 +13,7 @@ TUPLE: md5-state bytes-read state old-state ;
md5-state new
0 >>bytes-read
{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
[ clone >>state ] [ clone >>old-state ] bi ;
[ clone >>state ] [ >>old-state ] bi ;
<PRIVATE
@ -21,10 +21,10 @@ TUPLE: md5-state bytes-read state old-state ;
: update-md5-state ( md5-state -- )
[ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
[ (>>old-state) ] [ (>>state) ] bi ;
[ (>>old-state) ] [ (>>state) ] bi ; inline
: T ( N -- Y )
sin abs 32 2^ * >integer ; foldable
sin abs 32 2^ * >integer ; inline
:: F ( X Y Z -- FXYZ )
#! F(X,Y,Z) = XY v not(X) Z
@ -179,15 +179,15 @@ MACRO: with-md5-round ( ops quot -- )
block f state bytes-read>> pad-last-block
[ state (process-md5-block) ] each
] if ;
:: stream>md5 ( stream state -- )
64 stream stream-read
[ state process-md5-block ] [ length 64 = ] bi
[ stream state stream>md5 ] when ;
: get-md5 ( md5-state -- bytes )
state>> [ 4 >le ] map B{ } concat-as ;
:: stream>md5 ( state stream -- )
64 stream stream-read
[ state process-md5-block ] [ length 64 = ] bi
[ state stream stream>md5 ] when ;
PRIVATE>
SINGLETON: md5
@ -195,4 +195,17 @@ SINGLETON: md5
INSTANCE: md5 stream-checksum
M: md5 checksum-stream
drop <md5-state> [ stream>md5 ] [ get-md5 ] bi ;
drop [ <md5-state> ] dip [ stream>md5 ] [ drop get-md5 ] 2bi ;
GENERIC: initialize-checksum ( checksum -- state )
GENERIC# add-bytes 1 ( state bytes -- state )
GENERIC# add-stream 1 ( state stream -- state )
GENERIC: finish-checksum ( state -- bytes )
M: md5 initialize-checksum drop <md5-state> ;
M: md5-state finish-checksum get-md5 ;
M: md5-state add-bytes over [ binary <byte-reader> stream>md5 ] dip ;
M: md5-state add-stream over [ stream>md5 ] dip ;