forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shasum.go
90 lines (77 loc) · 1.73 KB
/
shasum.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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"github.com/spf13/pflag"
)
func helpPrinter() {
fmt.Printf("Usage:\nshasum -a <algorithm> <File Name>\n")
pflag.PrintDefaults()
os.Exit(0)
}
func versionPrinter() {
fmt.Println("shasum utility, URoot Version.")
os.Exit(0)
}
func getInput(fileName string) (input []byte, err error) {
if fileName != "" {
return ioutil.ReadFile(fileName)
}
return ioutil.ReadAll(os.Stdin)
}
//
// shaPrinter prints sha1/sha256 of given data. The
// value of algorithm is expected to be 1 for SHA1
// and 256 for SHA256
//
func shaPrinter(algorithm int, data []byte) string {
var sha string
if algorithm == 256 {
sha = fmt.Sprintf("%x", sha256.Sum256(data))
} else if algorithm == 1 {
sha = fmt.Sprintf("%x", sha1.Sum(data))
} else {
fmt.Fprintf(os.Stderr, "Invalid algorithm")
return ""
}
return sha
}
func main() {
var (
algorithm int
help bool
version bool
)
cliArgs := ""
pflag.IntVarP(&algorithm, "algorithm", "a", 1, "SHA algorithm, valid args are 1 and 256")
pflag.BoolVarP(&help, "help", "h", false, "Show this help and exit")
pflag.BoolVarP(&version, "version", "v", false, "Print Version")
pflag.Parse()
if help {
helpPrinter()
}
if version {
versionPrinter()
}
if len(pflag.Args()) == 1 {
cliArgs = pflag.Args()[0]
}
input, err := getInput(cliArgs)
if err != nil {
fmt.Println("Error getting input.")
os.Exit(-1)
}
fmt.Printf("%s ", shaPrinter(algorithm, input))
if cliArgs == "" {
fmt.Printf(" -\n")
} else {
fmt.Printf(" %s\n", cliArgs)
}
os.Exit(0)
}