hash.go 591 B

12345678910111213141516171819202122232425262728293031323334
  1. package common
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. )
  8. func Sha256Raw(data []byte) []byte {
  9. h := sha256.New()
  10. h.Write(data)
  11. return h.Sum(nil)
  12. }
  13. func Sha1Raw(data []byte) []byte {
  14. h := sha1.New()
  15. h.Write(data)
  16. return h.Sum(nil)
  17. }
  18. func Sha1(data []byte) string {
  19. return hex.EncodeToString(Sha1Raw(data))
  20. }
  21. func HmacSha256Raw(message, key []byte) []byte {
  22. h := hmac.New(sha256.New, key)
  23. h.Write(message)
  24. return h.Sum(nil)
  25. }
  26. func HmacSha256(message, key string) string {
  27. return hex.EncodeToString(HmacSha256Raw([]byte(message), []byte(key)))
  28. }