]> gitweb.factorcode.org Git - factor.git/blob - extra/crypto/hmac/hmac.factor
Merge branch 'master' into experimental
[factor.git] / extra / crypto / hmac / hmac.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators checksums checksums.md5
4 checksums.sha1 checksums.md5.private io io.binary io.files
5 io.streams.byte-array kernel math math.vectors memoize sequences
6 io.encodings.binary ;
7 IN: crypto.hmac
8
9 : sha1-hmac ( Ko Ki -- hmac )
10     initialize-sha1 process-sha1-block
11     stream>sha1 get-sha1
12     initialize-sha1
13     [ process-sha1-block ]
14     [ process-sha1-block ] bi* get-sha1 ;
15
16 : md5-hmac ( Ko Ki -- hmac )
17     initialize-md5 process-md5-block
18     stream>md5 get-md5
19     initialize-md5
20     [ process-md5-block ]
21     [ process-md5-block ] bi* get-md5 ;
22
23 : seq-bitxor ( seq seq -- seq )
24     [ bitxor ] 2map ;
25
26 MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
27 MEMO: opad ( -- seq ) 64 HEX: 5c <array> ;
28
29 : init-hmac ( K -- o i )
30     64 0 pad-right 
31     [ opad seq-bitxor ] keep
32     ipad seq-bitxor ;
33
34 : stream>sha1-hmac ( K stream -- hmac )
35     [ init-hmac sha1-hmac ] with-input-stream ;
36
37 : file>sha1-hmac ( K path -- hmac )
38     binary <file-reader> stream>sha1-hmac ;
39
40 : byte-array>sha1-hmac ( K string -- hmac )
41     binary <byte-reader> stream>sha1-hmac ;
42
43 : stream>md5-hmac ( K stream -- hmac )
44     [ init-hmac md5-hmac ] with-input-stream ;
45
46 : file>md5-hmac ( K path -- hmac )
47     binary <file-reader> stream>md5-hmac ;
48
49 : byte-array>md5-hmac ( K string -- hmac )
50     binary <byte-reader> stream>md5-hmac ;