public
Created

md5

  • Download Gist
md5.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package main
 
import (
"crypto/md5"
"fmt"
"flag"
"io"
"os"
)
 
// open open a file, the filename is - will return os.Stdin
func open(filename string) (io.ReadCloser, error) {
if filename == "-" {
return os.Stdin, nil
}
 
file, err := os.Open(filename)
if err != nil {
return nil, err
}
 
return file, err
}
 
func main() {
var filename string
flag.StringVar(&filename, "file", "-", "file name")
flag.Parse()
 
file, err := open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error: can't open %s - %s\n", filename, err)
os.Exit(1)
}
defer file.Close()
 
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
fmt.Fprintf(os.Stderr, "error: can't read %s - %s\n", filename, err)
os.Exit(1)
}
 
fmt.Printf("%x\n", hash.Sum(nil))
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.