You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
519 B
32 lines
519 B
package util
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// 计算文件的hash
|
|
func CalacHash(rfile string) string {
|
|
// 获取到真实地址
|
|
//
|
|
file, err := os.Open(rfile)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// initlize hash object
|
|
hash := sha256.New()
|
|
|
|
// write file into hash object
|
|
if _, err := io.Copy(hash, file); err != nil {
|
|
panic(err)
|
|
}
|
|
// get hash value
|
|
hashBytes := hash.Sum(nil)
|
|
//converto to hash string
|
|
hastString := fmt.Sprintf("%x", hashBytes)
|
|
return hastString
|
|
}
|
|
|