;; @module crypto.lsp
;; @description SSL crypto functions for MD5 and SHA-1 hashing
;; @version 1.01 - initial release
;; @version 1.02 - renamed to crypto, new lib detection
;; @author Lutz Mueller, 2007
;;
;; <h2>Module for SSL lib crypto bindings</h2>
;; This modules imports functions for the MD5 and SHA-1 hashing algorithms described
;; here: @link http://www.ietf.org/rfc/rfc3174.txt http://www.ietf.org/rfc/rfc3174.txt .
;; The crypto library is part of the @link http://www.openssl.org/ OpenSSL libraries.
;;
;; To use this module include the following 'load' statement at the
;; beginning of the program file:
;; <pre>
;; (load "/usr/local/share/newlisp/crypto.lsp")
;; </pre>
;;
;; <h2>Requirements:</h2>
;; On Mac OS X, UBUNTU and many other Linux, BSDs and other UNIX installations
;; <tt>libcrypto.so</tt> is installed by default as part of the OpenSSL
;; libraries in <tt>usr/lib/libcrypto.so</tt>. If loading this module
;; finishes with an error message the path of the library should be corrected.
;;
;; This module has been tested on Mac OS X, UBUNTU Linux and FreeBSD.
(context 'crypto)
; set library to path-name of the library on your platform OS
;
(set 'files '(
"/usr/lib/libcrypto.so"
"/usr/lib/libcrypto.so.0.9.8"
"/usr/lib/libcrypto.so.0.9.7"
"/usr/lib/libcrypto.so.0.9.6"
"/usr/lib/libcrypto.so.4"
"/usr/lib/libcrypto.dylib"
))
(set 'library (files (or
(find true (map file? files))
(begin (println "cannot find crypto library") (exit)))))
(import library "MD5")
(import library "SHA1")
;; @syntax (crypto:md5 <string>)
;; @param <string> the string buffer for which to calculate a MD5 hash
;; @return the 16 Byte MD5 hash as a 32 Byte long hex string
;;
;; @example
;; (crypto:md5 "ABC") => "902fbdd2b1df0c4f70b4a5d23525e932"
;;
;; (crypto:md5 (read-file "newlisp-9.1.0.tgz")) => "46c79c93e904df35c6a8474ace406c92"
(define (md5 str)
(join
(map (lambda (x) (format "%02x" (& x 0xff)))
(unpack (dup "c" 16) (MD5 str (length str) 0))))
)
;; @syntax (crypto:sha1 <string>)
;; @param <string> the string buffer for which to calculate a SHA-1 hash
;; @return the 20 Byte SHA-1 hash as a 40 Byte long hex string
;;
;; @example
;; (crypto:sha1 "ABC") => "3c01bdbb26f358bab27f267924aa2c9a03fcfdb8"
;;
;; (crypto:sha1 (read-file "newlisp-9.1.0.tgz")) => "2127a9c487f338b00f36cfd60b5f33d27b8d0010"
(define (sha1 str)
(join
(map (lambda (x) (format "%02x" (& x 0xff)))
(unpack (dup "c" 20) (SHA1 str (length str) 0))))
)
; eof ;
syntax highlighted by Code2HTML, v. 0.9.1