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

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

digest: simplify API.

SHA256 only working for PATHNAME objects at the moment.

File size: 5.0 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 (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)
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-digest
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  "Calculate digest with default of :SHA-256 pathname specified by URL.
97Returns an array of JVM primitive signed 8-bit bytes.
98
99Uses \"New I/O\" in JVM \"worse named API of all time\".
100
101*DIGEST-TYPES* controls the allowable digest types."
102  (format *debug-io* "~&pathname: ~S" url)
103
104 (let* ((digest-type (cdr (assoc digest *digest-types*)))
105        (digest (java:jstatic "getInstance" "java.security.MessageDigest" digest-type))
106        (namestring (if (pathnamep url) (namestring url) url))
107        (file-input-stream (java:jnew "java.io.FileInputStream" namestring))
108        (channel (java:jcall "getChannel" file-input-stream))
109        (length 8192)
110        (buffer (java:jstatic "allocateDirect" "java.nio.ByteBuffer" length))
111        (array (java:jnew-array "byte" length)))
112   (do ((read (java:jcall "read" channel buffer)
113              (java:jcall "read" channel buffer)))
114       ((not (> read 0)))
115     (java:jcall +byte-buffer-rewind+ buffer)
116     (java:jcall +byte-buffer-get+ buffer array 0 read)
117     (java:jcall +byte-buffer-rewind+ buffer)
118     (java:jcall +digest-update+ digest array 0 read))
119   (java:jcall "digest" digest)))
120
121(defmethod digest ((source string) &key (digest 'sha-256))
122  (declare (ignorable source digest))
123  (warn "Umimplemented.")
124  "deadbeef")
125         
126
127
128(export 'sha256 :system)
Note: See TracBrowser for help on using the repository browser.