base64.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;;
  3. ;;; Code taken from:
  4. ;;;
  5. ;;; GNU Guix --- Functional package management for GNU
  6. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  7. ;;;
  8. ;;; This file is part of guile-gcrypt.
  9. ;;;
  10. ;;; guile-gcrypt is free software; you can redistribute it and/or
  11. ;;; modify it under the terms of the GNU Lesser General Public License
  12. ;;; as published by the Free Software Foundation; either version 3 of
  13. ;;; the License, or (at your option) any later version.
  14. ;;;
  15. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. ;;; Lesser General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU Lesser General Public License
  21. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (test-base64)
  23. #:use-module (gcrypt base64)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (srfi srfi-64))
  26. (define (string->base64 str)
  27. (base64-encode (string->utf8 str)))
  28. (define (base64->string base64)
  29. (utf8->string (base64-decode base64)))
  30. (define (string->base64-padding str padding)
  31. (let ((bv (string->utf8 str)))
  32. (base64-encode bv 0 (bytevector-length bv) #f (not padding))))
  33. (define (base64->string-padding base64 padding)
  34. (utf8->string (base64-decode base64 base64url-alphabet #f #f padding)))
  35. ;;; Test vectors from <https://tools.ietf.org/rfc/rfc4648.txt>.
  36. (test-begin "base64")
  37. ;; Encoding
  38. (test-equal "empty string"
  39. (string->base64 "")
  40. "")
  41. (test-equal "f"
  42. (string->base64 "f")
  43. "Zg==")
  44. (test-equal "fo"
  45. (string->base64 "fo")
  46. "Zm8=")
  47. (test-equal "foo"
  48. (string->base64 "foo")
  49. "Zm9v")
  50. (test-equal "foob"
  51. (string->base64 "foob")
  52. "Zm9vYg==")
  53. (test-equal "fooba"
  54. (string->base64 "fooba")
  55. "Zm9vYmE=")
  56. (test-equal "foobar"
  57. (string->base64 "foobar")
  58. "Zm9vYmFy")
  59. (test-equal "foob (no padding)"
  60. (string->base64-padding "foob" #f)
  61. "Zm9vYg")
  62. (test-equal "foob (padding)"
  63. (string->base64-padding "foob" #t)
  64. "Zm9vYg==")
  65. ;; Decoding
  66. (test-equal "empty string"
  67. (base64->string "")
  68. "")
  69. (test-equal "f"
  70. (base64->string "Zg==")
  71. "f")
  72. (test-equal "fo"
  73. (base64->string "Zm8=")
  74. "fo")
  75. (test-equal "foo"
  76. (base64->string "Zm9v")
  77. "foo")
  78. (test-equal "foob"
  79. (base64->string "Zm9vYg==")
  80. "foob")
  81. (test-equal "fooba"
  82. (base64->string "Zm9vYmE=")
  83. "fooba")
  84. (test-equal "foobar"
  85. (base64->string "Zm9vYmFy")
  86. "foobar")
  87. (test-equal "foob (no padding)"
  88. (base64->string-padding "Zm9vYg" #f)
  89. "foob")
  90. (test-equal "foob (padding)"
  91. (base64->string-padding "Zm9vYg==" #t)
  92. "foob")
  93. (test-end "base64")