factor/basis/checksums/hmac/hmac.factor

37 lines
1.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays checksums checksums.common
io.encodings.binary io.files io.streams.byte-array kernel locals
math math.vectors sequences ;
IN: checksums.hmac
<PRIVATE
2011-11-23 21:49:33 -05:00
: opad ( checksum-state -- seq ) block-size>> 0x5c <array> ;
2011-11-23 21:49:33 -05:00
: ipad ( checksum-state -- seq ) block-size>> 0x36 <array> ;
2009-05-26 18:46:41 -04:00
:: init-key ( checksum key checksum-state -- o i )
checksum-state block-size>> key length <
[ key checksum checksum-bytes ] [ key ] if
checksum-state block-size>> 0 pad-tail
[ checksum-state opad vbitxor ]
[ checksum-state ipad vbitxor ] bi ;
PRIVATE>
2009-05-26 18:46:41 -04:00
:: hmac-stream ( stream key checksum -- value )
checksum initialize-checksum-state :> checksum-state
checksum key checksum-state init-key :> ( Ko Ki )
2009-05-17 13:45:20 -04:00
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-26 18:46:41 -04:00
: hmac-file ( path key checksum -- value )
[ binary <file-reader> ] 2dip hmac-stream ;
2009-05-26 18:46:41 -04:00
: hmac-bytes ( seq key checksum -- value )
[ binary <byte-reader> ] 2dip hmac-stream ;