1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; 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.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix hash)
20 #:use-module (guix gcrypt)
21 #:use-module (rnrs bytevectors)
22 #:use-module (rnrs io ports)
23 #:use-module (system foreign)
24 #:use-module ((guix build utils) #:select (dump-port))
25 #:use-module (srfi srfi-11)
30 open-sha256-input-port))
34 ;;; Cryptographic hashes.
43 (define-syntax GCRY_MD_SHA256
44 ;; Value as of Libgcrypt 1.5.2.
45 (identifier-syntax 8))
48 (let ((hash (pointer->procedure void
49 (libgcrypt-func "gcry_md_hash_buffer")
50 `(,int * * ,size_t))))
52 "Return the SHA256 of BV as a bytevector."
53 (let ((digest (make-bytevector (/ 256 8))))
54 (hash GCRY_MD_SHA256 (bytevector->pointer digest)
55 (bytevector->pointer bv) (bytevector-length bv))
58 (define open-sha256-md
59 (let ((open (pointer->procedure int
60 (libgcrypt-func "gcry_md_open")
61 `(* ,int ,unsigned-int))))
63 (let* ((md (bytevector->pointer (make-bytevector (sizeof '*))))
64 (err (open md GCRY_MD_SHA256 0)))
66 (dereference-pointer md)
67 (throw 'gcrypt-error err))))))
70 (pointer->procedure void
71 (libgcrypt-func "gcry_md_write")
75 (pointer->procedure '*
76 (libgcrypt-func "gcry_md_read")
80 (pointer->procedure void
81 (libgcrypt-func "gcry_md_close")
85 (define (open-sha256-port)
86 "Return two values: an output port, and a thunk. When the thunk is called,
87 it returns the SHA256 hash (a bytevector) of all the data written to the
95 (let ((ptr (md-read sha256-md 0)))
96 (set! digest (bytevector-copy (pointer->bytevector ptr 32)))
97 (md-close sha256-md)))
99 (define (write! bv offset len)
104 (let ((ptr (bytevector->pointer bv offset)))
105 (md-write sha256-md ptr len)
112 (values (make-custom-binary-output-port "sha256"
120 (define (port-sha256 port)
121 "Return the SHA256 hash (a bytevector) of all the data drained from PORT."
122 (let-values (((out get)
128 (define (file-sha256 file)
129 "Return the SHA256 hash (a bytevector) of FILE."
130 (call-with-input-file file port-sha256))
132 (define (open-sha256-input-port port)
133 "Return an input port that wraps PORT and a thunk to get the hash of all the
134 data read from PORT. The thunk always returns the same value."
138 (define (read! bv start count)
139 (let ((n (get-bytevector-n! port bv start count)))
144 (let ((ptr (bytevector->pointer bv start)))
145 (md-write md ptr n)))
151 (let ((ptr (md-read md 0)))
152 (set! digest (bytevector-copy (pointer->bytevector ptr 32)))
160 (define (unbuffered port)
161 ;; Guile <= 2.0.9 does not support 'setvbuf' on custom binary input ports.
162 ;; If you get a wrong-type-arg error here, the fix is to upgrade Guile. :-)
163 (setvbuf port _IONBF)
166 (values (unbuffered (make-custom-binary-input-port "sha256" read! #f #f #f))
169 ;;; hash.scm ends here