go-asink/asink/hash.go

31 lines
441 B
Go
Raw Normal View History

2013-09-04 22:02:17 -04:00
/*
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
*/
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
}