hmac.go 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package hash
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "crypto/sha512"
  8. )
  9. // HmacMD5 hmac md5 hash
  10. func HmacMD5(k, bs []byte) (hs []byte, err error) {
  11. h := hmac.New(md5.New, k)
  12. _, err = h.Write(bs)
  13. if err != nil {
  14. return
  15. }
  16. hs = h.Sum(nil)
  17. return
  18. }
  19. // HmacSHA1 hmac sha1 hash
  20. func HmacSHA1(k, bs []byte) (hs []byte, err error) {
  21. h := hmac.New(sha1.New, k)
  22. _, err = h.Write(bs)
  23. if err != nil {
  24. return
  25. }
  26. hs = h.Sum(nil)
  27. return
  28. }
  29. // HmacSHA256 hmac sha256 hash
  30. func HmacSHA256(k, bs []byte) (hs []byte, err error) {
  31. h := hmac.New(sha256.New, k)
  32. _, err = h.Write(bs)
  33. if err != nil {
  34. return
  35. }
  36. hs = h.Sum(nil)
  37. return
  38. }
  39. // HmacSHA512 hmac sha256 hash
  40. func HmacSHA512(k, bs []byte) (hs []byte, err error) {
  41. h := hmac.New(sha512.New, k)
  42. _, err = h.Write(bs)
  43. if err != nil {
  44. return
  45. }
  46. hs = h.Sum(nil)
  47. return
  48. }