Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/cmd/generate-checksum/main.go
2070 views
1
package main
2
3
import (
4
"bytes"
5
"crypto/sha1"
6
"encoding/hex"
7
"io"
8
"io/fs"
9
"log"
10
"os"
11
"path/filepath"
12
"strings"
13
)
14
15
func main() {
16
if len(os.Args) < 3 {
17
log.Fatalf("Usage: %s <templates-directory> <checksum-file>\n", os.Args[0])
18
}
19
checksumFile := os.Args[2]
20
templatesDirectory := os.Args[1]
21
22
file, err := os.Create(checksumFile)
23
if err != nil {
24
log.Fatalf("Could not create file: %s\n", err)
25
}
26
defer func() {
27
_ = file.Close()
28
}()
29
30
err = filepath.WalkDir(templatesDirectory, func(path string, d fs.DirEntry, err error) error {
31
if err != nil || d.IsDir() {
32
return nil
33
}
34
pathIndex := path[strings.Index(path, "nuclei-templates/")+17:]
35
pathIndex = strings.TrimPrefix(pathIndex, "nuclei-templates/")
36
// Ignore items starting with dots
37
if strings.HasPrefix(pathIndex, ".") {
38
return nil
39
}
40
data, err := os.ReadFile(path)
41
if err != nil {
42
return nil
43
}
44
h := sha1.New()
45
_, _ = io.Copy(h, bytes.NewReader(data))
46
hash := hex.EncodeToString(h.Sum(nil))
47
48
_, _ = file.WriteString(pathIndex)
49
_, _ = file.WriteString(":")
50
_, _ = file.WriteString(hash)
51
_, _ = file.WriteString("\n")
52
return nil
53
})
54
if err != nil {
55
log.Fatalf("Could not walk directory: %s\n", err)
56
}
57
}
58
59