summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/cespare/xxhash/v2@v2.1.2/xxhsum/xxhsum.go
blob: 9b1d035a63ea90daa2036e9914ab58983376b831 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"fmt"
	"io"
	"os"

	"github.com/cespare/xxhash/v2"
)

func main() {
	if contains(os.Args[1:], "-h") {
		fmt.Fprintf(os.Stderr, `Usage:
  %s [filenames]
If no filenames are provided or only - is given, input is read from stdin.
`, os.Args[0])
		os.Exit(1)
	}
	if len(os.Args) < 2 || len(os.Args) == 2 && os.Args[1] == "-" {
		printHash(os.Stdin, "-")
		return
	}
	for _, path := range os.Args[1:] {
		f, err := os.Open(path)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		printHash(f, path)
		f.Close()
	}
}

func contains(ss []string, s string) bool {
	for _, s1 := range ss {
		if s1 == s {
			return true
		}
	}
	return false
}

func printHash(r io.Reader, name string) {
	h := xxhash.New()
	if _, err := io.Copy(h, r); err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Printf("%016x  %s\n", h.Sum64(), name)
}