aboutsummaryrefslogtreecommitdiff
path: root/gnu/gnunet/hashcode-ascii.scm
blob: ca5c12559c381e78f7fab6cf89975ed18df4ed81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;;   This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
;;   Copyright (C) 2001--2013, 2020 GNUnet e.V.
;;
;;   GNUnet is free software: you can redistribute it and/or modify it
;;   under the terms of the GNU Affero General Public License as published
;;   by the Free Software Foundation, either version 3 of the License,
;;   or (at your option) any later version.
;;
;;   GNUnet is distributed in the hope that it will be useful, but
;;   WITHOUT ANY WARRANTY; without even the implied warranty of
;;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;   Affero General Public License for more details.
;;
;;   You should have received a copy of the GNU Affero General Public License
;;   along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;;   SPDX-License-Identifier: AGPL-3.0-or-later
;;
;;   As a special exception to the GNU Affero General Public License,
;;   the file may be relicensed under any license used for
;;   most source code of GNUnet 0.13.1, or later versions, as published by
;;   GNUnet e.V.

;; Extracted from src/util/crypto_hash.c by Christian Grothoff

(library (gnu gnunet hashcode-ascii (1 1))
  (export hashcode->ascii ascii->hashcode
          #; hashcode-ascii-length)
  (import (gnu gnunet hashcode)
          (gnu gnunet data-string)
	  (rnrs base))

  ;; In upstream, this is 104 for the \0 terminator
  #;(define hashcode-ascii-length 103)

  (define (hashcode->ascii hashcode)
    "Convert a <hashcode> to ASCII encoding.  The ASCII encoding is rather
GNUnet specific.  It was chosen such that it only uses characters
in [0-9A-V], can be produced without complex arithmetics and uses a
small number of characters.  The GNUnet encoding uses 103
characters.

@var{block} the hash code"
    (data->string (hashcode->bv hashcode)))

  (define (ascii->hashcode ascii)
    "Convert ASCII encoding back to hash code.

@var{ascii} the encoding
Return @lisp{#f} in case of an encoding error."
    (let ((bv (string->data ascii hashcode-u8-length)))
      (and bv (bv->hashcode bv)))))