simplification
Download
61 lines 1.8 KiBNomsuModified 2026-11-08 raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
#!/usr/bin/env nomsu -V8.0.0
###
    This file defines some actions for hashing files and looking up files by hash.
    
use "filesystem"
use "base64"
use "bitwise"

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:
    public:
        (hash $) means:
            $hash = (=lua "\$hashlib.new('sha1'):final(\$)")
            return (base64 $hash)
..else:
    ### TODO: remove warning?
    say ("
        \("\027[31;1m")WARNING: OpenSSL module not found. Defaulting to a non-cryptographically secure
    .... hash function.\("\027[0m")
    ")
    
    public:
        (hash $) means:
            $bytes = ($, bytes)
            $hash = ($bytes.1 << 7)
            for $i in (2 to #$bytes):
                $hash = ((1000003 * $hash) ~ $bytes.$i)
            $hash = ($hash ~ #$bytes)
            return "\$hash"

public:
    (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))