Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created November 21, 2012 04:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tebeka/4123056 to your computer and use it in GitHub Desktop.
Save tebeka/4123056 to your computer and use it in GitHub Desktop.
md5
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment