hmac.scm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.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-hmac)
  19. #:use-module (rnrs bytevectors)
  20. #:use-module (srfi srfi-64)
  21. #:use-module (gcrypt hmac))
  22. (test-begin "hmac")
  23. (define test-key (gen-signing-key))
  24. (let ((sig (sign-data test-key "monkey party")))
  25. ;; Should be a bytevector
  26. (test-assert (bytevector? sig))
  27. ;; Correct sig succeeds
  28. (test-assert (verify-sig test-key "monkey party" sig))
  29. ;; Incorrect data fails
  30. (test-assert (not (verify-sig test-key "something else" sig)))
  31. ;; Fake signature fails
  32. (test-assert (not (verify-sig test-key "monkey party"
  33. (string->utf8 "fake sig"))))
  34. ;; Should equal a re-run of itself
  35. (test-equal sig (sign-data test-key "monkey party"))
  36. ;; Shouldn't equal something different
  37. (test-assert (not (equal? sig (sign-data test-key "cookie party")))))
  38. ;; Now with base64 encoding
  39. (let ((sig (sign-data-base64 test-key "monkey party")))
  40. ;; Should be a string
  41. (test-assert (string? sig))
  42. ;; Correct sig succeeds
  43. (test-assert (verify-sig-base64 test-key "monkey party" sig))
  44. ;; Incorrect data fails
  45. (test-assert (not (verify-sig-base64 test-key "something else" sig)))
  46. ;; Fake signature fails
  47. (test-assert (not (verify-sig-base64 test-key "monkey party"
  48. "f41c3516")))
  49. ;; Should equal a re-run of itself
  50. (test-equal sig (sign-data-base64 test-key "monkey party"))
  51. ;; Shouldn't equal something different
  52. (test-assert (not (equal? sig (sign-data-base64 test-key "cookie party")))))
  53. (test-end "hmac")