hashpool

package
v0.0.0-...-5cfa2d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2021 License: Apache-2.0, MIT Imports: 12 Imported by: 0

README

pool

Getting Started

go get -u github.com/webx-top/poolx/hashpool

Example

package main

import (
	"crypto"
	"encoding/hex"
	"fmt"
	"io"
	"strings"

	"github.com/webx-top/poolx/hashpool"
)

func main() {
    // Get a bytes.Buffer from the pool.
    buf := hashpool.GetBuffer()
    defer buf.Close() // Put the bytes.Buffer back into the pool!

    // Get a SHA256 hash function from the pool.
    h := hashpool.GetHash(crypto.SHA256)
    defer h.Close() // Put the hash function back into the pool!

    // Do something with the buffer and hash function.
    io.Copy(io.MultiWriter(buf, h), strings.NewReader("hello, world"))

    // Print the results.
    fmt.Printf("SHA256 %q: %s", buf.String(), hex.EncodeToString(h.Sum(nil)))
}
Output:
SHA256 "hello, world": 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL