Debian Codegraph @

guile/examples/hash-example.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This library is free software; you can redistribute it and/or
7 ;;; modify it under the terms of the GNU Lesser General Public
8 ;;; License as published by the Free Software Foundation; either
9 ;;; version 2.1 of the License, or (at your option) any later version.
10 ;;;
11 ;;; This library 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 GNU
14 ;;; Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with this library; if not, see <https://www.gnu.org/licenses/>.
18
19 (use-modules (ice-9 rdelim)
20 (ice-9 binary-ports)
21 (rnrs bytevectors)
22 (gnutls))
23 (format #t "Which file do you want to hash?\n")
24
25 (let ((file-name (read-line)))
26 ;; Create a new state that will be reused when new bytes are
27 ;; available.
28 (let ((state (make-hash digest/sha256)))
29 (call-with-input-file file-name
30 (lambda (port)
31 (let hash-all ()
32 ;; Read raw bytes from the file.
33 (let ((next (get-bytevector-some port)))
34 (if (eof-object? next)
35 ;; No more data in the file
36 (format #t "The digest is: ~s\n"
37 (hash-output state))
38 (begin
39 ;; Hash the bytes we got, and continue.
40 (hash! state next)
41 (hash-all))))))
42 #:binary #t)))