Changeset 14202


Ignore:
Timestamp:
10/16/12 08:54:42 (8 years ago)
Author:
Mark Evenson
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/digest.lisp

    r14200 r14202  
    3333(in-package :system)
    3434
    35 (defun asciify-digest (digest)
     35(defun asciify (digest)
    3636  (format nil "~{~X~}"
    3737          (mapcar (lambda (b) (if (< b 0) (+ 256 b) b))
     
    4141;;;; Really needs to concatenate all input into a single source of
    4242;;;; bytes, running digest over that concatentation
    43 (defun sha256 (&rest paths-or-strings)
     43(defun sha256 (&rest paths-or-strings) ;;; XXX more than one arg is very broken.
    4444  "Returned ASCIIfied representation of SHA256 digest of byte-based resource at PATHS-OR-STRINGs."
    4545  (let ((first (first paths-or-strings))
     
    4747    (concatenate 'string
    4848                  (when first
    49                     (asciify-digest
     49                    (asciify
    5050                     (typecase first
    5151                       (pathname (digest first))
     
    9494
    9595(defmethod digest ((url pathname) &key (digest 'sha-256))
     96  (digest-nio url :digest digest))
     97
     98(defun digest-nio (source &key (digest 'sha-256))
    9699  "Calculate digest with default of :SHA-256 pathname specified by URL.
    97100Returns an array of JVM primitive signed 8-bit bytes.
     
    100103
    101104*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)
     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)
    113121              (java:jcall "read" channel buffer)))
    114122       ((not (> read 0)))
     
    120128
    121129(defmethod digest ((source string) &key (digest 'sha-256))
    122   (declare (ignorable source digest))
    123   (warn "Umimplemented.")
    124   "deadbeef")
    125          
    126 
     130    (digest-nio source :digest digest))
    127131
    128132(export 'sha256 :system)
Note: See TracChangeset for help on using the changeset viewer.