hash.go raw

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