encode.go 743 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package util
  4. import (
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/sha1"
  8. "crypto/sha256"
  9. "encoding/hex"
  10. )
  11. // MD5 将字符串 s 进行 md5 编码
  12. func MD5(s string) string {
  13. m := md5.New()
  14. m.Write([]byte(s))
  15. return hex.EncodeToString(m.Sum(nil))
  16. }
  17. // SHA256 将字符串 s 进行 SHA256 编码
  18. func SHA256(s string) string {
  19. h := sha256.New()
  20. h.Write([]byte(s))
  21. return hex.EncodeToString(h.Sum(nil))
  22. }
  23. // HMACBySHA1 将字符串 s 进行 以SHA1算法生成 HMAC (哈希运算消息认证码)
  24. func HMACBySHA1(s, key string) []byte {
  25. bkey := []byte(key)
  26. mac := hmac.New(sha1.New, bkey)
  27. mac.Write([]byte(s))
  28. return mac.Sum(nil)
  29. }