计算文件md5的各种实现方式

最近在对接对象存储,为了确保上传到对象存储的文件没有被损坏,所以需要计算文件的md5值,以确保文件的完整性。
现分享下目前工作中用到的各语言的md5的计算方式:

linux shell

md5sum ./cheshi.txt

windows cmd

certutil.exe -hashfile .\ceshi.txt MD5

python

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)
}
posted @ 2021-10-18 22:02  Rainbowhhy  阅读(838)  评论(0编辑  收藏  举报