hash.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2013, 2014, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of guile-gcrypt.
  5. ;;;
  6. ;;; guile-gcrypt 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
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; guile-gcrypt 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 GNU
  14. ;;; General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-hash)
  19. #:use-module (gcrypt hash)
  20. #:use-module (gcrypt base16)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-11)
  23. #:use-module (srfi srfi-64)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (rnrs io ports))
  26. ;; Test the (guix hash) module.
  27. (define %empty-sha256
  28. ;; SHA256 hash of the empty string.
  29. (base16-string->bytevector
  30. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
  31. (define %hello-sha256
  32. ;; SHA256 hash of "hello world"
  33. (base16-string->bytevector
  34. "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))
  35. (define (supports-unbuffered-cbip?)
  36. "Return #t if unbuffered custom binary input ports (CBIPs) are supported.
  37. In Guile <= 2.0.9, CBIPs were always fully buffered, so the
  38. 'open-sha256-input-port' does not work there."
  39. (false-if-exception
  40. (setvbuf (make-custom-binary-input-port "foo" pk #f #f #f)
  41. (cond-expand ((and guile-2 (not guile-2.2)) _IONBF)
  42. (else 'none)))))
  43. (test-begin "hash")
  44. (test-equal "sha1, empty"
  45. (base16-string->bytevector "da39a3ee5e6b4b0d3255bfef95601890afd80709")
  46. (sha1 #vu8()))
  47. (test-equal "sha1, hello"
  48. (base16-string->bytevector "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
  49. (sha1 (string->utf8 "hello world")))
  50. (test-equal "sha256, empty"
  51. %empty-sha256
  52. (sha256 #vu8()))
  53. (test-equal "sha256, hello"
  54. %hello-sha256
  55. (sha256 (string->utf8 "hello world")))
  56. (test-equal "open-sha256-port, empty"
  57. %empty-sha256
  58. (let-values (((port get)
  59. (open-sha256-port)))
  60. (close-port port)
  61. (get)))
  62. (test-equal "open-sha256-port, hello"
  63. (list %hello-sha256 (string-length "hello world"))
  64. (let-values (((port get)
  65. (open-sha256-port)))
  66. (put-bytevector port (string->utf8 "hello world"))
  67. (force-output port)
  68. (list (get) (port-position port))))
  69. (test-assert "port-sha256"
  70. (let* ((file (search-path %load-path "ice-9/psyntax.scm"))
  71. (size (stat:size (stat file)))
  72. (contents (call-with-input-file file get-bytevector-all)))
  73. (equal? (sha256 contents)
  74. (call-with-input-file file port-sha256))))
  75. (test-skip (if (supports-unbuffered-cbip?) 0 4))
  76. (test-equal "open-sha256-input-port, empty"
  77. `("" ,%empty-sha256)
  78. (let-values (((port get)
  79. (open-sha256-input-port (open-string-input-port ""))))
  80. (let ((str (get-string-all port)))
  81. (list str (get)))))
  82. (test-equal "open-sha256-input-port, hello"
  83. `("hello world" ,%hello-sha256)
  84. (let-values (((port get)
  85. (open-sha256-input-port
  86. (open-bytevector-input-port
  87. (string->utf8 "hello world")))))
  88. (let ((str (get-string-all port)))
  89. (list str (get)))))
  90. (test-equal "open-sha256-input-port, hello, one two"
  91. (list (string->utf8 "hel") (string->utf8 "lo")
  92. (base16-string->bytevector ; echo -n hello | sha256sum
  93. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  94. " world")
  95. (let-values (((port get)
  96. (open-sha256-input-port
  97. (open-bytevector-input-port (string->utf8 "hello world")))))
  98. (let* ((one (get-bytevector-n port 3))
  99. (two (get-bytevector-n port 2))
  100. (hash (get))
  101. (three (get-string-all port)))
  102. (list one two hash three))))
  103. (test-equal "open-sha256-input-port, hello, read from wrapped port"
  104. (list (string->utf8 "hello")
  105. (base16-string->bytevector ; echo -n hello | sha256sum
  106. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  107. " world")
  108. (let*-values (((wrapped)
  109. (open-bytevector-input-port (string->utf8 "hello world")))
  110. ((port get)
  111. (open-sha256-input-port wrapped)))
  112. (let* ((hello (get-bytevector-n port 5))
  113. (hash (get))
  114. ;; Now read from WRAPPED to make sure its current position is
  115. ;; correct.
  116. (world (get-string-all wrapped)))
  117. (list hello hash world))))
  118. (test-end)