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
[
] 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
+
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 ;
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 ;
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 ]
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
call-next-method
[ clone ] change-bytes ;
+GENERIC: initialize-checksum-state ( class -- checksum-state )
+
GENERIC: checksum-block ( bytes checksum -- )
GENERIC: get-checksum ( checksum -- value )