message-digest-utils

  1. message-digest-utils
  2. Documentation
    1. Message Digest Byte Vector
      1. Usage
      2. message-digest-update-blob
      3. message-digest-update-string
      4. message-digest-string
      5. message-digest-blob
      6. message-digest-string!
      7. message-digest-blob!
      8. message-digest-update-substring (DEPRECATED)
    2. Message Digest Int
      1. Usage
      2. message-digest-update-char-u8
      3. message-digest-update-char
      4. message-digest-update-char-be
      5. message-digest-update-char-le
      6. message-digest-update-u8
      7. message-digest-update-u16
      8. message-digest-update-u16-be
      9. message-digest-update-u16-le
      10. message-digest-update-u32
      11. message-digest-update-u32-be
      12. message-digest-update-u32-le
      13. message-digest-update-u64
      14. message-digest-update-u64-be
      15. message-digest-update-u64-le
    3. Message Digest Update Item
      1. Usage
      2. message-digest-update-file
      3. message-digest-update-procedure
      4. message-digest-update-port
      5. message-digest-update-object
    4. Message Digest Item
      1. Usage
      2. message-digest-object
      3. message-digest-file
      4. message-digest-port
      5. message-digest-object!
      6. message-digest-file!
      7. message-digest-port!
    5. Message Digest SRFI 4
      1. Usage
      2. message-digest-update-u8vector
      3. message-digest-u8vector
      4. message-digest-u8vector!
      5. message-digest-update-subu8vector (DEPRECATED)
      6. message-digest-update-bytevector (DEPRECATED)
    6. Message Digest Port
      1. Usage
      2. Common Argument Definitions
      3. digest-output-port
      4. open-output-digest
      5. get-output-digest
      6. call-with-output-digest
      7. with-output-to-digest
    7. Message Digest Chunk
      1. Usage
      2. message-digest-chunk-port-read-maker
      3. message-digest-chunk-fileno-read-maker
      4. message-digest-chunk-size
      5. message-digest-chunk-converter
  3. Examples
  4. Bugs and Limitations
  5. Notes
  6. Requirements
  7. Author
  8. Repository
  9. Version history
  10. License

Documentation

Message Digest provides support for message digest primitives. A message-digest is a function taking some input source and returning a fixed-length hash.

For best results the source object(s) to be accumulated into the digest should be something easily treated as a bytevector.

Message Digest Byte Vector

Digest routines for string & blob.

Usage

(import message-digest-byte-vector)

message-digest-update-blob

[procedure] (message-digest-update-blob DIGEST BLOB [START [END]])

Update the DIGEST with a BLOB, optionally sliced by START END.

message-digest-update-string

[procedure] (message-digest-update-string DIGEST STRING [START [END]])

Update the DIGEST with a STRING, optionally sliced by START END.

message-digest-string

[procedure] (message-digest-string PRIM STRING [RESULT-FORM [START [END]]]) -> message-digest-result-type

Returns the RESULT for the digest algorithm PRIM applied to STRING, optionally sliced by START END, in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

message-digest-blob

