working on checksums
parent
6dabec9ed8
commit
bee3c05fe9
|
@ -8,6 +8,7 @@ IN: checksums.hmac
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
/*
|
||||||
: sha1-hmac ( Ko Ki stream -- hmac )
|
: sha1-hmac ( Ko Ki stream -- hmac )
|
||||||
initialize-sha1 process-sha1-block
|
initialize-sha1 process-sha1-block
|
||||||
stream>sha1 get-sha1
|
stream>sha1 get-sha1
|
||||||
|
@ -15,12 +16,13 @@ IN: checksums.hmac
|
||||||
[ process-sha1-block ]
|
[ process-sha1-block ]
|
||||||
[ process-sha1-block ] bi* get-sha1 ;
|
[ process-sha1-block ] bi* get-sha1 ;
|
||||||
|
|
||||||
: md5-hmac ( Ko Ki stream -- hmac )
|
: md5-hmac ( Ko Ki stream -- hmac )
|
||||||
initialize-md5 process-md5-block
|
initialize-md5 process-md5-block
|
||||||
stream>md5 get-md5
|
stream>md5 get-md5
|
||||||
initialize-md5
|
initialize-md5
|
||||||
[ process-md5-block ]
|
[ process-md5-block ]
|
||||||
[ process-md5-block ] bi* get-md5 ;
|
[ process-md5-block ] bi* get-md5 ;
|
||||||
|
*/
|
||||||
|
|
||||||
: seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
|
: seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
|
||||||
|
|
||||||
|
@ -35,12 +37,14 @@ MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: hmac ( K stream checksum -- value )
|
|
||||||
;
|
|
||||||
|
|
||||||
:: hmac-stream ( K stream checksum -- value )
|
:: hmac-stream ( K stream checksum -- value )
|
||||||
K init-K :> i :> o
|
K init-K :> Ki :> Ko
|
||||||
stream checksum checksum-stream ;
|
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 )
|
: hmac-file ( K path checksum -- value )
|
||||||
[ binary <file-reader> ] dip hmac-stream ;
|
[ binary <file-reader> ] dip hmac-stream ;
|
||||||
|
|
|
@ -13,7 +13,7 @@ TUPLE: md5-state bytes-read state old-state ;
|
||||||
md5-state new
|
md5-state new
|
||||||
0 >>bytes-read
|
0 >>bytes-read
|
||||||
{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
|
{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
|
||||||
[ clone >>state ] [ clone >>old-state ] bi ;
|
[ clone >>state ] [ >>old-state ] bi ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
@ -21,10 +21,10 @@ TUPLE: md5-state bytes-read state old-state ;
|
||||||
|
|
||||||
: update-md5-state ( md5-state -- )
|
: update-md5-state ( md5-state -- )
|
||||||
[ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
|
[ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
|
||||||
[ (>>old-state) ] [ (>>state) ] bi ;
|
[ (>>old-state) ] [ (>>state) ] bi ; inline
|
||||||
|
|
||||||
: T ( N -- Y )
|
: T ( N -- Y )
|
||||||
sin abs 32 2^ * >integer ; foldable
|
sin abs 32 2^ * >integer ; inline
|
||||||
|
|
||||||
:: F ( X Y Z -- FXYZ )
|
:: F ( X Y Z -- FXYZ )
|
||||||
#! F(X,Y,Z) = XY v not(X) Z
|
#! F(X,Y,Z) = XY v not(X) Z
|
||||||
|
@ -180,14 +180,14 @@ MACRO: with-md5-round ( ops quot -- )
|
||||||
[ state (process-md5-block) ] each
|
[ state (process-md5-block) ] each
|
||||||
] if ;
|
] 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 )
|
: get-md5 ( md5-state -- bytes )
|
||||||
state>> [ 4 >le ] map B{ } concat-as ;
|
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>
|
PRIVATE>
|
||||||
|
|
||||||
SINGLETON: md5
|
SINGLETON: md5
|
||||||
|
@ -195,4 +195,17 @@ SINGLETON: md5
|
||||||
INSTANCE: md5 stream-checksum
|
INSTANCE: md5 stream-checksum
|
||||||
|
|
||||||
M: md5 checksum-stream
|
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 ;
|
||||||
|
|
Loading…
Reference in New Issue