digest.go 845 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package crypto4go
  2. import (
  3. "crypto/md5"
  4. "crypto/sha1"
  5. "crypto/sha256"
  6. "crypto/sha512"
  7. "encoding/hex"
  8. )
  9. func MD5(value []byte) []byte {
  10. var m = md5.New()
  11. m.Write(value)
  12. return m.Sum(nil)
  13. }
  14. func MD5String(value string) string {
  15. return hex.EncodeToString(MD5([]byte(value)))
  16. }
  17. func SHA1(value []byte) []byte {
  18. var s = sha1.New()
  19. s.Write(value)
  20. return s.Sum(nil)
  21. }
  22. func SHA1String(value string) string {
  23. return hex.EncodeToString(SHA1([]byte(value)))
  24. }
  25. func SHA256(value []byte) []byte {
  26. var s = sha256.New()
  27. s.Write(value)
  28. return s.Sum(nil)
  29. }
  30. func SHA256String(value string) string {
  31. return hex.EncodeToString(SHA256([]byte(value)))
  32. }
  33. func SHA512(value []byte) []byte {
  34. var s = sha512.New()
  35. s.Write(value)
  36. return s.Sum(nil)
  37. }
  38. func SHA512String(value string) string {
  39. return hex.EncodeToString(SHA512([]byte(value)))
  40. }