From 0dc478746a8498a4a85a457dfbb0ed043e1c3d5f Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 15 Nov 2017 11:29:26 -0800 Subject: [PATCH] Add rsha256 - remote sha256 utility. --- cmd/rsha256/main.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cmd/rsha256/main.go diff --git a/cmd/rsha256/main.go b/cmd/rsha256/main.go new file mode 100644 index 0000000..6b447c0 --- /dev/null +++ b/cmd/rsha256/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "crypto/sha256" + "flag" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "path/filepath" + + "github.com/kisom/goutils/lib" +) + +func fetch(remote string) ([]byte, error) { + resp, err := http.Get(remote) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return body, nil +} + +func main() { + flag.Parse() + + for _, remote := range flag.Args() { + u, err := url.Parse(remote) + if err != nil { + lib.Warn(err, "parsing %s", remote) + continue + } + + name := filepath.Base(u.Path) + if name == "" { + lib.Warnx("source URL doesn't appear to name a file") + continue + } + + body, err := fetch(remote) + if err != nil { + lib.Warn(err, "fetching %s", remote) + continue + } + + h := sha256.Sum256(body) + fmt.Printf("%s: sha256=%x\n", name, h) + } +}