checksums.common: Checksums was calculating the new length based on the entire array, not on the number of recently added bytes.

Reported by Александр Ilin.
char-rename
Doug Coleman 2016-07-09 13:53:30 -05:00
parent e4f2770456
commit 2c5a2e9600
2 changed files with 19 additions and 9 deletions

View File

@ -39,16 +39,18 @@ GENERIC: checksum-block ( bytes checksum-state -- )
GENERIC: get-checksum ( checksum-state -- value )
: add-checksum-bytes ( checksum-state data -- checksum-state' )
over bytes>> [ push-all ] keep
[ dup length pick block-size>> >= ]
[
over block-size>> cut-slice [
over checksum-block
[ block-size>> ] keep [ + ] change-bytes-read
] dip
] while
>byte-vector
[ >>bytes ] [ length [ + ] curry change-bytes-read ] bi ;
over bytes>> [ push-all ] keep
[ dup length pick block-size>> >= ]
[
over block-size>> cut-slice [
over checksum-block
[ block-size>> ] keep [ + ] change-bytes-read
] dip
] while
>byte-vector >>bytes
] keep
length [ + ] curry change-bytes-read ;
: add-checksum-stream ( checksum-state stream -- checksum-state )
[ [ add-checksum-bytes ] each-block ] with-input-stream ;

View File

@ -67,3 +67,11 @@ IN: checksums.sha.tests
<sha-224-state> "asdf" binary <byte-reader> add-checksum-stream
[ get-checksum ] [ get-checksum ] bi =
] unit-test
CONSTANT: bytes-a B{ 0 1 0 0 0 0 0 0 }
CONSTANT: bytes-b B{ 1 2 3 4 5 6 7 8 }
{ t } [
sha1 initialize-checksum-state bytes-a bytes-b append add-checksum-bytes get-checksum
sha1 initialize-checksum-state bytes-a add-checksum-bytes bytes-b add-checksum-bytes get-checksum
] unit-test