sha.go 658 B

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