hash.go 875 B

12345678910111213141516171819202122232425262728293031
  1. package dns
  2. import (
  3. "bytes"
  4. "crypto"
  5. "hash"
  6. )
  7. // identityHash will not hash, it only buffers the data written into it and returns it as-is.
  8. type identityHash struct {
  9. b *bytes.Buffer
  10. }
  11. // Implement the hash.Hash interface.
  12. func (i identityHash) Write(b []byte) (int, error) { return i.b.Write(b) }
  13. func (i identityHash) Size() int { return i.b.Len() }
  14. func (i identityHash) BlockSize() int { return 1024 }
  15. func (i identityHash) Reset() { i.b.Reset() }
  16. func (i identityHash) Sum(b []byte) []byte { return append(b, i.b.Bytes()...) }
  17. func hashFromAlgorithm(alg uint8) (hash.Hash, crypto.Hash, error) {
  18. hashnumber, ok := AlgorithmToHash[alg]
  19. if !ok {
  20. return nil, 0, ErrAlg
  21. }
  22. if hashnumber == 0 {
  23. return identityHash{b: &bytes.Buffer{}}, hashnumber, nil
  24. }
  25. return hashnumber.New(), hashnumber, nil
  26. }