ZBLOG

golang MD5

In Go, the crypto/md5 package provides a simple way to calculate the MD5 hash of a given string or byte slice.

Here is an example program that demonstrates how to use the crypto/md5 package:

  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. )
  7. func main() {
  8. data := []byte("hello world")
  9. // Calculate the MD5 hash
  10. hash := md5.Sum(data)
  11. // Convert the hash to a hex string
  12. hexHash := hex.EncodeToString(hash[:])
  13. fmt.Println(hexHash) // Output: 5eb63bbbe01eeed093cb22bb8f5acdc3
  14. }

In this example, we first define a byte slice containing the data we want to hash ("hello world"). We then call the md5.Sum() function with this data as input. This function returns an array of 16 bytes representing the MD5 hash.

To convert this byte array to a human-readable hex string, we use Go’s built-in hex.EncodeToString() function. Finally, we print out the resulting hex string ("5eb63bbbe01eeed093cb22bb8f5acdc3") using fmt.Println().

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5884

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?