MD5 for Common Lisp (Lispworks)

MD5.lisp is an implementation of the MD5 message-digest algorithm and the Hashed Message Authentication digest (HMAC) using the MD5 digest algorithm in Common Lisp. It requires no additional libraries, all the needed code is included in one file.

Download

Click here to download MD5.lisp (ver:20131106, 11Kb)

Example of usage

(load "md5.lisp")

(md5:digest #(1 2 3))
(md5:digest "The quick brown fox jumps over the lazy dog.")
(md5:digest-hex #(1 2 3))
(md5:digest-hex "The quick brown fox jumps over the lazy dog.")

(md5:hmac #(1 2 3) #(1 2 3))
(md5:hmac "key" "The quick brown fox jumps over the lazy dog")
(md5:hmac-hex #(1 2 3) #(1 2 3))
(md5:hmac-hex "key" "The quick brown fox jumps over the lazy dog")

Limitations

During the computation it creates a vector of the same size as the original message and fills it byte by byte. Because of this fact the library works best for messages of small and medium size strings and vectors; it is not advisable to use this library if you need speed and memory efficiency in computing digests for very large messages.

Programming interface

MD5.lisp defines MD5 package and exports some symbols as a public interface.

Symbols available in the MD5 package:
digest, digest-hex, hmac, hmac-hex

[function]
digest arg => digest

Calculates the MD5 digest of the data given in a string or a vector. Returns a vector of bytes with the MD5 digest.

arg   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
digest    MD5 digest in a form of (unsigned-byte 8) vector.

[function]
digest-hex arg => digest

Calculates the MD5 digest of the data given in a string or a vector. Returns a hexadecimal encoded version of the digest as a string.

arg   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
digest    a string with a hexadecimal encoded version of the MD5 digest.

[function]
hmac key message => digest

Calculate an HMAC digest using the MD5 digest algorithm. Key and message are strings or vectors. Returns a vector of bytes with the hmac digest.

key   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
message   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
digest    HMAC digest in a form of (unsigned-byte 8) vector.

[function]
hmac-hex key message => digest

Calculate an HMAC digest using the MD5 digest algorithm. Key and message are strings or vectors. Returns a hexadecimal encoded version of the digest as a string.

key   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
message   a string or a vector of (unsigned-byte 8) type. If a string is given then the string is converted first into a vector using the utf-8 encoding.
digest    a string with a hexadecimal encoded version of the HMAC digest.

Feedback

Email to artobrezan[at]yahoo.com

License

Copyright (c) 2013, Art Obrezan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Use in source and binary forms are prohibited in projects under GNU General Public Licenses and its derivatives.

THIS SOFTWARE IS PROVIDED BY ART OBREZAN ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ART OBREZAN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.