Debian Codegraph @

guile/tests/hash.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS
2 ;;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This program is free software: you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation, either version 3 of the License, or
9 ;;; (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, see <https://www.gnu.org/licenses/>.
18
19 ;; Check the hash algorithm implementation.
20
21 (use-modules (gnutls)
22 (gnutls build tests)
23 (rnrs bytevectors)
24 (ice-9 match)
25 (srfi srfi-1)
26 (srfi srfi-26))
27
28 (run-test
29 (lambda ()
30 ;; This checks that the direct and indirect hash methods give the same
31 ;; results.
32 (let ((payload (string->utf8 "Example data to hash.")))
33 (let ((direct-method (hash-direct digest/sha256 payload))
34 (indirect-method
35 (let ((state (make-hash digest/sha256)))
36 (hash! state payload)
37 (when (defined? 'hash-copy)
38 (let ((copy (hash-copy state)))
39 (hash! copy (string->utf8 " More data."))
40 (unless
41 (equal?
42 (hash-output copy)
43 #vu8(163 1 149 172 97 42 249 15 132 114 243 203 145
44 67 18 186 127 135 127 158 131 43 244 38 23
45 46 156 254 193 225 201 171))
46 (error "The hash state copy failed."))))
47 (hash-output state))))
48 (unless (equal? direct-method indirect-method)
49 (error "The direct and indirect hash methods do not give the same result."))))
50 ;; This is the example in the manual.
51 (let ((manual-example
52 (lambda ()
53 (load-from-path "hash-example.scm")))
54 ;; This is how we run the example for the purpose of testing the
55 ;; code.
56 (manual-example-environment
57 (lambda (f)
58 (call-with-output-file "hash-manual-example-data"
59 (lambda (port)
60 (format port "Example data to hash."))
61 #:encoding "UTF-8")
62 (let* ((input (call-with-output-string
63 (lambda (port)
64 ;; Which file do you want to hash?
65 (format port "./hash-manual-example-data\n"))))
66 (expected-output
67 (call-with-output-string
68 (lambda (port)
69 (format port "Which file do you want to hash?\n")
70 ;; we tell it the file
71 (format port "The digest is: ~s\n"
72 #vu8(87 122 203 105 209 216 209 172 80 105
73 249 68 57 176 185 62 230 184 16 225
74 123 155 151 181 137 113 249 49 37
75 135 34 234)))))
76 (true-output
77 (call-with-output-string
78 (lambda (output-port)
79 (with-input-from-port
80 (open-input-string input)
81 (lambda ()
82 (with-output-to-port output-port
83 f)))))))
84 (delete-file "hash-manual-example-data")
85 (unless (equal? expected-output true-output)
86 (error "Manual example for hash failed."))))))
87 (manual-example-environment manual-example))))