go で暗号化:md5 でチェックサムの計算と検証
はじめに
go の暗号化処理についてまとめます。
今回は crypto/md5
でチェックサムの計算と検証を行います。
crypto/md5
md5 のアルゴリズムはwikiがわかりやすいです。
よくファイルダウンロードの改ざん検知で見かけるやつですね。
checksumで比較することで簡単に改ざん検知ができそうです。
package main import ( "crypto/md5" "fmt" "io" ) func main() { h := md5.New() msg := "This is go-encryption md5 example" io.WriteString(h, msg) checksum := h.Sum(nil) fmt.Printf("length: %d byte \n", len(checksum)) fmt.Printf("output : %x \n", checksum) // output : 1eee48b333321a3e75b84357a562cee1 fmt.Printf("output1: %x\n", md5.Sum([]byte(msg))) // output1: 1eee48b333321a3e75b84357a562cee1 fmt.Printf("output2: %x\n", md5.Sum([]byte(msg+"a"))) // output2: 5b0e3e957616ce4610baaba2cb74388e }