commit e523a510e1188dd0314475df85d148b7e33224e5 Author: stephen mcquay Date: Tue Dec 8 10:18:42 2015 -0800 init diff --git a/license b/license new file mode 100644 index 0000000..68bbb1b --- /dev/null +++ b/license @@ -0,0 +1,20 @@ +MIT License +Copyright (c) 2015 smcquay + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/main.go b/main.go new file mode 100644 index 0000000..9869453 --- /dev/null +++ b/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "flag" + "fmt" + "io" + "os" +) + +var algo = flag.String("a", "sha256", "algorithm to use") + +func main() { + flag.Parse() + files := flag.Args() + h := sha256.New() + switch *algo { + case "sha1", "1": + h = sha1.New() + case "sha256", "256": + h = sha256.New() + case "sha512", "512": + h = sha512.New() + default: + fmt.Fprintf(os.Stderr, "unsupported algorithm: %v\n", *algo) + os.Exit(1) + } + + if len(files) == 0 { + _, err := io.Copy(h, os.Stdin) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + fmt.Printf("%x -\n", h.Sum(nil)) + } else { + for _, name := range files { + f, err := os.Open(name) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + continue + } + h.Reset() + _, err = io.Copy(h, f) + f.Close() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + continue + } + fmt.Printf("%x %s\n", h.Sum(nil), name) + } + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..0f02e1b --- /dev/null +++ b/readme.md @@ -0,0 +1,12 @@ +# cs + +calculate checksums + +It's a simpler version of shasum + md5sum, but only for sha1, sha256, and +sha512. + +## usage + + cs -a 256 < foo.txt + cs foo.txt + cs -a sha1 foo.txt foo.txt foo.txt