go-asink/client/hash.go

27 lines
378 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) {
2013-08-13 21:55:17 -04:00
//TODO change to sha512?
2013-02-10 23:39:23 -05:00
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
}