1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators checksums checksums.md5
4 checksums.sha1 checksums.md5.private io io.binary io.files
5 io.streams.byte-array kernel math math.vectors memoize sequences
9 : sha1-hmac ( Ko Ki -- hmac )
10 initialize-sha1 process-sha1-block
13 [ process-sha1-block ]
14 [ process-sha1-block ] bi* get-sha1 ;
16 : md5-hmac ( Ko Ki -- hmac )
17 initialize-md5 process-md5-block
21 [ process-md5-block ] bi* get-md5 ;
23 : seq-bitxor ( seq seq -- seq )
26 MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
27 MEMO: opad ( -- seq ) 64 HEX: 5c <array> ;
29 : init-hmac ( K -- o i )
31 [ opad seq-bitxor ] keep
34 : stream>sha1-hmac ( K stream -- hmac )
35 [ init-hmac sha1-hmac ] with-input-stream ;
37 : file>sha1-hmac ( K path -- hmac )
38 binary <file-reader> stream>sha1-hmac ;
40 : byte-array>sha1-hmac ( K string -- hmac )
41 binary <byte-reader> stream>sha1-hmac ;
43 : stream>md5-hmac ( K stream -- hmac )
44 [ init-hmac md5-hmac ] with-input-stream ;
46 : file>md5-hmac ( K path -- hmac )
47 binary <file-reader> stream>md5-hmac ;
49 : byte-array>md5-hmac ( K string -- hmac )
50 binary <byte-reader> stream>md5-hmac ;