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
10 : opad ( checksum-state -- seq ) block-size>> 0x5c <array> ;
12 : ipad ( checksum-state -- seq ) block-size>> 0x36 <array> ;
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 ;
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
32 : hmac-file ( path key checksum -- value )
33 [ binary <file-reader> ] 2dip hmac-stream ;
35 : hmac-bytes ( seq key checksum -- value )
36 [ binary <byte-reader> ] 2dip hmac-stream ;