diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-08-29 19:38:14 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-08-29 19:39:15 -0700 |
| commit | 4f30e02acb666c52e0254eb9a3bf89a9cabb5e6d (patch) | |
| tree | bacc6a2baf70c18d3d44db06dc2235ec42edd49f /lib/file_hash.nom | |
| parent | aae5ce31feb482a86d8ef96fb1f104194f26828c (diff) | |
Handling more compatibility stuff, including Lua 5.4, and a backup for
if openssl module is not found, and moving containers (List/Dict) into
their own file, as well as bit operators (and support for __bxor, etc.
metamethods in Lua 5.2/LuaJIT)
Diffstat (limited to 'lib/file_hash.nom')
| -rw-r--r-- | lib/file_hash.nom | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/lib/file_hash.nom b/lib/file_hash.nom index 188df78..bce8c08 100644 --- a/lib/file_hash.nom +++ b/lib/file_hash.nom @@ -5,13 +5,38 @@ use "lib/os.nom" use "lib/base64.nom" -%hashlib = (=lua "require('openssl.digest')") +lua> ".." + local \%use_sha1, \%hashlib = pcall(require, 'openssl.digest') test: - assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") -action [hash %, sha1 %]: - %hash = (=lua "\%hashlib.new('sha1'):final(\%)") - return (base64 %hash) + assume ((hash "hello world") == (hash "hello world")) + assume ((hash "hello world") != (hash "goodbye")) or barf ".." + 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" + assume ((hash "\0") != (hash "\0\0\0\0\0")) or barf "Incorrect hashing of null strings" + if %use_sha1: + assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") +if %use_sha1: + action [hash %]: + %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" + action [hash %]: + %bytes = (bytes of %) + %hash = (%bytes.1 << 7) + for %i in 2 to (size of %bytes): + %hash = ((1000003 * %hash) ~ %bytes.%i) + %hash = (%hash ~ (size of %bytes)) + return "\%hash" action [file with hash %hash]: for file %filename in ".": @@ -19,4 +44,4 @@ action [file with hash %hash]: %file_hash = (hash %contents) if (%file_hash == %hash): return %filename -parse [hash of file %filename] as (sha1 (read file %filename)) +parse [hash of file %filename] as (hash (read file %filename)) |
