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-key ( checksum key checksum-state -- o i )
17 checksum-state block-size>> key length <
18 [ key checksum checksum-bytes ] [ key ] 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 ( stream key checksum -- value )
26 checksum initialize-checksum-state :> checksum-state
27 checksum key checksum-state init-key :> Ki :> Ko
28 checksum-state Ki add-checksum-bytes
29 stream add-checksum-stream get-checksum
30 checksum initialize-checksum-state
31 Ko add-checksum-bytes swap add-checksum-bytes
34 : hmac-file ( path key checksum -- value )
35 [ binary <file-reader> ] 2dip hmac-stream ;
37 : hmac-bytes ( seq key checksum -- value )
38 [ binary <byte-reader> ] 2dip hmac-stream ;