Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashCloser ¶
type HashCloser interface {
hash.Hash
// Close resets the hash to its initial state and puts it back into the pool.
Close() error
}
HashCloser represents a pooled hash.Hash.
func GetHash ¶
func GetHash(h crypto.Hash) HashCloser
GetHash returns a pooled hash function.
Example (Multiple) ¶
package main
import (
"crypto"
"encoding/hex"
"fmt"
"io"
"strings"
"github.com/webx-top/poolx/hashpool"
)
func main() {
// Get a SHA256 hash function from the pool.
sha256 := hashpool.GetHash(crypto.SHA256)
defer sha256.Close()
// Get an MD5 hash function from the pool.
md5 := hashpool.GetHash(crypto.MD5)
defer md5.Close()
// Hash something with the hash functions.
io.Copy(io.MultiWriter(sha256, md5), strings.NewReader("hello, world"))
// Get the hash sums.
fmt.Println("SHA256:", hex.EncodeToString(sha256.Sum(nil)))
fmt.Println("MD5:", hex.EncodeToString(md5.Sum(nil)))
}
Output: SHA256: 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b MD5: e4d7f1b4ed2e42d15898f4b27b019da4
Example (Sha256) ¶
package main
import (
"crypto"
"encoding/hex"
"fmt"
"io"
"strings"
"github.com/webx-top/poolx/hashpool"
)
func main() {
// Get a SHA256 hash function from the pool.
h := hashpool.GetHash(crypto.SHA256)
defer h.Close() // Dont forget to call Close! This puts the hash function back into the pool.
// Hash something.
io.Copy(h, strings.NewReader("hello, world"))
// Get the hash sum.
sum := h.Sum(nil)
fmt.Println("SHA256:", hex.EncodeToString(sum))
}
Output: SHA256: 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b
Click to show internal directories.
Click to hide internal directories.