golang 中的md5 、hmac、sha1算法的简单实现

前端之家收集整理的这篇文章主要介绍了golang 中的md5 、hmac、sha1算法的简单实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package main
  2.  
  3. import (
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "crypto/sha1"
  7. "encoding/hex"
  8. "fmt"
  9. )
  10.  
  11. func Md5(data string) string {
  12. md5 := md5.New()
  13. md5.Write([]byte(data))
  14. md5Data := md5.Sum([]byte(""))
  15. return hex.EncodeToString(md5Data)
  16. }
  17.  
  18. func Hmac(key,data string) string {
  19. hmac := hmac.New(md5.New,[]byte(key))
  20. hmac.Write([]byte(data))
  21. return hex.EncodeToString(hmac.Sum([]byte("")))
  22. }
  23.  
  24. func Sha1(data string) string {
  25. sha1 := sha1.New()
  26. sha1.Write([]byte(data))
  27. return hex.EncodeToString(sha1.Sum([]byte("")))
  28. }
  29.  
  30. func main() {
  31. fmt.Println(Md5("hello"))
  32. fmt.Println(Hmac("key2","hello"))
  33. fmt.Println(Sha1("hello"))
  34. }

猜你在找的Go相关文章