You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
751 B

7 years ago
7 years ago
7 years ago
  1. package tmhash
  2. import (
  3. "crypto/sha256"
  4. "hash"
  5. )
  6. const (
  7. Size = 20
  8. BlockSize = sha256.BlockSize
  9. )
  10. type sha256trunc struct {
  11. sha256 hash.Hash
  12. }
  13. func (h sha256trunc) Write(p []byte) (n int, err error) {
  14. return h.sha256.Write(p)
  15. }
  16. func (h sha256trunc) Sum(b []byte) []byte {
  17. shasum := h.sha256.Sum(b)
  18. return shasum[:Size]
  19. }
  20. func (h sha256trunc) Reset() {
  21. h.sha256.Reset()
  22. }
  23. func (h sha256trunc) Size() int {
  24. return Size
  25. }
  26. func (h sha256trunc) BlockSize() int {
  27. return h.sha256.BlockSize()
  28. }
  29. // New returns a new hash.Hash.
  30. func New() hash.Hash {
  31. return sha256trunc{
  32. sha256: sha256.New(),
  33. }
  34. }
  35. // Sum returns the first 20 bytes of SHA256 of the bz.
  36. func Sum(bz []byte) []byte {
  37. hash := sha256.Sum256(bz)
  38. return hash[:Size]
  39. }