| 1 |
package md5simd |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/md5" |
| 5 |
"hash" |
| 6 |
"sync" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
// The blocksize of MD5 in bytes. |
| 11 |
BlockSize = 64 |
| 12 |
|
| 13 |
// The size of an MD5 checksum in bytes. |
| 14 |
Size = 16 |
| 15 |
|
| 16 |
// internalBlockSize is the internal block size. |
| 17 |
internalBlockSize = 32 << 10 |
| 18 |
) |
| 19 |
|
| 20 |
type Server interface { |
| 21 |
NewHash() Hasher |
| 22 |
Close() |
| 23 |
} |
| 24 |
|
| 25 |
type Hasher interface { |
| 26 |
hash.Hash |
| 27 |
Close() |
| 28 |
} |
| 29 |
|
| 30 |
// StdlibHasher returns a Hasher that uses the stdlib for hashing. |
| 31 |
// Used hashers are stored in a pool for fast reuse. |
| 32 |
func StdlibHasher() Hasher { |
| 33 |
return &md5Wrapper{Hash: md5Pool.New().(hash.Hash)} |
| 34 |
} |
| 35 |
|
| 36 |
// md5Wrapper is a wrapper around the builtin hasher. |
| 37 |
type md5Wrapper struct { |
| 38 |
hash.Hash |
| 39 |
} |
| 40 |
|
| 41 |
var md5Pool = sync.Pool{New: func() interface{} { |
| 42 |
return md5.New() |
| 43 |
}} |
| 44 |
|
| 45 |
// fallbackServer - Fallback when no assembly is available. |
| 46 |
type fallbackServer struct { |
| 47 |
} |
| 48 |
|
| 49 |
// NewHash -- return regular Golang md5 hashing from crypto |
| 50 |
func (s *fallbackServer) NewHash() Hasher { |
| 51 |
return &md5Wrapper{Hash: md5Pool.New().(hash.Hash)} |
| 52 |
} |
| 53 |
|
| 54 |
func (s *fallbackServer) Close() { |
| 55 |
} |
| 56 |
|
| 57 |
func (m *md5Wrapper) Close() { |
| 58 |
if m.Hash != nil { |
| 59 |
m.Reset() |
| 60 |
md5Pool.Put(m.Hash) |
| 61 |
m.Hash = nil |
| 62 |
} |
| 63 |
} |