获取golang 编译好的包本身的md5值,在验证开源的程序有没有被修改时很有用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)
func HashFileMd5(filePath string) (string, error) {
var returnMD5String string
file, err := os.Open(filePath)
if err != nil {
return returnMD5String, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return returnMD5String, err
}
hashInBytes := hash.Sum(nil)[:16]
returnMD5String = hex.EncodeToString(hashInBytes)
return returnMD5String, nil
}
func main() {
hash, err := HashFileMd5(os.Args[0])
if err == nil {
fmt.Println(hash)
}
}
本文网址: https://golangnote.com/topic/192.html 转摘请注明来源