aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash.nom
blob: 32f6703914ad389a434041bfa6cf8c4836ad5c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env nomsu -V2.5.5.5
#
    This file defines some actions for hashing files and looking up files by hash.

use "lib/os.nom"
use "lib/base64.nom"

%hashlib = (=lua "require('openssl.digest')")

test:
    assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=")
action [hash %, sha1 %]:
    %hash = (=lua "\%hashlib.new('sha1'):final(\%)")
    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 (read file %filename))