Add rsha256 - remote sha256 utility.

This commit is contained in:
Kyle Isom 2017-11-15 11:29:26 -08:00
parent f44bbc9eca
commit 0dc478746a
1 changed files with 54 additions and 0 deletions

54
cmd/rsha256/main.go Normal file
View File

@ -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)
}
}