sha.go 834 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package hash
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "fmt"
  8. "io"
  9. )
  10. func ComputeHmacSHA1(data string, secret string) string {
  11. key := []byte(secret)
  12. h := hmac.New(sha1.New, key)
  13. h.Write([]byte(data))
  14. return fmt.Sprintf("%x", h.Sum(nil))
  15. }
  16. func ComputeSHA1(data string) string {
  17. t := sha1.New()
  18. _, _ = io.WriteString(t, data)
  19. return fmt.Sprintf("%x", t.Sum(nil))
  20. }
  21. func ComputeMD5(data string) string {
  22. t := md5.New()
  23. _, _ = io.WriteString(t, data)
  24. return fmt.Sprintf("%x", t.Sum(nil))
  25. }
  26. func ComputeHmacSha256(message string, secret string) string {
  27. key := []byte(secret)
  28. h := hmac.New(sha256.New, key)
  29. h.Write([]byte(message))
  30. return fmt.Sprintf("%x", h.Sum(nil))
  31. }
  32. func ComputeSha256(message string) []byte {
  33. p := []byte(message)
  34. h := sha256.New()
  35. h.Write(p)
  36. return h.Sum(nil)
  37. }