factor/basis/checksums/hmac/hmac.factor

41 lines
1.3 KiB
Factor
Raw Normal View History

! 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 ;
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-17 13:45:20 -04:00
: ipad ( checksum-state -- seq ) block-size>> HEX: 36 <array> ;
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 ;
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 ;
: 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 )
[ binary <byte-reader> ] dip hmac-stream ;