source: branches/1.1.x/src/org/armedbear/lisp/digest.lisp @ 14308

Last change on this file since 14308 was 14308, checked in by Mark Evenson, 8 years ago

Backport r14307 | mevenson | 2012-12-07 11:44:09 +0100 (Fri, 07 Dec 2012) | 1 line
Backport r14306 | mevenson | 2012-12-07 11:38:15 +0100 (Fri, 07 Dec 2012) | 1 line

digest: spellcheck emitted warning.

digest: warn that digest implementation does not currently have valid tests.

File size: 5.5 KB
Line 
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 "~{~X~}"
37          (mapcar (lambda (b) (if (< b 0) (+ 256 b) b))
38                  (java::list-from-jarray digest))))
39
40
41;;;; Really needs to concatenate all input into a single source of
42;;;; bytes, running digest over that concatentation
43(defun sha256 (&rest paths-or-strings) ;;; XXX more than one arg is very broken.
44  "Returned ASCIIfied representation of SHA256 digest of byte-based resource at PATHS-OR-STRINGs." 
45  (warn "Unaudited computatation of cryptographic digest initiated.") ;; TODO Need tests with some tool for verification
46  (let ((first (first paths-or-strings))
47        (rest (rest paths-or-strings)))
48    (concatenate 'string 
49                  (when first
50                    (asciify
51                     (typecase first
52                       (pathname (digest first))
53                       (string (digest first))
54                       (null)
55                       (list
56                        (concatenate 'string 
57                                     (sha256 (first first))
58                                     (sha256 (rest first)))))))
59                  (when rest
60                    (sha256 rest)))))
61                       
62#+nil ;; Bugs out the compiler
63(defun sha256 (paths-or-strings)   
64  (labels ((walk (p-or-s)
65             ((atom p-or-s)
66              (typecase p-or-s 
67                (pathname
68                 (digest-path p-or-s))
69                (string 
70                 (error "Somebody implement me please"))))
71             ((cons p-or-s)
72              (walk (first p-or-s)
73                    (rest p-or-s)))))
74         (concatenate 'string
75                      (walk paths-or-strings))))
76
77           
78(defgeneric digest (resource &key (digest 'sha-256))
79  (:documentation "Digest byte based resource at RESOURCE."))
80(defun digest-path (path) (asciify-digest (digest path 'nio 'sha-256)))
81
82(defvar *digest-types* 
83  '((sha-1 . "SHA-1")
84    (sha-256 . "SHA-256")
85    (sha-512 . "SHA-512"))
86  "Normalization of cryptographic digest naming.")
87
88;;; Implementation
89(defconstant +byte-buffer-rewind+ 
90  (java:jmethod "java.nio.ByteBuffer" "rewind"))
91(defconstant +byte-buffer-get+ 
92  (java:jmethod "java.nio.ByteBuffer" "get" "[B" "int" "int"))
93(defconstant +digest-update+ 
94  (java:jmethod "java.security.MessageDigest" "update" "[B" "int" "int"))
95
96(defmethod digest ((url pathname) &key (digest 'sha-256))
97  (digest-nio url :digest digest))
98
99(defun digest-nio (source &key (digest 'sha-256))
100  "Calculate digest with default of :SHA-256 pathname specified by URL.
101Returns an array of JVM primitive signed 8-bit bytes.
102
103Uses \"New I/O\" in JVM \"worse named API of all time\".
104
105*DIGEST-TYPES* controls the allowable digest types."
106  (let* 
107      ((channel (typecase source
108                    (pathname 
109                     (java:jcall "getChannel" (java:jnew "java.io.FileInputStream" 
110                                                       (namestring source))))
111                     (string 
112                      (java:jstatic "newChannel" "java.nio.channels.Channels" 
113                                    (java:jnew "java.io.ByteArrayInputStream" 
114                                               (java:jcall "getBytes" source))))
115                   (error "Typecase failed of object of type ~S." source)))
116       (digest-type (cdr (assoc digest *digest-types*)))
117       (digest (java:jstatic "getInstance" "java.security.MessageDigest" digest-type))
118       (length 8192)
119       (buffer (java:jstatic "allocateDirect" "java.nio.ByteBuffer" length))
120       (array (java:jnew-array "byte" length)))
121  (do ((read (java:jcall "read" channel buffer)
122              (java:jcall "read" channel buffer)))
123       ((not (> read 0)))
124     (java:jcall +byte-buffer-rewind+ buffer)
125     (java:jcall +byte-buffer-get+ buffer array 0 read)
126     (java:jcall +byte-buffer-rewind+ buffer)
127     (java:jcall +digest-update+ digest array 0 read))
128   (java:jcall "digest" digest)))
129
130(defmethod digest ((source string) &key (digest 'sha-256))
131    (digest-nio source :digest digest))
132
133(export 'sha256 :system)
Note: See TracBrowser for help on using the repository browser.