[procedure] (message-digest-blob PRIM BLOB [RESULT-FORM [START [END]]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to BLOB, optionally sliced by START END, in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

message-digest-string!

[procedure] (message-digest-string! PRIM STRING BUFFER [START [END]]) -> message-digest-result-type

Returns the RESULT for the digest algorithm PRIM applied to STRING, optionally sliced by START END, in the BUFFER.

message-digest-blob!

[procedure] (message-digest-blob! PRIM BLOB BUFFER [START [END]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to BLOB, optionally sliced by START END, in the {BUFFER}.

message-digest-update-substring (DEPRECATED)

[procedure] (message-digest-update-substring DIGEST STRING START END)

Update the DIGEST with a substring STRING START END.

Message Digest Int

Provides digest update operations for character and integer datatypes.

Usage

(import message-digest-int)

message-digest-update-char-u8

[procedure] (message-digest-update-char-u8 DIGEST CHAR)

Update the DIGEST with the low-order 8-bits of a character CHAR.

message-digest-update-char

[procedure] (message-digest-update-char DIGEST CHAR [ENDIAN])

Update the DIGEST with a the character CHAR 32-bit integer value treated as ENDIAN.

ENDIAN default is (machine-byte-order).

message-digest-update-char-be

[procedure] (message-digest-update-char-be DIGEST CHAR)

Update the DIGEST with a the character CHAR 32-bit integer value treated as big-endian.

message-digest-update-char-le

[procedure] (message-digest-update-char-le DIGEST CHAR)

Update the DIGEST with a the character CHAR 32-bit integer value treated as little-endian.

message-digest-update-u8

[procedure] (message-digest-update-u8 DIGEST U8)

Update the DIGEST with an 8-bit integer U8.

message-digest-update-u16

[procedure] (message-digest-update-u16 DIGEST U16 [ENDIAN])

Update the DIGEST with a 16-bit integer U16 treated as ENDIAN.

ENDIAN default is (machine-byte-order).

message-digest-update-u16-be

[procedure] (message-digest-update-u16-be DIGEST U16)

Update the DIGEST with a 16-bit integer U16 treated as big-endian.

message-digest-update-u16-le

[procedure] (message-digest-update-u16-le DIGEST U16)

Update the DIGEST with a 16-bit integer U16 treated as little-endian.

message-digest-update-u32

[procedure] (message-digest-update-u32 DIGEST U32 [ENDIAN])

Update the DIGEST with a 32-bit integer U32 treated as ENDIAN.

ENDIAN default is (machine-byte-order).

message-digest-update-u32-be

[procedure] (message-digest-update-u32-be DIGEST U32)

Update the DIGEST with a 32-bit integer U32 treated as big-endian.

message-digest-update-u32-le

[procedure] (message-digest-update-u32-le DIGEST U32)

Update the DIGEST with a 32-bit integer U32 treated as little-endian.

message-digest-update-u64

[procedure] (message-digest-update-u64 DIGEST U64 [ENDIAN])

Update the DIGEST with a 64-bit integer U64 treated as ENDIAN.

ENDIAN default is (machine-byte-order).

message-digest-update-u64-be

[procedure] (message-digest-update-u64-be DIGEST U64)

Update the DIGEST with a 64-bit integer U64 treated as big-endian.

message-digest-update-u64-le

[procedure] (message-digest-update-u64-le DIGEST U64)

Update the DIGEST with a 64-bit integer U64 treated as little-endian.

Message Digest Update Item

Provides digest update operations for Scheme objects.

Usage

(import message-digest-update-item)

message-digest-update-file

[procedure] (message-digest-update-file DIGEST FILENAME)

Update the DIGEST with the contents of file FILENAME.

message-digest-update-procedure

[procedure] (message-digest-update-procedure DIGEST THUNK)

Update the DIGEST with the THUNK result, until it returns #f.

THUNK
(-> (or BYTE-SOURCE false))
BYTE-SOURCE
(or string blob srfi-4-vector )
;simple example (from test suite)
(import message-digest-item)
(: just-once (-> (or string false)))
(define just-once
  ;override most specific inferred type, true, w/ what is needed, boolean
  (let ((flag (the boolean #t)))
    (lambda ()
      (and flag (begin (set! flag #f) "ab cd")))))

message-digest-update-port

[procedure] (message-digest-update-port DIGEST INPUT-PORT)

Update the DIGEST with byte-source from an INPUT-PORT until #!eof encountered.

Uses the message-digest-chunk-read-maker to create a reader for the port.

message-digest-update-object

[procedure] (message-digest-update-object DIGEST SOURCE [START [END]])

Update the DIGEST with some SOURCE.

SOURCE maybe

input-port
as in message-digest-update-port
procedure
as in message-digest-update-procedure
string
blob
srfi-4-vector
*
((message-digest-chunk-converter) SOURCE) -> byte-source.

If START END supplied, and possible, the byte-source is sliced.

Message Digest Item

Provides digest operations for whole Scheme objects.

Usage

(import message-digest-item)

message-digest-object

[procedure] (message-digest-object PRIM SOURCE [RESULT-FORM [START [END]]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to SOURCE, optionally sliced by START END, in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

message-digest-file

[procedure] (message-digest-file PRIM FILENAME [RESULT-FORM]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to the file FILENAME in the RESULT-FORM. Reads until #!eof encountered.

RESULT-FORM default is (message-digest-result-form).

message-digest-port

[procedure] (message-digest-port PRIM INPUT-PORT [RESULT-FORM]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to INPUT-PORT in the RESULT-FORM. Reads until #!eof encountered.

RESULT-FORM default is (message-digest-result-form).

message-digest-object!

[procedure] (message-digest-object! PRIM SOURCE BUFFER [START [END]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to SOURCE, optionally sliced by START END, in the BUFFER.

message-digest-file!

[procedure] (message-digest-file! PRIM FILENAME BUFFER) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to the file FILENAME in the BUFFER. Reads until #!eof encountered.

message-digest-port!

[procedure] (message-digest-port! PRIM INPUT-PORT BUFFER) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to INPUT-PORT in the BUFFER. Reads until #!eof encountered.

Message Digest SRFI 4

Provides digest operations for SRFI-4 packed-vectors.

Usage

(import message-digest-srfi-4)

message-digest-update-u8vector

[procedure] (message-digest-update-u8vector DIGEST U8VECTOR [START [END]])

Update the DIGEST with a U8VECTOR, optionally sliced by START END.

message-digest-u8vector

[procedure] (message-digest-u8vector PRIM U8VECTOR [RESULT-FORM [START [END]]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to U8VECTOR, optionally sliced by START END, in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

message-digest-u8vector!

[procedure] (message-digest-u8vector! PRIM U8VECTOR BUFFER [START [END]]) -> message-digest-result-type

Returns the result for the digest algorithm PRIM applied to U8VECTOR, optionally sliced by START END, in the BUFFER.

message-digest-update-subu8vector (DEPRECATED)

[procedure] (message-digest-update-subu8vector DIGEST U8VECTOR START END)

Update the DIGEST with a subvector U8VECTOR START END.

message-digest-update-bytevector (DEPRECATED)

[procedure] (message-digest-update-bytevector DIGEST BYTEVECTOR [LENGTH])

Update the DIGEST with the BYTEVECTOR, a blob, string, or srfi-4-vector.

The LENGTH is the byte count. Default is the size in bytes of the BYTEVECTOR.

Message Digest Port

Provides a port abstraction for a message-digest-primitive.

Usage

(import message-digest-port)

Common Argument Definitions

PORT is a digest-output-port.

digest-output-port

[procedure] (digest-output-port? OBJ) -> boolean
[procedure] (check-digest-output-port LOC OBJ [NAM])
[procedure] (error-digest-output-port LOC OBJ [NAM])
[procedure] (digest-output-port-name PORT) -> string

open-output-digest

[procedure] (open-output-digest PRIM) -> digest-output-port

Returns a message digest output port for the supplied algorithm PRIM.

The initialization phase.

get-output-digest

[procedure] (get-output-digest PORT [RESULT-FORM]) -> string

Closes the PORT and returns the result as a RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

The finalization phase.

call-with-output-digest

[procedure] (call-with-output-digest PRIM PROC [RESULT-FORM]) -> message-digest-result-type

Returns the result of the call of PROC with an open-output-digest for PRIM in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

with-output-to-digest

[procedure] (with-output-to-digest PRIM THUNK [RESULT-FORM]) -> message-digest-result-type

Invoke the procedure THUNK with (current-output-port) bound to a digest-output-port and return in the RESULT-FORM.

RESULT-FORM default is (message-digest-result-form).

Message Digest Chunk

An inchoate Chunking API.

Usage

(import message-digest-chunk)

message-digest-chunk-port-read-maker

[parameter] (message-digest-chunk-port-read-maker [CTOR]) -> procedure

Supplies the procedure used to create an input procedure.

CTOR
(PORT #!optional SIZE) -> (-> byte-source)
PORT
source open input port
SIZE
chunk size, default (message-digest-chunk-size)

The default CTOR returns a reader from PORT in SIZE bytes.

message-digest-chunk-fileno-read-maker

[parameter] (message-digest-chunk-fileno-read-maker [CTOR]) -> procedure

Supplies the procedure used to create an input procedure.

CTOR
(FD #!optional TOTAL SIZE) -> (-> byte-source)
FD
source open fileno
TOTAL
total size, default (file-size FD))
SIZE
chunk size, default (message-digest-chunk-size)

The default CTOR returns a reader conditioned on memory-mapped-file support.

message-digest-chunk-size

[parameter] (message-digest-chunk-size [SIZE]) -> positive-integer

The number of bytes to read from a binary-stream during the message-digest update phase. Used by the default message-digest-chunk-read-maker.

SIZE
positive-integer, default 1024

message-digest-chunk-converter

[parameter] (message-digest-chunk-converter [CONV]) -> (or #f procedure)

The procedure used to translate an arbitrary object into something suitable for an UPDATE procedure. See make-message-digest-primitive.

CONV
(* -> byte-source) or #f, default #f

Should the CONV be #f or return #f then no translation is attempted.

Examples

Uses the message-digest port abstraction to get an MD5 digest of a string:

(import message-digest-port md5) ; Or sha1, or sha2, ...

(call-with-output-digest (md5-primitive) (cut display "foo" <>))
;=> "acbd18db4cc2f85cedef654fccc4a4d8"

Bugs and Limitations

Notes

;macOS 10.15.6 w/ 3.2 GHz 6-Core Intel Core i7

;csc md-v-simple.scm -Dfile -Dsimple -o simple-file
;csc md-v-simple.scm -Dstring -Dsimple -o simple-string
;csc md-v-simple.scm -Dfile -Dmd -o md-file
;csc md-v-simple.scm -Dstring -Dmd -o md-string

;./simple-file
;=> Simple = 90bbbcbab37cde02d31e2955f1ab9e17becab506
;=> 2.207s CPU time, 4316/17 mutations (total/tracked), 0/2 GCs (major/minor), maximum live heap: 328.82 KiB

;./md-file
;=> MD = 90bbbcbab37cde02d31e2955f1ab9e17becab506
;=> 2.189s CPU time, 223/208 mutations (total/tracked), 0/1 GCs (major/minor), maximum live heap: 391.99 KiB

;./simple-string
;=> Simple = c62af4d8bd70d82bca1081f59f5372628f4742f5
;=> 0.053s CPU time, 431200/1688 mutations (total/tracked), 1/165 GCs (major/minor), maximum live heap: 330.45 KiB

;./md-string
;=> MD = c62af4d8bd70d82bca1081f59f5372628f4742f5
;=> 0.051s CPU time, 0.001s GC time (major), 37529/27733 mutations (total/tracked), 13/133 GCs (major/minor), maximum live heap: 414.04 KiB

(import scheme (chicken base) (chicken time) miscmacros)

(cond-expand
  (simple
    (import simple-sha1)
    (define *source* "Simple")
    (define-syntax digest-string (syntax-rules () ((digest-string ?str) (string->sha1sum ?str))))
    (define-syntax digest-file (syntax-rules () ((digest-file ?fl) (sha1sum ?fl)))))
  (md
    (import sha1 message-digest-byte-vector message-digest-item)
    (define *source* "MD")
    (define *sha1* (sha1-primitive))
    (define-syntax digest-string (syntax-rules () ((digest-string ?str) (message-digest-string *sha1* ?str))))
    (define-syntax digest-file (syntax-rules () ((digest-file ?fl) (message-digest-file *sha1* ?fl))))))

(cond-expand
  (string
    (define *test-data* (make-string 1627 #\x))
    (define *reps* 10000)
    (define-syntax digest-data (syntax-rules () ((digest-data ?dat) (digest-string ?dat)))))
  (file
    ;11MB File
    (define *test-data* "The Periodic Table of Irrational Nonsense.jpg")
    (define *reps* 100)
    (define-syntax digest-data (syntax-rules () ((digest-data ?dat) (digest-file ?dat))))))

(print *source* " = " (digest-data *test-data*))
(time (repeat *reps* (digest-data *test-data*)))

Requirements

check-errors blob-utils string-utils memory-mapped-files message-digest-primitive message-digest-type

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/message-digest-utils

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

4.2.0
Add message-digest.types.scm.
4.1.2
.
4.0.0
CHICKEN 5 release.

License

 Copyright (C) 2006-2020 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.