hash.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 %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-equal "hash-size"
  51. (list 20 32 64)
  52. (map hash-size
  53. (list (hash-algorithm sha1)
  54. (hash-algorithm sha256)
  55. (hash-algorithm sha512))))
  56. (test-equal "sha1, empty"
  57. (base16-string->bytevector "da39a3ee5e6b4b0d3255bfef95601890afd80709")
  58. (sha1 #vu8()))
  59. (test-equal "sha1, hello"
  60. (base16-string->bytevector "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
  61. (sha1 (string->utf8 "hello world")))
  62. (test-equal "sha256, empty"
  63. %empty-sha256
  64. (sha256 #vu8()))
  65. (test-equal "sha256, hello"
  66. %hello-sha256
  67. (sha256 (string->utf8 "hello world")))
  68. (test-equal "sha512, empty"
  69. "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
  70. (bytevector->base16-string
  71. (bytevector-hash #vu8() (hash-algorithm sha512))))
  72. (test-equal "sha512, hello"
  73. %hello-sha512
  74. (bytevector-hash (string->utf8 "hello world")
  75. (hash-algorithm sha512)))
  76. (test-equal "open-sha256-port, empty"
  77. %empty-sha256
  78. (let-values (((port get)
  79. (open-sha256-port)))
  80. (close-port port)
  81. (get)))
  82. (test-equal "open-sha256-port, hello"
  83. (list %hello-sha256 (string-length "hello world"))
  84. (let-values (((port get)
  85. (open-sha256-port)))
  86. (put-bytevector port (string->utf8 "hello world"))
  87. (force-output port)
  88. (list (get) (port-position port))))
  89. (test-equal "open-hash-port, sha512, hello"
  90. (list %hello-sha512 (string-length "hello world"))
  91. (let-values (((port get)
  92. (open-hash-port (hash-algorithm sha512))))
  93. (put-bytevector port (string->utf8 "hello world"))
  94. (force-output port)
  95. (list (get) (port-position port))))
  96. (test-assert "port-sha256"
  97. (let* ((file (search-path %load-path "ice-9/psyntax.scm"))
  98. (size (stat:size (stat file)))
  99. (contents (call-with-input-file file get-bytevector-all)))
  100. (equal? (sha256 contents)
  101. (call-with-input-file file port-sha256))))
  102. (test-skip (if (supports-unbuffered-cbip?) 0 4))
  103. (test-equal "open-sha256-input-port, empty"
  104. `("" ,%empty-sha256)
  105. (let-values (((port get)
  106. (open-sha256-input-port (open-string-input-port ""))))
  107. (let ((str (get-string-all port)))
  108. (list str (get)))))
  109. (test-equal "open-sha256-input-port, hello"
  110. `("hello world" ,%hello-sha256)
  111. (let-values (((port get)
  112. (open-sha256-input-port
  113. (open-bytevector-input-port
  114. (string->utf8 "hello world")))))
  115. (let ((str (get-string-all port)))
  116. (list str (get)))))
  117. (test-equal "open-hash-input-port, sha512, hello"
  118. `("hello world" ,%hello-sha512)
  119. (let-values (((port get)
  120. (open-hash-input-port
  121. (hash-algorithm sha512)
  122. (open-bytevector-input-port
  123. (string->utf8 "hello world")))))
  124. (let ((str (get-string-all port)))
  125. (list str (get)))))
  126. (test-equal "open-sha256-input-port, hello, one two"
  127. (list (string->utf8 "hel") (string->utf8 "lo")
  128. (base16-string->bytevector ; echo -n hello | sha256sum
  129. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  130. " world")
  131. (let-values (((port get)
  132. (open-sha256-input-port
  133. (open-bytevector-input-port (string->utf8 "hello world")))))
  134. (let* ((one (get-bytevector-n port 3))
  135. (two (get-bytevector-n port 2))
  136. (hash (get))
  137. (three (get-string-all port)))
  138. (list one two hash three))))
  139. (test-equal "open-sha256-input-port, hello, read from wrapped port"
  140. (list (string->utf8 "hello")
  141. (base16-string->bytevector ; echo -n hello | sha256sum
  142. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
  143. " world")
  144. (let*-values (((wrapped)
  145. (open-bytevector-input-port (string->utf8 "hello world")))
  146. ((port get)
  147. (open-sha256-input-port wrapped)))
  148. (let* ((hello (get-bytevector-n port 5))
  149. (hash (get))
  150. ;; Now read from WRAPPED to make sure its current position is
  151. ;; correct.
  152. (world (get-string-all wrapped)))
  153. (list hello hash world))))
  154. (test-end)