base16.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2012, 2014, 2017 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 (gcrypt base16)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-26)
  21. #:use-module (srfi srfi-60)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (ice-9 vlist)
  24. #:use-module (ice-9 format)
  25. #:export (bytevector->base16-string
  26. base16-string->bytevector))
  27. ;;;
  28. ;;; Base 16.
  29. ;;;
  30. (define (bytevector->base16-string bv)
  31. "Return the hexadecimal representation of BV's contents."
  32. (define len
  33. (bytevector-length bv))
  34. (let-syntax ((base16-chars (lambda (s)
  35. (syntax-case s ()
  36. (_
  37. (let ((v (list->vector
  38. (unfold (cut > <> 255)
  39. (lambda (n)
  40. (format #f "~2,'0x" n))
  41. 1+
  42. 0))))
  43. v))))))
  44. (define chars base16-chars)
  45. (let loop ((i len)
  46. (r '()))
  47. (if (zero? i)
  48. (string-concatenate r)
  49. (let ((i (- i 1)))
  50. (loop i
  51. (cons (vector-ref chars (bytevector-u8-ref bv i)) r)))))))
  52. (define base16-string->bytevector
  53. (let ((chars->value (fold (lambda (i r)
  54. (vhash-consv (string-ref (number->string i 16)
  55. 0)
  56. i r))
  57. vlist-null
  58. (iota 16))))
  59. (lambda (s)
  60. "Return the bytevector whose hexadecimal representation is string S."
  61. (define bv
  62. (make-bytevector (quotient (string-length s) 2) 0))
  63. (string-fold (lambda (chr i)
  64. (let ((j (quotient i 2))
  65. (v (and=> (vhash-assv chr chars->value) cdr)))
  66. (if v
  67. (if (zero? (logand i 1))
  68. (bytevector-u8-set! bv j
  69. (arithmetic-shift v 4))
  70. (let ((w (bytevector-u8-ref bv j)))
  71. (bytevector-u8-set! bv j (logior v w))))
  72. (error "invalid hexadecimal character" chr)))
  73. (+ i 1))
  74. 0
  75. s)
  76. bv)))