working on hmac
parent
b2ac4396c1
commit
f1f1a26b60
|
@ -1,6 +1,6 @@
|
||||||
USING: kernel io strings byte-arrays sequences namespaces math
|
USING: kernel io strings byte-arrays sequences namespaces math
|
||||||
parser checksums.hmac tools.test checksums.md5 checksums.sha1
|
parser checksums.hmac tools.test checksums.md5 checksums.sha1
|
||||||
checksums.sha2 ;
|
checksums.sha2 checksums ;
|
||||||
IN: checksums.hmac.tests
|
IN: checksums.hmac.tests
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -39,4 +39,11 @@ IN: checksums.hmac.tests
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" ]
|
[ "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
|
||||||
|
|
||||||
|
|
|
@ -3,48 +3,35 @@
|
||||||
USING: arrays checksums checksums.md5 checksums.md5.private
|
USING: arrays checksums checksums.md5 checksums.md5.private
|
||||||
checksums.sha1 combinators fry io io.binary io.encodings.binary
|
checksums.sha1 combinators fry io io.binary io.encodings.binary
|
||||||
io.files io.streams.byte-array kernel math math.vectors memoize
|
io.files io.streams.byte-array kernel math math.vectors memoize
|
||||||
sequences ;
|
sequences locals accessors ;
|
||||||
IN: checksums.hmac
|
IN: checksums.hmac
|
||||||
|
|
||||||
<PRIVATE
|
<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 ;
|
: 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 )
|
:: init-K ( K checksum checksum-state -- o i )
|
||||||
64 0 pad-tail
|
checksum-state block-size>> K length <
|
||||||
[ opad seq-bitxor ]
|
[ K checksum checksum-bytes ] [ K ] if
|
||||||
[ ipad seq-bitxor ] bi ;
|
checksum-state block-size>> 0 pad-tail
|
||||||
|
[ checksum-state opad seq-bitxor ]
|
||||||
|
[ checksum-state ipad seq-bitxor ] bi ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
:: hmac-stream ( K stream checksum -- value )
|
:: hmac-stream ( K stream checksum -- value )
|
||||||
K init-K :> Ki :> Ko
|
K checksum dup initialize-checksum-state
|
||||||
checksum initialize-checksum
|
dup :> checksum-state
|
||||||
Ki add-bytes
|
init-K :> Ki :> Ko
|
||||||
stream add-stream finish-checksum
|
checksum-state Ki add-checksum-bytes
|
||||||
checksum initialize-checksum
|
stream add-checksum-stream get-checksum
|
||||||
Ko add-bytes swap add-bytes
|
checksum initialize-checksum-state
|
||||||
finish-checksum ;
|
Ko add-checksum-bytes swap add-checksum-bytes
|
||||||
|
get-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 ;
|
||||||
|
|
|
@ -14,10 +14,13 @@ INSTANCE: md5 stream-checksum
|
||||||
TUPLE: md5-state < checksum-state state old-state ;
|
TUPLE: md5-state < checksum-state state old-state ;
|
||||||
|
|
||||||
: <md5-state> ( -- md5 )
|
: <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 }
|
{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
|
||||||
[ clone >>state ] [ >>old-state ] bi ;
|
[ clone >>state ] [ >>old-state ] bi ;
|
||||||
|
|
||||||
|
M: md5 initialize-checksum-state drop <md5-state> ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
|
: v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
|
||||||
|
|
|
@ -36,7 +36,5 @@ IN: checksums.sha2.tests
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
|
! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
|
||||||
! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 test-checksum ] unit-test
|
! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 test-checksum ] unit-test
|
||||||
|
|
|
@ -122,17 +122,23 @@ CONSTANT: K-384
|
||||||
ALIAS: K-512 K-384
|
ALIAS: K-512 K-384
|
||||||
|
|
||||||
: <sha-224-state> ( -- sha2-state )
|
: <sha-224-state> ( -- sha2-state )
|
||||||
64 sha-224-state new-checksum-state
|
sha-224-state new-checksum-state
|
||||||
|
64 >>block-size
|
||||||
K-256 >>K
|
K-256 >>K
|
||||||
initial-H-224 >>H
|
initial-H-224 >>H
|
||||||
4 >>word-size ;
|
4 >>word-size ;
|
||||||
|
|
||||||
: <sha-256-state> ( -- sha2-state )
|
: <sha-256-state> ( -- sha2-state )
|
||||||
64 sha-256-state new-checksum-state
|
sha-256-state new-checksum-state
|
||||||
|
64 >>block-size
|
||||||
K-256 >>K
|
K-256 >>K
|
||||||
initial-H-256 >>H
|
initial-H-256 >>H
|
||||||
4 >>word-size ;
|
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' )
|
: s0-256 ( x -- x' )
|
||||||
[
|
[
|
||||||
[ -7 bitroll-32 ]
|
[ -7 bitroll-32 ]
|
||||||
|
|
|
@ -8,9 +8,8 @@ MIXIN: checksum
|
||||||
|
|
||||||
TUPLE: checksum-state bytes-read block-size bytes ;
|
TUPLE: checksum-state bytes-read block-size bytes ;
|
||||||
|
|
||||||
: new-checksum-state ( block-size class -- checksum-state )
|
: new-checksum-state ( class -- checksum-state )
|
||||||
new
|
new
|
||||||
swap >>block-size
|
|
||||||
0 >>bytes-read
|
0 >>bytes-read
|
||||||
V{ } clone >>bytes ; inline
|
V{ } clone >>bytes ; inline
|
||||||
|
|
||||||
|
@ -18,6 +17,8 @@ M: checksum-state clone
|
||||||
call-next-method
|
call-next-method
|
||||||
[ clone ] change-bytes ;
|
[ clone ] change-bytes ;
|
||||||
|
|
||||||
|
GENERIC: initialize-checksum-state ( class -- checksum-state )
|
||||||
|
|
||||||
GENERIC: checksum-block ( bytes checksum -- )
|
GENERIC: checksum-block ( bytes checksum -- )
|
||||||
|
|
||||||
GENERIC: get-checksum ( checksum -- value )
|
GENERIC: get-checksum ( checksum -- value )
|
||||||
|
|
Loading…
Reference in New Issue