hash.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2013, 2014, 2017, 2019, 2020 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
  7. ;;; modify it under the terms of the GNU Lesser General Public License
  8. ;;; as published by the Free Software Foundation; either version 3 of
  9. ;;; the License, or (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. ;;; Lesser General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Lesser 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 %hello-sha512
  36. (base16-string->bytevector
  37. "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"))
  38. (define (supports-unbuffered-cbip?)
  39. "Return #t if unbuffered custom binary input ports (CBIPs) are supported.
  40. In Guile <= 2.0.9, CBIPs were always fully buffered, so the
  41. 'open-hash-input-port' does not work there."
  42. (false-if-exception
  43. (setvbuf (make-custom-binary-input-port "foo" pk #f #f #f)
  44. (cond-expand ((and guile-2 (not guile-2.2)) _IONBF)
  45. (else 'none)))))
  46. (test-begin "hash")
  47. (test-equal "lookup-hash-algorithm"
  48. (hash-algorithm blake2b-512)
  49. (lookup-hash-algorithm 'blake2b-512))
  50. (test-eq "hash-algorithm-name"
  51. 'sha3-512
  52. (hash-algorithm-name (hash-algorithm sha3-512)))
  53. (test-equal "hash-size"
  54. (list 20 32 64)
  55. (map hash-size
  56. (list (hash-algorithm sha1)
  57. (hash-algorithm sha256)
  58. (hash-algorithm sha512))))
  59. (test-equal "sha1, empty"
  60. (base16-string->bytevector "da39a3ee5e6b4b0d3255bfef95601890afd80709")
  61. (sha1 #vu8()))
  62. (test-equal "sha1, hello"
  63. (base16-string->bytevector "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
  64. (sha1 (string->utf8 "hello world")))
  65. (test-equal "sha256, empty"
  66. %empty-sha256
  67. (sha256 #vu8()))
  68. (test-equal "sha256, hello"
  69. %hello-sha256
  70. (sha256 (string->utf8 "hello world")))
  71. (test-equal "sha512, empty"
  72. "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
  73. (bytevector->base16-string (sha512 #vu8())))
  74. (test-equal "sha512, hello"
  75. %hello-sha512
  76. (bytevector-hash (string->utf8 "hello world")
  77. (hash-algorithm sha512)))
  78. (test-equal "open-sha256-port, empty"
  79. %empty-sha256
  80. (let-values (((port get)
  81. (open-sha256-port)))
  82. (close-port port)
  83. (get)))
  84. (test-equal "open-sha256-port, hello"
  85. (list %hello-sha256 (string-length "hello world"))
  86. (let-values (((port get)
  87. (open-sha256-port)))
  88. (put-bytevector port (string->utf8 "hello world"))
  89. (force-output port)
  90. (list (get) (port-position port))))
  91. (test-equal "open-hash-port, sha512, hello"
  92. (list %hello-sha512 (string-length "hello world"))
  93. (let-values (((port get)
  94. (open-hash-port (hash-algorithm sha512))))
  95. (put-bytevector port (string->utf8 "hello world"))
  96. (force-output port)
  97. (list (get) (port-position port))))
  98. (test-assert "port-sha256"
  99. (let* ((file (search-path %load-path "ice-9/psyntax.scm"))
  100. (size (stat:size (stat file)))
  101. (contents (call-with-input-file file get-bytevector-all)))
  102. (equal? (sha256 contents)
  103. (call-with-input-file file port-sha256))))
  104. (test-skip (if (supports-unbuffered-cbip?) 0 4))
  105. (test-equal "open-sha256-input-port, empty"
  106. `("" ,%empty-sha256)
  107. (let-values (((port get)
  108. (open-sha256-input-port (open-string-input-port ""))))
  109. (let ((str (get-string-all port)))
  110. (list str (get)))))
  111. (test-equal "open-sha256-input-port, hello"
  112. `("hello world" ,%hello-sha256)
  113. (let-values (((port get)
  114. (open-sha256-input-port
  115. (open-bytevector-input-port
  116. (string->utf8 "hello world")))))
  117. (let ((str (get-string-all port)))
  118. (list str (get)))))
  119. (test-equal "open-hash-input-port, sha512, hello"
  120. `("hello world" ,%hello-sha512)
  121. (let-values (((port get)
  122. (open-hash-input-port
  123. (hash-algorithm sha512)
  124. (open-bytevector-input-port
  125. (string->utf8 "hello world")))))
  126. (let ((str (get-string-all port)))
  127. (list str (get)))))
  128. (test-equal "open-sha256-input-port, hello, one two"
  129. (list (string->utf8 "hel") (string->utf8 "lo")
  130. (base16-string->bytevector ; echo -n hello | sha256sum
  131. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  132. " world")
  133. (let-values (((port get)
  134. (open-sha256-input-port
  135. (open-bytevector-input-port (string->utf8 "hello world")))))
  136. (let* ((one (get-bytevector-n port 3))
  137. (two (get-bytevector-n port 2))
  138. (hash (get))
  139. (three (get-string-all port)))
  140. (list one two hash three))))
  141. (test-equal "open-sha256-input-port, hello, read from wrapped port"
  142. (list (string->utf8 "hello")
  143. (base16-string->bytevector ; echo -n hello | sha256sum
  144. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  145. " world")
  146. (let*-values (((wrapped)
  147. (open-bytevector-input-port (string->utf8 "hello world")))
  148. ((port get)
  149. (open-sha256-input-port wrapped)))
  150. (let* ((hello (get-bytevector-n port 5))
  151. (hash (get))
  152. ;; Now read from WRAPPED to make sure its current position is
  153. ;; correct.
  154. (world (get-string-all wrapped)))
  155. (list hello hash world))))
  156. (test-end)