diff options
-rw-r--r-- | LICENSE | 13 | ||||
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | main.go | 71 |
3 files changed, 93 insertions, 0 deletions
@@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + + 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`. @@ -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 +} |