chiark / gitweb /
zone.lisp: New utility for hashing files.
[zone] / zone.lisp
index 28ee038486bf4b5149878903a4e16c83283c43d5..177ded6b292c7601a2befb06f1135906cb43c3d0 100644 (file)
--- a/zone.lisp
+++ b/zone.lisp
@@ -202,6 +202,19 @@ (defun mapenum (func name)
   "Call FUNC on TAG/VALUE pairs from the enumeration called NAME."
   (maphash func (get name 'enum-forward)))
 
+(defun hash-file (hash file context)
+  "Hash the FILE using the OpenSSL HASH function, returning an octet string.
+
+   CONTEXT is a temporary-files context."
+  (let ((temp (temporary-file context "hash")))
+    (run-program (list "openssl" "dgst" (concatenate 'string "-" hash))
+                :input file :output temp)
+    (with-open-file (in temp)
+      (let ((line (read-line in)))
+       (assert (and (>= (length line) 9)
+                    (string= line "(stdin)= " :end1 9)))
+       (decode-hex line :start 9)))))
+
 ;;;--------------------------------------------------------------------------
 ;;; Zone types.