From 2145d6d7b56f44a2698529c809186037aba893cc Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 26 Jun 2017 11:51:11 +0200 Subject: Initial commit --- LICENSE | 13 ++++++++++++ README.md | 9 ++++++++ main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec23b69 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +sha3sum +======= + +Simple program to expose `golang.org/x/crypto/sha3` on the command line (similar to the `sha1sum` utility) + +Usage +----- + +Call the program with a list of filenames, these will be hashed. The filename '-' represents stdin. When no argument is passed, hash stdin. Optionally you can specify the hash size (e.g. 256, 512) with the optional `-algo` flag. See `sha3sum -help`. diff --git a/main.go b/main.go new file mode 100644 index 0000000..d73db50 --- /dev/null +++ b/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "encoding/hex" + "flag" + "fmt" + "golang.org/x/crypto/sha3" + "hash" + "io" + "os" +) + +var ( + algo = flag.String("algo", "256", "Which algo should be used (224,256,384,512)") +) + +var algos = map[string]func() hash.Hash{ + "256": sha3.New256, + "224": sha3.New224, + "384": sha3.New384, + "512": sha3.New512, +} + +func main() { + flag.Parse() + + files := append([]string{}, flag.Args()...) + + if len(files) == 0 { + files = append(files, "-") + } + + for _, fn := range files { + if !doFile(fn) { + os.Exit(1) + } + } +} + +func doFile(fn string) bool { + var r io.Reader + + if fn == "-" { + r = os.Stdin + } else { + f, err := os.Open(fn) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return false + } + defer f.Close() + + r = f + } + + gethash, ok := algos[*algo] + if !ok { + fmt.Fprintf(os.Stderr, "Unknown algo: %s\n", *algo) + return false + } + + hash := gethash() + + if _, err := io.Copy(hash, r); err != nil { + fmt.Fprintln(os.Stderr, err) + return false + } + + fmt.Printf("%s %s\n", hex.EncodeToString(hash.Sum([]byte{})), fn) + return true +} -- cgit v1.2.3-70-g09d2