aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash.nom
blob: 988248172bd04439cae22f1c0b22a6ee5c1bfd53 (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 -V1
#
    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')"