SHA1 for Common Lisp (Lispworks)

SHA-1 (Secure Hash Algorithm 1) and Hashed Message Authentication digest (HMAC) implementations in generic Common Lisp, not Lispworks specific. It requires no additional libraries, all the needed code is included in one file.

Download

sha1.lisp  (8JUL2023, 10KB)

Example of usage

(load "sha1.lisp")

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

(sha1:hmac #(1 2 3) #(1 2 3))
(sha1:hmac "key" "The quick brown fox jumps over the lazy dog")
(sha1:hmac-hex #(1 2 3) #(1 2 3))
(sha1: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

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

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

[function]
digest arg => digest

Calculates the SHA1 digest of the data given in a string or a vector. Returns a vector of bytes with the SHA1 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  SHA1 digest in a form of (unsigned-byte 8) vector.


[function]
digest-hex arg => digest

Calculates the SHA1 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 SHA1 digest.


[function]
hmac key message => digest

Calculate an HMAC digest using the SHA1 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 SHA1 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.

License

Copyright (c) 2013-2023, 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.