source: trunk/abcl/src/org/armedbear/lisp/digest.lisp @ 14202

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

SYS:SHA256 now hashes strings passed its way.

TODO: develop a "lazy serialization" so that all of the arguments of
SHA256 will have the underlying java.nio.Channel objects ready t obe
run through the digest function.

Enough for an implementation of renaming dynamic callbacks.

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