[plt-scheme] MD5 in v299.400

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Oct 21 09:57:03 EDT 2005

David Richards wrote:

> On Oct 21, 2005, at 7:57 AM, Jens Axel Søgaard wrote:
>
>> David Richards wrote:
>>
>>>
>>> I don't understand the discrepancy between the definition of md5  
>>> in  md5.ss, and the one which is provided by (require (lib  "md5.ss")).
>>
>> Where is the first the first md5.ss located? The one in (require  
>> (lib "md5.ss")) is in "collects/mzlib/md5.ss".
>
> Yes.  That's it.
>
>>> I don't understand why md5 requires a byte-string argument,  
>>> instead  of a string.
>>
>> That is due to unicode. The md5-algorithm works on good, old  strings 
>> consisting
>> of 8-bit characters. The source of md5.ss keeps mentioning strings,  
>> but note that
>> it was written in 2002, before unicode and byte-strings were  
>> introduced in PLT Scheme.
>
>
> Ok, yes, I assumed it had something to do with that.  But my question  
> is still not answered.  If the scheme code in "collects/mzlib/md5.ss"  
> uses strings, why does the the md5 function provided by (require (lib  
> "md5.ss")) require bytes?

The code in "collects/mzlib/md5.ss" does require bytes. If you try (md5 
"abc") you get an

     bytes-length: expects argument of type <byte string>; given "abc"

error, and a search for bytes-length in md5.ss gives:

  ;; MD5
  ;; The algorithm consists of five steps.
  ;; All we need to do, is to call them in order.
  ;; md5 : string -> string

  (define (md5-computation str)
    (step5 (step4 (step2 (* 8 (bytes-length str)) 
			 (step1 (string->bytes str))))))

and (much) later:

 (set! md5 md5-computation)    

So md5 (or md5-computation) expects str to be bytes since it uses 
(bytes-length str).

The function string->bytes does indeed have a very confusing name.

  (define (string->bytes s)
     (bytes->list s))

That is, string->bytes does not turn a string into bytes, but
turns bytes into a list of characters. Since bytes were called strings
in 2002, and a list of chars were called bytes, the name hopefully makes
a little sense after all.

-- 
Jens Axel Søgaard




Posted on the users mailing list.