diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-30 14:11:45 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-30 14:11:45 -0700 |
| commit | 2a12310b25f68208ff0101fce5d0137f267c7394 (patch) | |
| tree | 28a845cc5aaab477e6a6d87539a88162f5685365 /lib/file_hash.nom | |
| parent | 43e8da178084184db1701b0abb9d615046b4c51d (diff) | |
Switching hashes to use base64 instead of hex.
Diffstat (limited to 'lib/file_hash.nom')
| -rw-r--r-- | lib/file_hash.nom | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/lib/file_hash.nom b/lib/file_hash.nom index 8bbdd0a..aef1c78 100644 --- a/lib/file_hash.nom +++ b/lib/file_hash.nom @@ -2,25 +2,23 @@ # This file defines some actions for hashing files and looking up files by hash. -action [file with hash %hash]: - lua> ".." - local Hash = require("openssl.digest") - for filename in io.popen('find -L . -not -path "*/\\\\.*" -type f -name "*.nom"'):lines() do - local file = io.open(filename) - local contents = file:read("*a") - file:close() - local hash = Hash.new("sha1"):final(contents) - local hex = hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end) - if hex == \%hash then - return filename - end - end +use "lib/os.nom" +use "lib/base64.nom" + +%hashlib = (=lua "require('openssl.digest')") + +test: + assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") action [hash %, sha1 %]: - %hashlib = (=lua "require('openssl.digest')") %hash = (=lua "\%hashlib.new('sha1'):final(\%)") - return (..) - =lua ".." - \%hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end) + return (base64 %hash) + +action [file with hash %hash]: + for file %filename in ".": + %contents = (read file %filename) + %file_hash = (hash %contents) + if (%file_hash == %hash): + return %filename -parse [hash of file %filename] as (sha1 (=lua "io.open(\%filename):read('*a')")) +parse [hash of file %filename] as (sha1 (read file %filename)) |
