blob: 0689804cc0544fb39e1c86795e6214c73253bce2 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
;; Copyright (C) 2006--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/include/gnunet_common.h
(library (gnu gnunet hashcode)
(export hashcode-bit-length hashcode-u8-length
short-hashcode-bit-length short-hashcode-u8-length
hashcode? short-hashcode?
bv->hashcode bv->short-hashcode
hashcode->bv short-hashcode->bv)
(import (rnrs base)
(rnrs control)
(rnrs records syntactic)
(rnrs bytevectors))
(define hashcode-bit-length 512)
(define short-hashcode-bit-length 256)
(define hashcode-u8-length (/ hashcode-bit-length 8))
(define short-hashcode-u8-length (/ short-hashcode-bit-length 8))
;; A 512-bit hashcode. These are the default length for GNUnet,
;; using SHA-512.
(define-record-type (<hashcode> %make-hashcode hashcode?)
(fields (immutable bv %hashcode-bv))
(opaque #t)
(sealed #t))
;; A 256-bit hashcode. Used under special conditions, like when space
;; is critical and security is not impacted by it.
(define-record-type (<short-hashcode> %make-short-hashcode short-hashcode?)
(fields (immutable bv %short-hashcode-bv))
(opaque #t)
(sealed #t))
(define (bv->something %wrap length)
(case-lambda
"Read something from a bytevector"
((bv) ; whole bytevector
(assert (= (bytevector-length bv) length))
(%wrap (bytevector-copy bv)))
((bv offset) ; part of a bytevector, starting at some offset
(assert (<= (+ offset length) (bytevector-length bv)))
(let ((bv-new (make-bytevector length)))
(bytevector-copy! bv 0 bv-new 0 length)
(%wrap bv-new)))))
(define bv->hashcode
(bv->something %make-hashcode hashcode-u8-length))
(define bv->short-hashcode
(bv->something %make-short-hashcode short-hashcode-u8-length))
(define (hashcode->bv hashcode)
"Extract the bytevector corresponding to @var{hashcode}
(read-only)"
(bytevector-copy (%hashcode-bv hashcode)))
(define (short-hashcode->bv hashcode)
"Extract the bytevector corresponding to @var{short-hashcode}
(read-only)"
(bytevector-copy (%short-hashcode-bv hashcode))))
|