digest.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package xmlenc
  2. import (
  3. "crypto/sha1"
  4. "crypto/sha256"
  5. "crypto/sha512"
  6. "hash"
  7. "golang.org/x/crypto/ripemd160"
  8. )
  9. type digestMethod struct {
  10. algorithm string
  11. hash func() hash.Hash
  12. }
  13. func (dm digestMethod) Algorithm() string {
  14. return dm.algorithm
  15. }
  16. func (dm digestMethod) Hash() hash.Hash {
  17. return dm.hash()
  18. }
  19. var (
  20. // SHA1 implements the SHA-1 digest method (which is considered insecure)
  21. SHA1 = digestMethod{
  22. algorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
  23. hash: sha1.New,
  24. }
  25. // SHA256 implements the SHA-256 digest method
  26. SHA256 = digestMethod{
  27. algorithm: "http://www.w3.org/2000/09/xmldsig#sha256",
  28. hash: sha256.New,
  29. }
  30. // SHA512 implements the SHA-512 digest method
  31. SHA512 = digestMethod{
  32. algorithm: "http://www.w3.org/2000/09/xmldsig#sha512",
  33. hash: sha512.New,
  34. }
  35. // RIPEMD160 implements the RIPEMD160 digest method
  36. RIPEMD160 = digestMethod{
  37. algorithm: "http://www.w3.org/2000/09/xmldsig#ripemd160",
  38. hash: ripemd160.New,
  39. }
  40. )
  41. func init() {
  42. RegisterDigestMethod(SHA1)
  43. RegisterDigestMethod(SHA256)
  44. RegisterDigestMethod(SHA512)
  45. RegisterDigestMethod(RIPEMD160)
  46. }