]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/hmac/hmac.factor
b163766016a33be55a0be16b0f0ee1253d9ef4e1
[factor.git] / basis / checksums / hmac / hmac.factor
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 ;
6 IN: checksums.hmac
7
8 <PRIVATE
9
10 : seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
11
12 : opad ( checksum-state -- seq ) block-size>> HEX: 5c <array> ;
13
14 : ipad ( checksum-state -- seq ) block-size>> HEX: 36 <array> ;
15
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 ;
22
23 PRIVATE>
24
25 :: hmac-stream ( K stream checksum -- value )
26     K checksum dup initialize-checksum-state
27         dup :> checksum-state
28         init-K :> Ki :> Ko
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
33     get-checksum ;
34
35 : hmac-file ( K path checksum -- value )
36     [ binary <file-reader> ] dip hmac-stream ;
37
38 : hmac-bytes ( K seq checksum -- value )
39     [ binary <byte-reader> ] dip hmac-stream ;