]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/hmac/hmac.factor
factor: trim using lists
[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 io.encodings.binary
4 io.files io.streams.byte-array kernel math math.vectors
5 sequences ;
6 IN: checksums.hmac
7
8 <PRIVATE
9
10 : opad ( checksum-state -- seq ) block-size>> 0x5c <array> ;
11
12 : ipad ( checksum-state -- seq ) block-size>> 0x36 <array> ;
13
14 :: init-key ( checksum key checksum-state -- o i )
15     checksum-state block-size>> key length <
16     [ key checksum checksum-bytes ] [ key ] if
17     checksum-state block-size>> 0 pad-tail
18     [ checksum-state opad vbitxor ]
19     [ checksum-state ipad vbitxor ] bi ;
20
21 PRIVATE>
22
23 :: hmac-stream ( stream key checksum -- value )
24     checksum initialize-checksum-state :> checksum-state
25     checksum key checksum-state init-key :> ( Ko Ki )
26     checksum-state Ki add-checksum-bytes
27     stream add-checksum-stream get-checksum
28     checksum initialize-checksum-state
29     Ko add-checksum-bytes swap add-checksum-bytes
30     get-checksum ;
31
32 : hmac-file ( path key checksum -- value )
33     [ binary <file-reader> ] 2dip hmac-stream ;
34
35 : hmac-bytes ( seq key checksum -- value )
36     [ binary <byte-reader> ] 2dip hmac-stream ;