X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=crypto%2Fsha3pool%2Fpool.go;fp=crypto%2Fsha3pool%2Fpool.go;h=ca69b6a1539e6f6191be5dc5a795de097361682f;hp=0000000000000000000000000000000000000000;hb=db158dcf09436b003defd333f1a665e7e051d820;hpb=d09b7a78d44dc259725902b8141cdba0d716b121 diff --git a/crypto/sha3pool/pool.go b/crypto/sha3pool/pool.go new file mode 100644 index 00000000..ca69b6a1 --- /dev/null +++ b/crypto/sha3pool/pool.go @@ -0,0 +1,31 @@ +// Package sha3pool is a freelist for SHA3-256 hash objects. +package sha3pool + +import ( + "sync" + + "golang.org/x/crypto/sha3" +) + +var pool = &sync.Pool{New: func() interface{} { return sha3.New256() }} + +// Get256 returns an initialized SHA3-256 hash ready to use. +// It is like sha3.New256 except it uses the freelist. +// The caller should call Put256 when finished with the returned object. +func Get256() sha3.ShakeHash { + return pool.Get().(sha3.ShakeHash) +} + +// Put256 resets h and puts it in the freelist. +func Put256(h sha3.ShakeHash) { + h.Reset() + pool.Put(h) +} + +// Sum256 uses a ShakeHash from the pool to sum into hash. +func Sum256(hash, data []byte) { + h := Get256() + h.Write(data) + h.Read(hash) + Put256(h) +}