From 4f30e02acb666c52e0254eb9a3bf89a9cabb5e6d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 29 Aug 2018 19:38:14 -0700 Subject: 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) --- lib/file_hash.nom | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'lib/file_hash.nom') 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)) -- cgit v1.2.3