aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-01-14 15:42:48 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-01-14 15:43:24 -0800
commitc1c32688a4afc43f6addb99b8b5fa878944a70e3 (patch)
treec886f21b5b08a9053aa74fcba4b241dae5ede76d /lib/file_hash
parent2309b696fc34b24f05f6658b94f9105ca8ee76e4 (diff)
Overhaul in progress, mostly working. Moved all the nomsu packages into
lib/, including core/*. Changes to how nomsu environments and importing work.
Diffstat (limited to 'lib/file_hash')
-rw-r--r--lib/file_hash/init.nom60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/file_hash/init.nom b/lib/file_hash/init.nom
new file mode 100644
index 0000000..7b428cd
--- /dev/null
+++ b/lib/file_hash/init.nom
@@ -0,0 +1,60 @@
+#!/usr/bin/env nomsu -V6.14
+#
+ This file defines some actions for hashing files and looking up files by hash.
+
+use "filesystem"
+use "base64"
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+lua> "local \$use_sha1, \$hashlib = pcall(require, 'openssl.digest')"
+test:
+ assume (hash "hello world") == (hash "hello world")
+ if ((hash "hello world") == (hash "goodbye")):
+ fail ("
+ Hash collision:
+ (hash "hello world") = \(hash "hello world")
+ (hash "goodbye") = \(hash "goodbye")
+ ")
+
+ assume
+ (
+ hash ("
+ This is a really long string meant to stress test the hashing function and
+ ensure that it's not overflowing with long inputs.
+ ")
+ ) != "inf"
+
+ if ((hash "\000") == (hash "\000\000\000\000\000")):
+ fail "Incorrect hashing of null strings"
+
+ if $use_sha1:
+ assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=")
+
+if $use_sha1:
+ externally (hash $) means:
+ $hash = (=lua "\$hashlib.new('sha1'):final(\$)")
+ return (base64 $hash)
+..else:
+ # TODO: remove warning?
+ say ("
+ \027[31;1mWARNING: OpenSSL module not found. Defaulting to a non-cryptographically secure \
+ ..hash function.\027[0m
+ ")
+
+ externally (hash $) means:
+ $bytes = ($, bytes)
+ $hash = ($bytes.1 << 7)
+ for $i in 2 to (size of $bytes):
+ $hash = ((1000003 * $hash) ~ $bytes.$i)
+ $hash = ($hash ~ (size of $bytes))
+ return "\$hash"
+
+externally (file with hash $hash) means:
+ for $filename in (files for "."):
+ $contents = (read file $filename)
+ $file_hash = (hash $contents)
+ if ($file_hash == $hash):
+ return $filename
+
+(hash of file $filename) parses as (hash (read file $filename))