factor/extra/crypto/hmac/hmac.factor

49 lines
1.3 KiB
Factor
Raw Normal View History

2008-10-02 19:45:51 -04:00
USING: arrays combinators checksums checksums.md5
2008-05-01 21:02:34 -04:00
checksums.sha1 checksums.md5.private io io.binary io.files
2008-04-30 17:11:55 -04:00
io.streams.byte-array kernel math math.vectors memoize sequences
io.encodings.binary ;
2007-09-20 18:09:08 -04:00
IN: crypto.hmac
: sha1-hmac ( Ko Ki -- hmac )
initialize-sha1 process-sha1-block
2008-05-01 21:02:34 -04:00
stream>sha1 get-sha1
2007-09-20 18:09:08 -04:00
initialize-sha1
>r process-sha1-block r>
process-sha1-block get-sha1 ;
: md5-hmac ( Ko Ki -- hmac )
initialize-md5 process-md5-block
2008-05-01 21:02:34 -04:00
stream>md5 get-md5
2007-09-20 18:09:08 -04:00
initialize-md5
>r process-md5-block r>
process-md5-block get-md5 ;
: seq-bitxor ( seq seq -- seq )
[ bitxor ] 2map ;
MEMO: ipad ( -- seq ) 64 HEX: 36 <array> ;
MEMO: opad ( -- seq ) 64 HEX: 5c <array> ;
: init-hmac ( K -- o i )
64 0 pad-right
[ opad seq-bitxor ] keep
ipad seq-bitxor ;
: stream>sha1-hmac ( K stream -- hmac )
[ init-hmac sha1-hmac ] with-input-stream ;
2007-09-20 18:09:08 -04:00
: file>sha1-hmac ( K path -- hmac )
binary <file-reader> stream>sha1-hmac ;
2007-09-20 18:09:08 -04:00
2008-03-08 03:51:26 -05:00
: byte-array>sha1-hmac ( K string -- hmac )
binary <byte-reader> stream>sha1-hmac ;
2007-09-20 18:09:08 -04:00
: stream>md5-hmac ( K stream -- hmac )
[ init-hmac md5-hmac ] with-input-stream ;
2007-09-20 18:09:08 -04:00
: file>md5-hmac ( K path -- hmac )
binary <file-reader> stream>md5-hmac ;
2007-09-20 18:09:08 -04:00
2008-03-08 03:51:26 -05:00
: byte-array>md5-hmac ( K string -- hmac )
binary <byte-reader> stream>md5-hmac ;