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

use "core"

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

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)

parse [hash of file %filename] as (sha1 (=lua "io.open(\%filename):read('*a')"))