Friday, 22 March 2013

MD5 checksum

If you use Rebol 2, don't use the checksum/method 'md5 since is broken.
Fortunately Andreas Bolka made a script to correct this behavior:

md5sum: func [
    "Returns an MD5 checksum for the contents of the file given."
    fname [file!] /local fport sport chunk-size
] [
    chunk-size: 4096 ;; 4K chunks, just like in md5sum (matches page size)
    fport: open/seek/binary/read fname
    sport: open [scheme: 'checksum algorithm: 'md5]
    while [not tail? fport] [
        insert sport copy/part fport chunk-size
        fport: skip fport chunk-size
    close fport
    update sport
    sum: copy sport
    close sport

The Rebol 3 checksum works well, unfortunately is not so friendly as the Rebol 2 version: the R3 wants a binary as input, so you have to write:
checksum/method (to-binary read %myfile)   'md5

1 comment:

  1. No, it is not broken.
    READ function converts line terminations character to unix style internally (CRLF to LF), so if you use "CHECKSUM/method READ %file.txt" it gives different result than "CHECKSUM/method READ/binary %file.txt".
    Binary is the default mode for READ function on R3, so:
    "CHECKSUM/method READ %file.txt" returns different results on R2 and R3, that makes you think that it is broken I think.