-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
353 lines (325 loc) · 9.53 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"bytes"
"crypto"
"crypto/hmac"
_ "crypto/md5"
_ "crypto/sha1"
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
_ "golang.org/x/crypto/sha3"
"golang.org/x/sync/errgroup"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync/atomic"
)
import flag "github.com/spf13/pflag"
const version string = "3.5.3"
func getOutput(results *Checksums, opts Options) []*Output {
var backslash string
outputs := make([]*Output, 0, len(results.checksums)+1)
file := results.file
if !opts.zero {
file = escapeFilename(file)
if opts.gnu && len(file) != len(results.file) {
backslash = "\\"
}
}
if opts.size {
outputs = append(outputs, &Output{
File: file,
Name: "SIZE",
Sum: strconv.FormatInt(results.size, 10),
})
}
for i := range results.checksums {
var sum string
if opts.base64 {
sum = base64.StdEncoding.EncodeToString(results.checksums[i].sum)
} else {
sum = hex.EncodeToString(results.checksums[i].sum)
}
outputs = append(outputs, &Output{
File: file,
Name: algorithms[results.checksums[i].hash].name,
Sum: backslash + sum,
})
}
return outputs
}
func printChecksums(results *Checksums, opts Options) {
if err := format.Execute(os.Stdout, getOutput(results, opts)); err != nil {
panic(err)
}
}
func printCheckResults(results *Checksums) (unmatched int) {
file := escapeFilename(results.file)
for i := range results.checksums {
var ok bool
if macKey != nil {
ok = hmac.Equal(results.checksums[i].sum, results.checksums[i].csum)
} else {
ok = bytes.Equal(results.checksums[i].sum, results.checksums[i].csum)
}
if ok {
if !opts.quiet && !opts.status {
if opts.verbose {
fmt.Printf("%s: %s OK\n", file, algorithms[results.checksums[i].hash].name)
} else {
fmt.Printf("%s: OK\n", file)
}
}
} else {
unmatched++
if !opts.status {
if opts.verbose {
fmt.Printf("%s: %s FAILED with %s\n", file, algorithms[results.checksums[i].hash].name, hex.EncodeToString(results.checksums[i].sum))
} else {
fmt.Printf("%s: FAILED\n", file)
}
}
}
}
return unmatched
}
func init() {
log.SetPrefix("ERROR: ")
log.SetFlags(0)
progname := filepath.Base(os.Args[0])
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [-s STRING...]|[-c FILE]|[-i FILE]|[FILE...]|[-r FILE... DIRECTORY...]\n", progname)
flag.PrintDefaults()
}
if strings.HasPrefix(progname, "xhash") {
flag.BoolVarP(&opts.all, "all", "a", false, "all algorithms (except others specified, if any)")
}
if strings.Contains(progname, "sum") {
flag.BoolVarP(&opts.base64, "base64", "", false, "output hash in Base64 encoding format")
flag.BoolVarP(&opts.dummy, "binary", "b", false, "read in binary mode")
flag.BoolVarP(&opts.dummy, "text", "t", false, "read in text mode (default)")
flag.BoolVarP(&opts.tag, "tag", "", false, "create a BSD-style checksum")
} else {
flag.BoolVarP(&opts.base64, "base64", "b", false, "output hash in Base64 encoding format")
flag.BoolVarP(&opts.gnu, "gnu", "", false, "output hashes in the format used by md5sum")
}
flag.BoolVarP(&opts.ignore, "ignore-missing", "", false, "don't fail or report status for missing files")
flag.BoolVarP(&opts.quiet, "quiet", "q", false, "don't print OK for each successfully verified file")
flag.BoolVarP(&opts.recursive, "recursive", "r", false, "recurse into directories")
flag.BoolVarP(&opts.size, "size", "", false, "output size")
flag.BoolVarP(&opts.status, "status", "S", false, "don't output anything, status code shows success")
flag.BoolVarP(&opts.strict, "strict", "", false, "exit non-zero for improperly formatted checksum lines")
flag.BoolVarP(&opts.str, "string", "s", false, "treat arguments as strings")
flag.BoolVarP(&opts.followSymlinks, "symlinks", "L", false, "follow symbolic links while recursing directories")
flag.BoolVarP(&opts.verbose, "verbose", "v", false, "verbose operation")
flag.BoolVarP(&opts.version, "version", "", false, "show version and exit")
flag.BoolVarP(&opts.warn, "warn", "w", false, "warn about improperly formatted checksum lines")
flag.BoolVarP(&opts.zero, "zero", "z", false, "end each output line with NUL, not newline, and disable file name escaping")
flag.StringVarP(&opts.check, "check", "c", "\x00", "read checksums from file (use \"\" for stdin)")
flag.StringVarP(&opts.input, "input", "i", "\x00", "read pathnames from file (use \"\" for stdin)")
flag.StringVarP(&opts.key, "hmac", "H", "\x00", "key for HMAC (in hexadecimal) or read from specified pathname")
if strings.Contains(progname, "sum") {
flag.StringVarP(&opts.format, "format", "f", gnuFormat, "output format")
} else {
flag.StringVarP(&opts.format, "format", "f", bsdFormat, "output format")
}
// Allow xhash to be hard-linked to "md5sum" or "md5", etc and only use that algorithm
if !strings.HasPrefix(progname, "xhash") {
cmd := strings.TrimSuffix(strings.TrimSuffix(progname, ".exe"), "sum")
var defaults = map[string]crypto.Hash{
"b2": crypto.BLAKE2b_512,
"b3": BLAKE3,
"md5": crypto.MD5,
"sha1": crypto.SHA1,
"sha256": crypto.SHA256,
"sha512": crypto.SHA512,
}
if h, ok := defaults[cmd]; ok {
hashes = []crypto.Hash{h}
}
}
algorithms = make(map[crypto.Hash]*Algorithm)
for _, h := range hashes {
if h == BLAKE3 {
algorithms[h] = &Algorithm{name: "BLAKE3"}
} else if h.Available() {
algorithms[h] = &Algorithm{
name: strings.ReplaceAll(strings.ReplaceAll(h.String(), "SHA-", "SHA"), "/", "-"),
}
}
if _, ok := algorithms[h]; ok {
if strings.HasPrefix(progname, "xhash") {
flag.BoolVar(
&algorithms[h].check,
strings.ToLower(algorithms[h].name),
false,
algorithms[h].name+" algorithm")
}
}
}
flag.Parse()
if opts.input != "\x00" && opts.check != "\x00" {
log.Fatal("The --input & --check options are mutually exclusive")
}
if opts.version {
fmt.Printf("v%s %v %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
fmt.Printf("Supported hashes:")
for _, h := range hashes {
fmt.Printf(" %s", algorithms[h].name)
}
fmt.Println()
os.Exit(0)
}
if strings.HasPrefix(progname, "xhash") {
if opts.all {
// Ignore algorithm if --all was specified
for h := range algorithms {
algorithms[h].check = !algorithms[h].check
}
}
// Initialize chosen and populate name2Hash
for _, h := range hashes {
available := h == BLAKE3 || h.Available()
if available && algorithms[h].check {
chosen = append(chosen, h)
}
name2Hash[algorithms[h].name] = h
}
if opts.check == "\x00" && len(chosen) == 0 {
// SHA-256 is default
chosen = append(chosen, crypto.SHA256)
}
} else {
chosen = []crypto.Hash{hashes[0]}
name2Hash[algorithms[hashes[0]].name] = hashes[0]
}
if opts.key != "\x00" {
var err error
if opts.key == "" {
if macKey, err = io.ReadAll(os.Stdin); err != nil {
log.Fatal(err)
}
} else if macKey, err = hex.DecodeString(opts.key); err != nil {
if macKey, err = os.ReadFile(opts.key); err != nil {
log.Fatal(err)
}
}
}
if opts.gnu {
opts.format = gnuFormat
} else if opts.tag {
opts.format = bsdFormat
}
opts.format = unescape(opts.format)
if opts.zero && !strings.Contains(opts.format, "\x00") {
opts.format = strings.ReplaceAll(opts.format, "\n", "\x00")
}
var err error
format, err = format.Parse(opts.format)
if err != nil {
log.Fatal(err)
}
if strings.Contains(progname, "sum") {
opts.gnu = true
}
}
func openFileOrStdin(filename string) io.ReadCloser {
var err error
f := os.Stdin
if filename != "" {
if f, err = os.Open(filename); err != nil {
log.Fatal(err)
}
}
return f
}
func main() {
var lines <-chan *Checksums
if opts.check != "\x00" {
onError := ErrorIgnore
if opts.warn {
onError = ErrorWarn
} else if opts.strict {
onError = ErrorExit
}
f := openFileOrStdin(opts.check)
defer f.Close()
lines = inputFromCheck(f, opts.zero, onError)
} else if opts.input != "\x00" {
f := openFileOrStdin(opts.input)
defer f.Close()
lines = inputFromFile(f, opts.zero)
} else if flag.NArg() == 0 {
printChecksums(hashStdin(), opts)
os.Exit(0)
} else if opts.recursive {
lines = inputFromDir(flag.Args(), opts.followSymlinks)
} else if opts.str {
for _, s := range flag.Args() {
printChecksums(hashString(s), opts)
}
os.Exit(0)
} else {
lines = inputFromArgs(flag.Args())
}
var unreadable atomic.Uint64
g := new(errgroup.Group)
g.SetLimit(runtime.NumCPU())
checksums := make(chan *Checksums)
go func() {
defer close(checksums)
for line := range lines {
g.Go(func() error {
if checksum, err := hashFile(line.file, line.checksums); err != nil {
unreadable.Add(1)
if !opts.ignore {
log.Print(err)
}
} else {
checksums <- checksum
}
return nil
})
}
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}()
if opts.check == "\x00" {
for checksum := range checksums {
printChecksums(checksum, opts)
}
os.Exit(0)
}
// Handle -c option
unmatched := 0
for checksum := range checksums {
unmatched += printCheckResults(checksum)
}
unreadableFiles := unreadable.Load()
if opts.check != "\x00" || opts.input != "\x00" {
plural := ""
if !opts.status && unreadableFiles > 0 {
if unreadableFiles > 1 {
plural = "s"
}
fmt.Fprintf(os.Stderr, "WARNING: %d listed file%s could not be read\n", unreadableFiles, plural)
}
if !opts.status && unmatched > 0 {
if unmatched > 1 {
plural = "s"
}
fmt.Fprintf(os.Stderr, "WARNING: %d computed checksum%s did NOT match\n", unmatched, plural)
}
}
if unreadableFiles > 0 || unmatched > 0 {
os.Exit(1)
}
}