最近在对接对象存储,为了确保上传到对象存储的文件没有被损坏,所以需要计算文件的md5值,以确保文件的完整性。
现分享下目前工作中用到的各语言的md5的计算方式:
md5sum ./cheshi.txtwindows cmd
certutil.exe -hashfile .ceshi.txt MD5python
import hashlib
if __name__ == "__main__":
with open("D:\ceshi.txt", "rb") as f:
data = f.read()
file_md5 = hashlib.md5(data).hexdigest()
print(file_md5)
golang
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)
func main() {
h := md5.New()
f, err := os.Open("D:\ceshi.txt")
if err != nil {
fmt.Println(err)
return
}
io.Copy(h, f)
file_md5 := hex.EncodeToString(h.Sum(nil))
fmt.Println(file_md5)
}



