-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha.go
90 lines (73 loc) · 1.61 KB
/
sha.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package hash
import (
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"os"
"bandr.me/p/pocryp/internal/common"
)
func ShaCmd(args []string) error {
fset := flag.NewFlagSet("hash-sha", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp hash-sha -alg [-bin] [-in INPUT] [-out OUTPUT]
Compute SHA digest of INPUT to OUTPUT.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
Options:
`)
fset.PrintDefaults()
}
fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := fset.String("in", "", "Read data from the file at path INPUT.")
fAlg := fset.String("alg", "", fmt.Sprintf("SHA algorithm to use; one of: %s.", common.SHAAlgs))
fBin := fset.Bool("bin", false, "Write output as binary not hex.")
if err := fset.Parse(args); err != nil {
return err
}
if *fAlg == "" {
fset.Usage()
return errors.New("hash alg not specified, use -alg")
}
hashFunc, err := common.HashFuncFrom(*fAlg)
if err != nil {
fset.Usage()
return err
}
var r io.Reader
if *fInput == "" {
r = os.Stdin
} else {
f, err := os.Open(*fInput)
if err != nil {
return err
}
defer f.Close()
r = f
}
var w io.Writer
if *fOutput == "" {
w = os.Stdout
} else {
f, err := os.Create(*fOutput)
if err != nil {
return err
}
defer f.Close()
w = f
}
h := hashFunc()
if _, err := io.Copy(h, r); err != nil {
return err
}
digest := h.Sum(nil)
if *fBin {
if _, err := w.Write(digest); err != nil {
return err
}
} else {
fmt.Fprintln(w, hex.EncodeToString(digest))
}
return nil
}