]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/hmac/hmac.factor
use radix literals
[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>> 0x5c <array> ;
13
14 : ipad ( checksum-state -- seq ) block-size>> 0x36 <array> ;
15
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 ;
22
23 PRIVATE>
24
25 :: hmac-stream ( stream key checksum -- value )
26     checksum initialize-checksum-state :> checksum-state
27     checksum key checksum-state init-key :> ( Ko Ki )
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
32     get-checksum ;
33
34 : hmac-file ( path key checksum -- value )
35     [ binary <file-reader> ] 2dip hmac-stream ;
36
37 : hmac-bytes ( seq key checksum -- value )
38     [ binary <byte-reader> ] 2dip hmac-stream ;