]> gitweb.factorcode.org Git - factor.git/commitdiff
checksums.sodium: support Sodium generic hash in checksums vocab
authorAlexander Iljin <ajsoft@yandex.ru>
Wed, 1 Mar 2017 22:41:49 +0000 (01:41 +0300)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 22 Jan 2018 17:26:16 +0000 (09:26 -0800)
extra/checksums/sodium/authors.txt [new file with mode: 0644]
extra/checksums/sodium/sodium.factor [new file with mode: 0644]
extra/checksums/sodium/summary.txt [new file with mode: 0644]

diff --git a/extra/checksums/sodium/authors.txt b/extra/checksums/sodium/authors.txt
new file mode 100644 (file)
index 0000000..8e1955f
--- /dev/null
@@ -0,0 +1 @@
+Alexander Ilin
diff --git a/extra/checksums/sodium/sodium.factor b/extra/checksums/sodium/sodium.factor
new file mode 100644 (file)
index 0000000..3fbcabd
--- /dev/null
@@ -0,0 +1,41 @@
+! Copyright (C) 2017 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors alien byte-arrays checksums checksums.common
+destructors kernel math sequences sodium sodium.ffi ;
+IN: checksums.sodium
+
+TUPLE: sodium-checksum { output-size fixnum } ;
+INSTANCE: sodium-checksum block-checksum
+C: <sodium-checksum> sodium-checksum
+
+<PRIVATE
+
+TUPLE: sodium-state < disposable
+    { state alien }
+    { output-size fixnum }
+    { output maybe{ byte-array } } ;
+
+PRIVATE>
+
+M: sodium-checksum initialize-checksum-state
+    output-size>> sodium-state new-disposable swap >>output-size
+    crypto_generichash_statebytes sodium_malloc >>state
+    [
+        [ state>> ] [ drop f 0 ] [ output-size>> ] tri
+        crypto_generichash_init check0
+    ] keep ;
+
+M: sodium-state dispose*
+    state>> [ sodium_free ] when* ;
+
+M: sodium-state add-checksum-bytes
+    [ dup state>> ] dip dup length crypto_generichash_update check0 ;
+
+M: sodium-state get-checksum
+    dup output>> [
+        dup state>> [
+            over output-size>> [ <byte-array> ] keep
+            [ crypto_generichash_final check0 ] 2keep drop
+        ] [ B{ } clone ] if*
+        [ >>output ] keep
+    ] unless* nip ;
diff --git a/extra/checksums/sodium/summary.txt b/extra/checksums/sodium/summary.txt
new file mode 100644 (file)
index 0000000..a4c8846
--- /dev/null
@@ -0,0 +1 @@
+The default hash implementation provided by the Sodium crypto library