Golang實現(xiàn)Md5校驗的示例代碼
最近項目中有個需求,就是地圖文件下發(fā)后,接收方需要文件的md5值,和接收到的文件做比對,以免文件不完整,引起bug,于是測試了下本地文件和遠程文件的md5計算。
1、本地文件
要獲取指定本地文件的MD5值,你可以使用crypto/md5包來計算文件的MD5散列值。以下是一個示例代碼,演示了如何打開一個文件并計算其MD5值:
package main import ( "crypto/md5" "encoding/hex" "fmt" "io" "net/http" "os" ) func main() { Md5ByLocalFile() } func Md5ByLocalFile() { // // 指定文件路徑 filePath := "d:/code/000fa28f-c114-49fe-9699-8c7f8b2eb222.png" // filePath := "https://minio.dev.inrobot.cloud/map/000fa28f-c114-49fe-9699-8c7f8b2eb222.png" // 打開文件 file, err := os.Open(filePath) if err != nil { fmt.Println("Failed to open file:", err) return } defer file.Close() // 創(chuàng)建一個md5哈希對象 hasher := md5.New() // 將文件內(nèi)容讀入哈希對象 if _, err := io.Copy(hasher, file); err != nil { fmt.Println("Failed to read file:", err) return } // 計算并獲取哈希值 hashBytes := hasher.Sum(nil) // 將哈希值轉(zhuǎn)換為十六進制字符串 hashString := hex.EncodeToString(hashBytes) // 輸出MD5哈希值 fmt.Println("MD5 hash of the file:", hashString) }
輸出結(jié)果如下:
PS D:\gostudy2022\TimeScheduler\md5> go run .\main.go
LocalFile MD5 hash of the file: b4735b024f3552b1277671303149719b
2、遠程文件
遠程文件其實就是網(wǎng)絡(luò)中可訪問的資源文件,要獲取指定網(wǎng)絡(luò)地址的文件的MD5值,你需要先下載文件的內(nèi)容到內(nèi)存或磁盤上,然后再計算其MD5值。這里我展示一個示例,該示例使用net/http包來下載文件,并使用crypto/md5包來計算MD5值。以下是完整的示例代碼:
package main import ( "crypto/md5" "encoding/hex" "fmt" "io" "net/http" "os" ) func main() { Md5ByRemoteFile() } func Md5ByRemoteFile() { // 指定文件的URL url := "https://minio.dev.inrobot.cloud/map/000fa28f-c114-49fe-9699-8c7f8b2eb222.png" // 發(fā)起HTTP GET請求 resp, err := http.Get(url) if err != nil { fmt.Println("Failed to fetch file:", err) return } defer resp.Body.Close() // 檢查響應(yīng)狀態(tài)碼 if resp.StatusCode != http.StatusOK { fmt.Printf("Failed to fetch file: HTTP status code %d\n", resp.StatusCode) return } // 創(chuàng)建一個md5哈希對象 hasher := md5.New() // 將文件內(nèi)容讀入哈希對象 if _, err := io.Copy(hasher, resp.Body); err != nil { fmt.Println("Failed to read file:", err) return } // 計算并獲取哈希值 hashBytes := hasher.Sum(nil) // 將哈希值轉(zhuǎn)換為十六進制字符串 hashString := hex.EncodeToString(hashBytes) // 輸出MD5哈希值 fmt.Println("MD5 hash of the file:", hashString) }
這里遠程文件和上例的文件相同,輸出結(jié)果如下:
PS D:\gostudy2022\TimeScheduler\md5> go run .\main.go
RemoteFile MD5 hash of the file: b4735b024f3552b1277671303149719b
可以看到,本地文件和遠程文件的md5值是相同的,從而在項目中,我們可以根據(jù)md5值是否相同,判斷文件是不是同一個文件,有沒有被損壞或篡改。
補:golang 分片上傳md5校驗不一致問題
錯誤計算md5值
// 打開合成文件 complateFile, _ := os.Create("./test.zip") defer complateFile.Close() // 循環(huán)分片合成 for i := 0; i < 5; i++ { // ... 省略中間步驟 complateFile.Write(fileBuffer) } // 計算分片md5進行比對 m5 := md5.New() _, _ = io.Copy(m5, complateFile) complateFileMd5 := hex.EncodeToString(m5.Sum(nil)) if complateFileMd5 != originalFileMd5{ fmt.Println("md5驗證失敗") }
錯誤原因
在Go中,如果你在合并分片文件的過程中直接使用*os.File(即file指針)來計算MD5,這通常會導(dǎo)致計算錯誤,原因在于文件指針的位置。當(dāng)你打開一個文件并開始讀取時,文件指針默認位于文件的開頭。如果你在合并過程中不重置文件指針,每次讀取都會從上次停止的地方開始,而不是從頭開始,導(dǎo)致計算的MD5不正確。
解決方案
- 計算md5前重置文件指針到文件的開頭
complateFile.Seek(0,0)
- 合成文件后重新打開文件進行md5值計算
func(){ complateFile, _ := os.Create("./test.zip") defer complateFile.Close() for i := 0; i < 5; i++ { // ... 省略中間步驟 complateFile.Write(fileBuffer) } }() // 驗證文件的完整性 file, _ := os.Open("./test.zip") m5 := md5.New() _, _ = io.Copy(m5, file) complateFileMd5 := hex.EncodeToString(m5.Sum(nil)) if complateFileMd5 != originalFileMd5{ fmt.Println("md5驗證失敗") }
到此這篇關(guān)于Golang實現(xiàn)Md5校驗的示例代碼的文章就介紹到這了,更多相關(guān)Golang Md5校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Go語言如何實現(xiàn)中文簡繁轉(zhuǎn)換和拼音轉(zhuǎn)換
這篇文章主要為大家詳細介紹了在Go語言如何實現(xiàn)中文簡繁轉(zhuǎn)換和拼音轉(zhuǎn)換功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02解決golang處理http response碰到的問題和需要注意的點
這篇文章主要介紹了解決golang處理http response碰到的問題和需要注意的點,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Go中的格式化字符串fmt.Sprintf()和fmt.Printf()使用示例
這篇文章主要為大家介紹了Go中的格式化字符串fmt.Sprintf()和fmt.Printf()使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06MacOS中 VSCode 安裝 GO 插件失敗問題的快速解決方法
這篇文章主要介紹了MacOS中 VSCode 安裝 GO 插件失敗問題的快速解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05