2009-05-10 21:42:20 -04:00
|
|
|
! Copyright (C) 2008 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays checksums checksums.md5 checksums.md5.private
|
|
|
|
checksums.sha1 combinators fry io io.binary io.encodings.binary
|
|
|
|
io.files io.streams.byte-array kernel math math.vectors memoize
|
2009-05-17 13:45:20 -04:00
|
|
|
sequences locals accessors ;
|
2009-05-10 21:42:20 -04:00
|
|
|
IN: checksums.hmac
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: seq-bitxor ( seq seq -- seq ) [ bitxor ] 2map ;
|
|
|
|
|
2009-05-17 13:45:20 -04:00
|
|
|
: opad ( checksum-state -- seq ) block-size>> HEX: 5c <array> ;
|
2009-05-10 21:42:20 -04:00
|
|
|
|
2009-05-17 13:45:20 -04:00
|
|
|
: ipad ( checksum-state -- seq ) block-size>> HEX: 36 <array> ;
|
2009-05-10 21:42:20 -04:00
|
|
|
|
2009-05-17 13:45:20 -04:00
|
|
|
:: init-K ( K checksum checksum-state -- o i )
|
|
|
|
checksum-state block-size>> K length <
|
|
|
|
[ K checksum checksum-bytes ] [ K ] if
|
|
|
|
checksum-state block-size>> 0 pad-tail
|
|
|
|
[ checksum-state opad seq-bitxor ]
|
|
|
|
[ checksum-state ipad seq-bitxor ] bi ;
|
2009-05-10 21:42:20 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
:: hmac-stream ( K stream checksum -- value )
|
2009-05-17 13:45:20 -04:00
|
|
|
K checksum dup initialize-checksum-state
|
|
|
|
dup :> checksum-state
|
|
|
|
init-K :> Ki :> Ko
|
|
|
|
checksum-state Ki add-checksum-bytes
|
|
|
|
stream add-checksum-stream get-checksum
|
|
|
|
checksum initialize-checksum-state
|
|
|
|
Ko add-checksum-bytes swap add-checksum-bytes
|
|
|
|
get-checksum ;
|
2009-05-10 21:42:20 -04:00
|
|
|
|
|
|
|
: hmac-file ( K path checksum -- value )
|
|
|
|
[ binary <file-reader> ] dip hmac-stream ;
|
|
|
|
|
2009-05-16 14:03:09 -04:00
|
|
|
: hmac-bytes ( K seq checksum -- value )
|
2009-05-10 21:42:20 -04:00
|
|
|
[ binary <byte-reader> ] dip hmac-stream ;
|