summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormjl <mjl@ueber.net>2012-08-19 20:45:31 +0200
committermjl <mjl@ueber.net>2012-08-19 20:45:31 +0200
commit1d9e0e6f7d589da79fa651eb68aef8c1a85085ea (patch)
tree438495e3fa1b2ab8f49cf18e9c352411fab18c5f
parent536522cddcd2b06109e936ac2efd5654d5695a5b (diff)
add md5sum
-rw-r--r--md5sum/md5sum.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/md5sum/md5sum.go b/md5sum/md5sum.go
new file mode 100644
index 0000000..a7beb5a
--- /dev/null
+++ b/md5sum/md5sum.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+ "crypto/md5"
+ "flag"
+ "fmt"
+ "io"
+ "os"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: md5sum [file ...]\n")
+ os.Exit(2)
+}
+
+func error(s string) {
+ fmt.Fprint(os.Stderr, s, "\n")
+ os.Exit(1)
+}
+
+func sum(f *os.File, path string) {
+ h := md5.New()
+ if _, err := io.Copy(h, f); err != nil {
+ fmt.Fprintf(os.Stderr, "read %s: %s\n", path, err)
+ return
+ }
+ if path == "" {
+ fmt.Printf("%x\n", h.Sum(nil))
+ } else {
+ fmt.Printf("%x\t%s\n", h.Sum(nil), path)
+ }
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ sum(os.Stdin, "")
+ } else {
+ for _, path := range args {
+ f, err := os.Open(path)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "open %s: %s\n", path, err)
+ continue
+ }
+ sum(f, path)
+ f.Close()
+ }
+ }
+ os.Exit(0)
+}