md5.go 917 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package md5simd
  2. import (
  3. "crypto/md5"
  4. "hash"
  5. "sync"
  6. )
  7. const (
  8. // The blocksize of MD5 in bytes.
  9. BlockSize = 64
  10. // The size of an MD5 checksum in bytes.
  11. Size = 16
  12. // internalBlockSize is the internal block size.
  13. internalBlockSize = 32 << 10
  14. )
  15. type Server interface {
  16. NewHash() Hasher
  17. Close()
  18. }
  19. type Hasher interface {
  20. hash.Hash
  21. Close()
  22. }
  23. // md5Wrapper is a wrapper around the builtin hasher.
  24. type md5Wrapper struct {
  25. hash.Hash
  26. }
  27. var md5Pool = sync.Pool{New: func() interface{} {
  28. return md5.New()
  29. }}
  30. // fallbackServer - Fallback when no assembly is available.
  31. type fallbackServer struct {
  32. }
  33. // NewHash -- return regular Golang md5 hashing from crypto
  34. func (s *fallbackServer) NewHash() Hasher {
  35. return &md5Wrapper{Hash: md5Pool.New().(hash.Hash)}
  36. }
  37. func (s *fallbackServer) Close() {
  38. }
  39. func (m *md5Wrapper) Close() {
  40. if m.Hash != nil {
  41. m.Reset()
  42. md5Pool.Put(m.Hash)
  43. m.Hash = nil
  44. }
  45. }