code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(63 lines)
1 #!/usr/bin/env nomsu -V7.0.0
2 ###
3 This file defines some actions for hashing files and looking up files by hash.
5 use "filesystem"
6 use "base64"
8 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 lua> "local \$use_sha1, \$hashlib = pcall(require, 'openssl.digest')"
11 test:
12 assume (hash "hello world") == (hash "hello world")
13 if ((hash "hello world") == (hash "goodbye")):
14 fail ("
15 Hash collision:
16 (hash "hello world") = \(hash "hello world")
17 (hash "goodbye") = \(hash "goodbye")
18 ")
20 assume
22 hash ("
23 This is a really long string meant to stress test the hashing function and
24 ensure that it's not overflowing with long inputs.
25 ")
26 ) != "inf"
28 if ((hash "\000") == (hash "\000\000\000\000\000")):
29 fail "Incorrect hashing of null strings"
31 if $use_sha1:
32 assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=")
34 if $use_sha1:
35 external:
36 (hash $) means:
37 $hash = (=lua "\$hashlib.new('sha1'):final(\$)")
38 return (base64 $hash)
39 ..else:
40 ### TODO: remove warning?
41 say ("
42 \027[31;1mWARNING: OpenSSL module not found. Defaulting to a non-cryptographically secure \
43 ..hash function.\027[0m
44 ")
46 external:
47 (hash $) means:
48 $bytes = ($, bytes)
49 $hash = ($bytes.1 << 7)
50 for $i in (2 to #$bytes):
51 $hash = ((1000003 * $hash) ~ $bytes.$i)
52 $hash = ($hash ~ #$bytes)
53 return "\$hash"
55 external:
56 (file with hash $hash) means:
57 for $filename in (files for "."):
58 $contents = (read file $filename)
59 $file_hash = (hash $contents)
60 if ($file_hash == $hash):
61 return $filename
63 (hash of file $filename) parses as (hash (read file $filename))