From 0bdccdb7acbcac5f6d8f0339d39cdb380c4709a8 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 16 May 2009 18:00:56 -0500 Subject: [PATCH] checksums work now --- basis/checksums/md5/md5-tests.factor | 21 ++++++++++++++++++ basis/checksums/md5/md5.factor | 32 ++++++++++++++++++---------- core/checksums/checksums.factor | 6 +++++- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/basis/checksums/md5/md5-tests.factor b/basis/checksums/md5/md5-tests.factor index 8e314f7c28..db15540f43 100644 --- a/basis/checksums/md5/md5-tests.factor +++ b/basis/checksums/md5/md5-tests.factor @@ -8,3 +8,24 @@ USING: kernel math namespaces checksums checksums.md5 tools.test byte-arrays ; [ "d174ab98d277d9f5a5611c2c9f419d9f" ] [ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" >byte-array md5 checksum-bytes hex-string ] unit-test [ "57edf4a22be3c955ac49da2e2107b67a" ] [ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" >byte-array md5 checksum-bytes hex-string ] unit-test + +[ + t +] [ + "asdf" add-checksum-bytes + [ get-checksum ] [ get-checksum ] bi = +] unit-test + +[ + t +] [ + "" add-checksum-bytes + [ get-checksum ] [ get-checksum ] bi = +] unit-test + +[ + t +] [ + "asdf" binary add-checksum-stream + [ get-checksum ] [ get-checksum ] bi = +] unit-test diff --git a/basis/checksums/md5/md5.factor b/basis/checksums/md5/md5.factor index ee00817ea5..97a263bab5 100644 --- a/basis/checksums/md5/md5.factor +++ b/basis/checksums/md5/md5.factor @@ -8,11 +8,12 @@ checksums.common checksums.stream combinators combinators.smart ; IN: checksums.md5 SINGLETON: md5 + INSTANCE: md5 stream-checksum TUPLE: md5-state < checksum-state state old-state ; -: ( -- md5-state ) +: ( -- md5 ) 64 md5-state new-checksum-state { HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 } [ clone >>state ] [ >>old-state ] bi ; @@ -21,7 +22,7 @@ TUPLE: md5-state < checksum-state state old-state ; : v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ; -: update-md5-state ( md5-state -- ) +: update-md5 ( md5 -- ) [ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri [ (>>old-state) ] [ (>>state) ] bi ; inline @@ -69,13 +70,13 @@ CONSTANT: d 3 :: (ABCD) ( x V a b c d k s i quot -- ) #! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) a V [ - b V nth-unsafe - c V nth-unsafe - d V nth-unsafe quot call w+ - k x nth-unsafe w+ + b V nth + c V nth + d V nth quot call w+ + k x nth w+ i T w+ s bitroll-32 - b V nth-unsafe w+ + b V nth w+ ] change-nth ; inline MACRO: with-md5-round ( ops quot -- ) @@ -170,14 +171,23 @@ M: md5-state checksum-block ( block state -- ) [ (process-md5-block-I) ] } 2cleave ] [ - nip update-md5-state + nip update-md5 ] 2bi ; -: md5-state>checksum ( md5-state -- bytes ) +: md5>checksum ( md5 -- bytes ) state>> [ 4 >le ] map B{ } concat-as ; -M: md5-state get-checksum ( md5-state -- bytes ) +M: md5-state clone ( md5 -- new-md5 ) + call-next-method + [ clone ] change-state + [ clone ] change-old-state ; + +M: md5-state get-checksum ( md5 -- bytes ) clone [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri - [ [ checksum-block ] curry each ] [ md5-state>checksum ] bi ; + [ [ checksum-block ] curry each ] [ md5>checksum ] bi ; + +M: md5 checksum-stream ( stream checksum -- byte-array ) + drop + [ ] dip add-checksum-stream get-checksum ; PRIVATE> diff --git a/core/checksums/checksums.factor b/core/checksums/checksums.factor index 27ee6a3435..0910a3efac 100644 --- a/core/checksums/checksums.factor +++ b/core/checksums/checksums.factor @@ -14,6 +14,10 @@ TUPLE: checksum-state bytes-read block-size bytes ; 0 >>bytes-read V{ } clone >>bytes ; inline +M: checksum-state clone + call-next-method + [ clone ] change-bytes ; + GENERIC: checksum-block ( bytes checksum -- ) GENERIC: get-checksum ( checksum -- value ) @@ -26,7 +30,7 @@ GENERIC: get-checksum ( checksum -- value ) over [ checksum-block ] [ [ 64 + ] change-bytes-read drop ] bi ] dip - ] while >vector >>bytes ; + ] while >vector [ >>bytes ] [ length [ + ] curry change-bytes-read ] bi ; : add-checksum-stream ( checksum-state stream -- checksum-state ) [