hash-algorithms#

The hash-algorithms library provides consistent access to a variety of hash algorithms:

  • md5

  • sha1

  • sha224

  • sha256

  • sha384

  • sha512

Hashing an entire string at once can be done via the functions named after each hash:

sha1("Some text");  // => {<simple-byte-vector> sequence 2, 217, 44, 88, 13, 78, ... }

If you want a printable digest, use hexdigest(<byte-vector>):

hexdigest(sha1("Some text"))
// => "02D92C580D4EDE6C80A878BDD9F3142D8F757BE8"

If you want to hash multiple strings into a single digest (useful when streaming), you can use the update-hash and digest functions:

let hash = make(<sha1>);
update-hash(hash, "Some");
update-hash(hash, " ");
update-hash(hash, "text");
digest(hash);     // => {<simple-byte-vector> sequence 2, 217, 44, 88, 13, 78, 222, ...}
hexdigest(hash);  // => "EA1C208EFBF629C89F52D165045B1716EA8EB012"

The hash-algorithms Module#

<hash> Class#
Superclasses:

<object>

digest-size Generic function#

Returns the digest size of the hash algorithm.

Signature:

digest-size (hash) => (digest-size)

Parameters:
  • hash – An instance of <hash>.

Values:
block-size Generic function#

Returns the block size of the hash algorithm.

Signature:

block-size (hash) => (block-size)

Parameters:
  • hash – An instance of <hash>.

Values:
update-hash Generic function#

Add more data to the hash.

Signature:

update-hash (hash, input) => ()

Parameters:
  • hash – An instance of <hash>.

  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Discussion:

Add more data to the hash. This is useful when streaming data or the data is available in multiple strings and you wish to avoid the overhead of concatenation.

Calling update-hash multiple times is equivalent to calling it once with a concatenation of the arguments:

let hash-separate = make(<sha1>);
update-hash(hash-separate, "Some");
update-hash(hash-separate, " ");
update-hash(hash-separate, "text");
digest(hash-separate);
// => {<simple-byte-vector> sequence 2, 217, 44, 88, 13, 78, 222, ... }

let hash-combined = make(<sha1>);
update-hash(hash-combined, "Some text");
digest(hash-combined);
// => {<simple-byte-vector> sequence 2, 217, 44, 88, 13, 78, 222, ... }

See also:

digest Generic function#
Signature:

digest (hash) => (digest)

Parameters:
  • hash – An instance of <hash>.

Values:
  • digest – An instance of <byte-vector>.

Discussion:

The return value digest is binary data and may include null bytes. To display this result in text form, use hexdigest(<hash>) or hexdigest(<byte-vector>).

Use update-hash to add data to the hash.

See also:

hexdigest(<hash>) Method#

Returns the digest for the given hash as a hexadecimal string.

Signature:

hexdigest (hash) => (hexdigest)

Parameters:
  • hash – An instance of <hash>.

Values:
See also:

hexdigest(<byte-vector>) Method#

Returns the digest given as a hexadecimal string.

Signature:

hexdigest (digest) => (hexdigest)

Parameters:
  • digest – An instance of <byte-vector>.

Values:
See also:

MD5#

<md5> Class#
Superclasses:

<hash>

md5 Function#
Signature:

md5 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.

SHA-1#

<sha1> Class#
Superclasses:

<hash>

sha1 Function#
Signature:

sha1 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.

SHA-2#

<sha256> Class#
Superclasses:

<hash>

sha256 Function#
Signature:

sha256 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.

<sha224> Class#
Superclasses:

<hash>

sha224 Function#
Signature:

sha224 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.

<sha384> Class#
Superclasses:

<hash>

sha384 Function#
Signature:

sha384 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.

<sha512> Class#
Superclasses:

<hash>

sha512 Function#
Signature:

sha512 (input) => (digest)

Parameters:
  • input – An instance of <byte-string>, <buffer> or <byte-vector>.

Values:
  • digest – An instance of <byte-vector>.