You are looking at historical revision 16423 of this page. It may differ significantly from its current revision.

message-digest

Documentation

Message Digest provides support for message digest primitives. A message-digest is a function taking an arbitrary input and returning a fixed-length hash. For best results the input should be something easily treated as a byte-block.

make-binary-message-digest

[procedure] (make-binary-message-digest SOURCE CONTEXT-INFO DIGEST-LENGTH INIT UPDATE FINAL [CALLER]) => STRING

Returns the message-digest for SOURCE as a string of bytes.

SOURCE is any Scheme-object. See the UPDATE entry below for interpretation of the SOURCE.

CONTEXT-INFO is either a procedure that returns an object CONTEXT, or a positive fixnum. When a fixnum a memory-block of length CONTEXT-INFO is allocated (and automatically free'ed). The CONTEXT is often a pointer but maybe any Scheme-object.

DIGEST-LENGTH is the fixnum count of bytes in the result string.

The phase procedures:

(INIT CONTEXT)

Initialization phase procedure. Sets up the CONTEXT, if necessary. The result is ignored.

(UPDATE CONTEXT BUFFER COUNT)

Accumulation phase procedure. Must accumulate the BUFFER, where BUFFER is a Scheme-object. Will be called one or more times. The result is ignored.

Where SOURCE is-a:

string
BUFFER = SOURCE.
blob
BUFFER = SOURCE.
procedure
updates with BUFFER = (procedure) until #f = (procedure).
input-port
like procedure above but from message-digest-chunk-reader.
object
BUFFER = (message-digest-chunk-converter SOURCE).

Where the actual number of bytes in the BUFFER can be determined COUNT will be a positive-integer. Otherwise COUNT is -1.

See message-digest-chunk-size to set the number of bytes read per update invocation.

See message-digest-chunk-reader to set the chunk reader procedure creator.

See message-digest-chunk-converter to set the chunk representation translation procedure.

(FINAL CONTEXT RESULT)

Finalization phase procedure. Must build the resulting message-digest in the supplied RESULT string of length DIGEST-LENGTH. The result is ignored.

The optional CALLER is for identification.

make-message-digest

[procedure] (make-message-digest SOURCE CONTEXT-INFO DIGEST-LENGTH INIT UPDATE FINAL [CALLER]) => STRING

Exactly as above but returns the message-digest for SOURCE using byte-string->hexadecimal.

message-digest-primitive

[procedure] (make-message-digest-primitive CONTEXT-INFO DIGEST-LENGTH INIT UPDATE FINAL ID)

Create a message-digest-primitive record object. The meaning of the fields are exactly as above. Predicates and accessors follow:

[procedure] (message-digest-primitive? OBJ)
[procedure] (message-digest-primitive-context-info PRIMITIVE)
[procedure] (message-digest-primitive-digest-length PRIMITIVE)
[procedure] (message-digest-primitive-init PRIMITIVE)
[procedure] (message-digest-primitive-update PRIMITIVE)
[procedure] (message-digest-primitive-final PRIMITIVE)
[procedure] (message-digest-primitive-name PRIMITIVE)

message-digest-primitive-apply

[procedure] (message-digest-primitive-apply MESSAGE-DIGEST-PRIMITIVE SOURCE [CALLER])

Returns a binary-message-digest of SOURCE using MESSAGE-DIGEST-PRIMITIVE.

Parameters

message-digest-chunk-size

[parameter] (message-digest-chunk-size [SIZE])

The number of bytes to read during the message-digest update phase. SIZE is a positive fixnum, default 1024.

message-digest-chunk-reader

[parameter] (message-digest-chunk-reader [READER-CREATOR])

The procedure used to create an input procedure.

[procedure] (READER-CREATOR INPUT-PORT) => PROCEDURE/0

The default READER-CREATOR will return a reader that takes from INPUT-PORT in (message-digest-chunk-size) bytes. The INPUT-PORT is treated as a binary-stream no matter how it is defined.

message-digest-chunk-converter

[parameter] (message-digest-chunk-converter [CONVERTER])

The procedure used to translate an arbitrary Scheme-object into something suitable for an UPDATE procedure. Should the CONVERTER be #f then no translation is attempted.

[procedure] (CONVERTER OBJECT) => OBJECT

The default CONVERTER will convert a heterogeneous-vector to a blob with shared-storage. No translation otherwise.

Auxillary Procedures

byte-string->substring-list

[procedure] (byte-string->substring-list STRING CHUNK-SIZE [START [END]]) => LIST

Returns a list of CHUNK-SIZE substrings of STRING, on the interval [START END). Defaults are [0 string-length).

Any remaining substring less than CHUNK-SIZE is appended to the list.

byte-string->substring-list/shared

[procedure] (byte-string->substring-list/shared STRING CHUNK-SIZE [START [END]]) => LIST

Returns a list of CHUNK-SIZE substrings of STRING, on the interval [START END). Defaults are [0 byte-string-length).

Any remaining substring less than CHUNK-SIZE is appended to the list.

The substrings can share storage with the STRING!

byte-string->hexadecimal

[procedure] (byte-string->hexadecimal STRING [START [END]]) => STRING

Returns a hex-encoded representation of STRING, on the interval [START END). Defaults are [0 byte-string-length).

Examples

Here's an example for creating a digest from the output of a procedure that expects to be called with an output port argument:

(use message-digest)

(define (call-with-output-digest primitive proc)
  (let* ((ctx-info (message-digest-primitive-context-info primitive))
         (update-digest (message-digest-primitive-update primitive))
         ;; Create new context, by allocating manually or calling the procedure
         (ctx (if (procedure? ctx-info) (ctx-info) (allocate ctx-info)))
	 ;; Create a procedure that updates the digest based on a string
         (update (lambda (str) (update-digest ctx str (string-length str))))
	 ;; Make a port that invokes the updater when written to
         (outport (make-output-port update void)))
    (handle-exceptions exn
      ;; If something goes wrong, clean up the context if we allocated it
      (begin (unless (procedure? ctx-info) (free ctx)) (signal exn))
      (let ((result (make-string
                     (message-digest-primitive-digest-length primitive))))
        ((message-digest-primitive-init primitive) ctx)
        (proc outport)
        ((message-digest-primitive-final primitive) ctx result)
	;; Don't forget to free the context if we allocated it
        (unless (procedure? ctx-info) (free ctx))
	;; Output is a byte string.  Convert to a hex number:
        (byte-string->hexadecimal result)))))

;; Usage:
(use md5)    ; Or sha1, or sha2, ...

(call-with-output-digest
  (md5-primitive)
  (lambda (out)
    (display "foo" out)))

Notes

Requirements

miscmacros check-errors

Bugs and Limitations

Author

kon lovett

Version history

2.0.1
Bug fix for (message-digest-chunk-converter) use by make-binary-message-digest.
2.0.0
Release for Chicken 4 [From a diff provided by Christian Kellermann]

License

 Copyright (C) 2009 Kon Lovett.  All rights reserved.
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the Software),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.