1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays checksums combinators fry io io.binary
4 io.encodings.binary io.files io.streams.byte-array kernel
5 locals math math.vectors memoize sequences ;
10 : seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
12 : opad ( checksum-state -- seq ) block-size>> HEX: 5c <array> ;
14 : ipad ( checksum-state -- seq ) block-size>> HEX: 36 <array> ;
16 :: init-K ( K checksum checksum-state -- o i )
17 checksum-state block-size>> K length <
18 [ K checksum checksum-bytes ] [ K ] if
19 checksum-state block-size>> 0 pad-tail
20 [ checksum-state opad seq-bitxor ]
21 [ checksum-state ipad seq-bitxor ] bi ;
25 :: hmac-stream ( K stream checksum -- value )
26 K checksum dup initialize-checksum-state
29 checksum-state Ki add-checksum-bytes
30 stream add-checksum-stream get-checksum
31 checksum initialize-checksum-state
32 Ko add-checksum-bytes swap add-checksum-bytes
35 : hmac-file ( K path checksum -- value )
36 [ binary <file-reader> ] dip hmac-stream ;
38 : hmac-bytes ( K seq checksum -- value )
39 [ binary <byte-reader> ] dip hmac-stream ;