go-asink/hash.go

26 lines
352 B
Go
Raw Normal View History

2013-02-10 23:39:23 -05:00
package main
import (
2013-02-11 23:17:12 -05:00
"crypto/sha256"
"fmt"
2013-02-10 23:39:23 -05:00
"io"
"os"
)
func HashFile(filename string) (string, error) {
hashfn := sha256.New()
infile, err := os.Open(filename)
2013-02-11 23:17:12 -05:00
if err != nil {
return "", err
}
2013-02-10 23:39:23 -05:00
defer infile.Close()
_, err = io.Copy(hashfn, infile)
2013-02-11 23:17:12 -05:00
if err != nil {
return "", err
}
2013-02-10 23:39:23 -05:00
return fmt.Sprintf("%x", hashfn.Sum(nil)), nil
}