gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-scheme] 34/49: crypto: Implement functional variants.


From: gnunet
Subject: [gnunet-scheme] 34/49: crypto: Implement functional variants.
Date: Sat, 25 Dec 2021 23:00:11 +0100

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit d68778f10a817e69d078dbb0bbf9e3e049a31164
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sat Oct 2 23:58:57 2021 +0200

    crypto: Implement functional variants.
    
    * gnu/gnunet/crypto.scm: Add the file to git.
    * gnu/gnunet/crypto.scm
      (hasher,hash-slice/bytevector,hash-slice,hash/sha512): New
      procedures.
      (hash-slice!): Use hash-slice/bytevector.
    * doc/scheme-gnunet.tm: Document hash/sha512.
---
 doc/scheme-gnunet.tm  |  7 ++++++
 gnu/gnunet/crypto.scm | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)

diff --git a/doc/scheme-gnunet.tm b/doc/scheme-gnunet.tm
index 25f4216..f388f79 100644
--- a/doc/scheme-gnunet.tm
+++ b/doc/scheme-gnunet.tm
@@ -1062,6 +1062,13 @@
   slice of length 512 bits / 64 bytes.<space|1em>The result is unspecified if
   <var|slice> and <var|to> overlap.>
 
+  <\explain>
+    <scm|(hash/sha512 <var|slice>)>
+  <|explain>
+    Like <scm|hash/sha512!>, but allocate the <var|>destination slice
+    <var|to> and return it.
+  </explain>
+
   <chapter|Implementation details>
 
   TODO<appendix|GNU Free Documentation License>
diff --git a/gnu/gnunet/crypto.scm b/gnu/gnunet/crypto.scm
new file mode 100644
index 0000000..c824faf
--- /dev/null
+++ b/gnu/gnunet/crypto.scm
@@ -0,0 +1,68 @@
+;; This file is part of GNUnet
+;; Copyright (C) 2021 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
+
+;; Small wrapper around guile-gcrypt
+(define-library (gnu gnunet crypto)
+  (export hash/sha512 hash/sha512!)
+  (import (only (gcrypt hash)
+               hash-algorithm open-hash-port sha512)
+         (gnu gnunet utils bv-slice)
+         (only (srfi srfi-8)
+               receive)
+         (only (guile)
+               %make-void-port close-port)
+         (only (ice-9 binary-ports)
+               put-bytevector)
+         (only (rnrs base)
+               begin lambda define))
+  (begin
+    ;; TODO: Extend bytevector-hash with offset + length.
+    (define (hash-slice/bytevector algorithm slice)
+      "Hash the data in the readable bytevector slice @var{slice} and
+return a bytevector with the resulting hash."
+      (define slice/read (slice/read-only slice))
+      (receive (port get-hash) (open-hash-port algorithm)
+       (put-bytevector port
+                       (slice-bv slice/read)
+                       (slice-offset slice/read)
+                       (slice-length slice/read))
+       (close-port port)
+       (get-hash)))
+
+    (define (hash-slice! algorithm slice to)
+      "Hash the data in the readable bytevector slice @var{slice} and write the
+hash to the bytevector slice @var{to}."
+      (slice-copy! (hash-slice/bytevector algorithm slice) to))
+
+    (define (hash-slice algorithm slice)
+      "Hash the data in the readable bytevector slice @var{slice} and return a
+fresh readable bytevector slice with the hash."
+      (slice/read-only
+       (bv-slice/read-write (hash-slice/bytevector algorithm slice))))
+
+    (define (hasher! algorithm)
+      (lambda (slice to)
+       (hash-slice! algorithm slice to)))
+
+    (define (hasher algorithm)
+      (lambda (slice)
+       (hash-slice algorithm slice)))
+
+    ;; (hash/sha512! data-to-hash-slice destination-slice) --> (nothing)
+    (define hash/sha512! (hasher! (hash-algorithm sha512)))
+    (define hash/sha512 (hasher (hash-algorithm sha512)))))

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]