| 1 | ;;; digest.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2012 Mark Evenson |
|---|
| 4 | ;;; $Id$ |
|---|
| 5 | |
|---|
| 6 | ;;; This program is free software; you can redistribute it and/or |
|---|
| 7 | ;;; modify it under the terms of the GNU General Public License |
|---|
| 8 | ;;; as published by the Free Software Foundation; either version 2 |
|---|
| 9 | ;;; of the License, or (at your option) any later version. |
|---|
| 10 | ;;; |
|---|
| 11 | ;;; This program is distributed in the hope that it will be useful, |
|---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | ;;; GNU General Public License for more details. |
|---|
| 15 | ;;; |
|---|
| 16 | ;;; You should have received a copy of the GNU General Public License |
|---|
| 17 | ;;; along with this program; if not, write to the Free Software |
|---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | ;;; |
|---|
| 20 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 21 | ;;; permission to link this library with independent modules to produce an |
|---|
| 22 | ;;; executable, regardless of the license terms of these independent |
|---|
| 23 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 24 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 25 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 26 | ;;; module. An independent module is a module which is not derived from |
|---|
| 27 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 28 | ;;; this exception to your version of the library, but you are not |
|---|
| 29 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 30 | ;;; exception statement from your version. |
|---|
| 31 | |
|---|
| 32 | (require :java) |
|---|
| 33 | (in-package :system) |
|---|
| 34 | |
|---|
| 35 | (defun asciify (digest) |
|---|
| 36 | (format nil "~{~2,'0X~}" |
|---|
| 37 | (mapcar (lambda (b) (if (< b 0) (+ 256 b) b)) |
|---|
| 38 | (java::list-from-jarray digest)))) |
|---|
| 39 | |
|---|
| 40 | ;;;; Really needs to concatenate all input into a single source of |
|---|
| 41 | ;;;; bytes, running digest over that concatentation. |
|---|
| 42 | (defun sha256 (&rest paths-or-strings) ;;; XXX more than one arg is very broken. |
|---|
| 43 | "Returned ASCIIfied representation of SHA256 digest of byte-based resource at PATHS-OR-STRINGs." |
|---|
| 44 | (unless (and (null (rest paths-or-strings)) |
|---|
| 45 | (pathnamep (first paths-or-strings))) |
|---|
| 46 | (warn "Unaudited computation of cryptographic digest initiated.")) ;; TODO Need tests with some tool for verification |
|---|
| 47 | (let ((first (first paths-or-strings)) |
|---|
| 48 | (rest (rest paths-or-strings))) |
|---|
| 49 | (concatenate 'string |
|---|
| 50 | (when first |
|---|
| 51 | (asciify |
|---|
| 52 | (typecase first |
|---|
| 53 | (pathname (digest first)) |
|---|
| 54 | (string (digest first)) |
|---|
| 55 | (null) |
|---|
| 56 | (list |
|---|
| 57 | (concatenate 'string |
|---|
| 58 | (sha256 (first first)) |
|---|
| 59 | (sha256 (rest first))))))) |
|---|
| 60 | (when rest |
|---|
| 61 | (sha256 rest))))) |
|---|
| 62 | |
|---|
| 63 | #+nil ;; Bugs out the compiler |
|---|
| 64 | (defun sha256 (paths-or-strings) |
|---|
| 65 | (labels ((walk (p-or-s) |
|---|
| 66 | ((atom p-or-s) |
|---|
| 67 | (typecase p-or-s |
|---|
| 68 | (pathname |
|---|
| 69 | (digest-path p-or-s)) |
|---|
| 70 | (string |
|---|
| 71 | (error "Somebody implement me please")))) |
|---|
| 72 | ((cons p-or-s) |
|---|
| 73 | (walk (first p-or-s) |
|---|
| 74 | (rest p-or-s))))) |
|---|
| 75 | (concatenate 'string |
|---|
| 76 | (walk paths-or-strings)))) |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | (defgeneric digest (resource &key (digest 'sha-256)) |
|---|
| 80 | (:documentation "Digest byte based resource at RESOURCE.")) |
|---|
| 81 | (defun digest-path (path) (asciify (digest path 'nio 'sha-256))) |
|---|
| 82 | |
|---|
| 83 | (defvar *digest-types* |
|---|
| 84 | '((sha-1 . "SHA-1") |
|---|
| 85 | (sha-256 . "SHA-256") |
|---|
| 86 | (sha-512 . "SHA-512")) |
|---|
| 87 | "Normalization of cryptographic digest naming.") |
|---|
| 88 | |
|---|
| 89 | ;;; Implementation |
|---|
| 90 | (defconstant +byte-buffer-rewind+ |
|---|
| 91 | (java:jmethod "java.nio.ByteBuffer" "rewind")) |
|---|
| 92 | (defconstant +byte-buffer-get+ |
|---|
| 93 | (java:jmethod "java.nio.ByteBuffer" "get" "[B" "int" "int")) |
|---|
| 94 | (defconstant +digest-update+ |
|---|
| 95 | (java:jmethod "java.security.MessageDigest" "update" "[B" "int" "int")) |
|---|
| 96 | |
|---|
| 97 | (defmethod digest ((url pathname) &key (digest 'sha-256)) |
|---|
| 98 | (digest-nio url :digest digest)) |
|---|
| 99 | |
|---|
| 100 | (defun digest-nio (source &key (digest 'sha-256)) |
|---|
| 101 | "Calculate digest with default of :SHA-256 pathname specified by URL. |
|---|
| 102 | Returns an array of JVM primitive signed 8-bit bytes. |
|---|
| 103 | |
|---|
| 104 | Uses \"New I/O\" in JVM \"worse named API of all time\". |
|---|
| 105 | |
|---|
| 106 | *DIGEST-TYPES* controls the allowable digest types." |
|---|
| 107 | (let* |
|---|
| 108 | ((channel (typecase source |
|---|
| 109 | (pathname |
|---|
| 110 | (java:jcall "getChannel" (java:jnew "java.io.FileInputStream" |
|---|
| 111 | (namestring source)))) |
|---|
| 112 | (string |
|---|
| 113 | (java:jstatic "newChannel" "java.nio.channels.Channels" |
|---|
| 114 | (java:jnew "java.io.ByteArrayInputStream" |
|---|
| 115 | (java:jcall "getBytes" source)))) |
|---|
| 116 | (error "Typecase failed of object of type ~S." source))) |
|---|
| 117 | (digest-type (cdr (assoc digest *digest-types*))) |
|---|
| 118 | (digest (java:jstatic "getInstance" "java.security.MessageDigest" digest-type)) |
|---|
| 119 | (length 8192) |
|---|
| 120 | (buffer (java:jstatic "allocateDirect" "java.nio.ByteBuffer" length)) |
|---|
| 121 | (array (java:jnew-array "byte" length))) |
|---|
| 122 | (do ((read (java:jcall "read" channel buffer) |
|---|
| 123 | (java:jcall "read" channel buffer))) |
|---|
| 124 | ((not (> read 0))) |
|---|
| 125 | (java:jcall +byte-buffer-rewind+ buffer) |
|---|
| 126 | (java:jcall +byte-buffer-get+ buffer array 0 read) |
|---|
| 127 | (java:jcall +byte-buffer-rewind+ buffer) |
|---|
| 128 | (java:jcall +digest-update+ digest array 0 read)) |
|---|
| 129 | (java:jcall "digest" digest))) |
|---|
| 130 | |
|---|
| 131 | (defmethod digest ((source string) &key (digest 'sha-256)) |
|---|
| 132 | (digest-nio source :digest digest)) |
|---|
| 133 | |
|---|
| 134 | (export 'sha256 :system) |
|---|