hash.go 717 B

12345678910111213141516171819202122232425
  1. package internal
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "strconv"
  6. "github.com/cespare/xxhash/v2"
  7. )
  8. // SHA256sum computes a cryptographic hash. Still used for proof-of-work challenges
  9. // where we need the security properties of a cryptographic hash function.
  10. func SHA256sum(text string) string {
  11. hash := sha256.New()
  12. hash.Write([]byte(text))
  13. return hex.EncodeToString(hash.Sum(nil))
  14. }
  15. // FastHash is a high-performance non-cryptographic hash function suitable for
  16. // internal caching, policy rule identification, and other performance-critical
  17. // use cases where cryptographic security is not required.
  18. func FastHash(text string) string {
  19. h := xxhash.Sum64String(text)
  20. return strconv.FormatUint(h, 16)
  21. }