working on hmac

db4
Doug Coleman 2009-05-17 12:45:20 -05:00
parent b2ac4396c1
commit f1f1a26b60
6 changed files with 41 additions and 39 deletions

View File

@ -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

View File

@ -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 ;

View 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 ;

View File

@ -36,7 +36,5 @@ IN: checksums.sha2.tests
] unit-test
! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 test-checksum ] unit-test

View 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 ]

View 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 )