Documentation ¶
Overview ¶
Package blake3 provides an SSE4.1/AVX2 accelerated BLAKE3 implementation.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveKey ¶ added in v0.0.3
DeriveKey derives a key based on reusable key material of any length, in the given context. The key will be stored in out, using all of its current length.
Context strings must be hardcoded constants, and the recommended format is "[application] [commit timestamp] [purpose]", e.g., "example.com 2019-12-25 16:18:03 session tokens v1".
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { out := make([]byte, 32) // See the documentation for good practices on what the context should be. blake3.DeriveKey( "my-application v0.1.1 session tokens v1", // context []byte("some material to derive key from"), // material out, ) fmt.Printf("%x\n", out) }
Output: 98a3333af735f89eb301b56eaf6a77713aa03cdb0057e5b04352a63ea9204add
func Sum256 ¶ added in v0.1.1
Sum256 returns the first 256 bits of the unkeyed digest of the data.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { digest := blake3.Sum256([]byte("some data")) fmt.Printf("%x\n", digest[:]) }
Output: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2
func Sum512 ¶ added in v0.1.1
Sum512 returns the first 512 bits of the unkeyed digest of the data.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { digest := blake3.Sum512([]byte("some data")) fmt.Printf("%x\n", digest[0:32]) fmt.Printf("%x\n", digest[32:64]) }
Output: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2 1b55688951738e3a7155d6398eb56c6bc35d5bca5f139d98eb7409be51d1be32
Types ¶
type Digest ¶ added in v0.0.3
type Digest struct {
// contains filtered or unexported fields
}
Digest captures the state of a Hasher allowing reading and seeking through the output stream.
func (*Digest) Read ¶ added in v0.0.3
Read reads data from the hasher into out. It always fills the entire buffer and never errors. The stream will wrap around when reading past 2^64 bytes.
func (*Digest) Seek ¶ added in v0.0.3
Seek sets the position to the provided location. Only SeekStart and SeekCurrent are allowed.
Example ¶
package main import ( "fmt" "io" "github.com/zeebo/blake3" ) func main() { h := blake3.New() h.Write([]byte("some data")) d := h.Digest() out := make([]byte, 32) d.Seek(32, io.SeekStart) d.Read(out) fmt.Printf("%x\n", out) }
Output: 1b55688951738e3a7155d6398eb56c6bc35d5bca5f139d98eb7409be51d1be32
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher is a hash.Hash for BLAKE3.
func New ¶
func New() *Hasher
New returns a new Hasher that has a digest size of 32 bytes.
If you need more or less output bytes than that, use Digest method.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { h := blake3.New() h.Write([]byte("some data")) fmt.Printf("%x\n", h.Sum(nil)) }
Output: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2
func NewDeriveKey ¶ added in v0.0.3
NewDeriveKey returns a Hasher that is initialized with the context string. See DeriveKey for details. It has a digest size of 32 bytes.
If you need more or less output bytes than that, use the Digest method.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { // See the documentation for good practices on what the context should be. h := blake3.NewDeriveKey("my-application v0.1.1 session tokens v1") h.Write([]byte("some material to derive key from")) fmt.Printf("%x\n", h.Sum(nil)) }
Output: 98a3333af735f89eb301b56eaf6a77713aa03cdb0057e5b04352a63ea9204add
func NewKeyed ¶ added in v0.0.3
NewKeyed returns a new Hasher that uses the 32 byte input key and has a digest size of 32 bytes.
If you need more or less output bytes than that, use the Digest method.
Example ¶
package main import ( "bytes" "fmt" "github.com/zeebo/blake3" ) func main() { h1, err := blake3.NewKeyed(bytes.Repeat([]byte("1"), 32)) if err != nil { panic(err) } h2, err := blake3.NewKeyed(bytes.Repeat([]byte("2"), 32)) if err != nil { panic(err) } h1.Write([]byte("some data")) h2.Write([]byte("some data")) fmt.Printf("%x\n", h1.Sum(nil)) fmt.Printf("%x\n", h2.Sum(nil)) }
Output: 107c6f88638356d73cdb80f4d56ffe50abcbd9664a80c8ab2b83b1f946ebaba1 b4be81075bef5a2448158ee5eeddaed897fe44a564c2cb088facbe7824a25073
func (*Hasher) BlockSize ¶
BlockSize implements part of the hash.Hash interface. It returns the most natural size to write to the Hasher.
func (*Hasher) Clone ¶ added in v0.2.0
Clone returns a new Hasher with the same internal state.
Modifying the resulting Hasher will not modify the original Hasher, and vice versa.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { h1 := blake3.New() h1.WriteString("some") h2 := h1.Clone() fmt.Println("before:") fmt.Printf("h1: %x\n", h1.Sum(nil)) fmt.Printf("h2: %x\n\n", h2.Sum(nil)) h2.WriteString(" data") fmt.Println("h2 modified:") fmt.Printf("h1: %x\n", h1.Sum(nil)) fmt.Printf("h2: %x\n\n", h2.Sum(nil)) h1.WriteString(" data") fmt.Println("h1 converged:") fmt.Printf("h1: %x\n", h1.Sum(nil)) fmt.Printf("h2: %x\n", h2.Sum(nil)) }
Output: before: h1: 2f610cf2e7e0dc09384cbaa75b2ae5d9704ac9a5ac7f28684342856e2867c707 h2: 2f610cf2e7e0dc09384cbaa75b2ae5d9704ac9a5ac7f28684342856e2867c707 h2 modified: h1: 2f610cf2e7e0dc09384cbaa75b2ae5d9704ac9a5ac7f28684342856e2867c707 h2: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2 h1 converged: h1: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2 h2: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2
func (*Hasher) Digest ¶ added in v0.0.3
Digest takes a snapshot of the hash state and returns an object that can be used to read and seek through 2^64 bytes of digest output.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { h := blake3.New() h.Write([]byte("some data")) d := h.Digest() out := make([]byte, 64) d.Read(out) fmt.Printf("%x\n", out[0:32]) fmt.Printf("%x\n", out[32:64]) }
Output: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2 1b55688951738e3a7155d6398eb56c6bc35d5bca5f139d98eb7409be51d1be32
func (*Hasher) Reset ¶
func (h *Hasher) Reset()
Reset implements part of the hash.Hash interface. It causes the Hasher to act as if it was newly created.
Example ¶
package main import ( "fmt" "github.com/zeebo/blake3" ) func main() { h := blake3.New() h.Write([]byte("some data")) fmt.Printf("%x\n", h.Sum(nil)) h.Reset() h.Write([]byte("some data")) fmt.Printf("%x\n", h.Sum(nil)) }
Output: b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2 b224a1da2bf5e72b337dc6dde457a05265a06dec8875be379e2ad2be5edb3bf2
func (*Hasher) Size ¶
Size implements part of the hash.Hash interface. It returns the number of bytes the hash will output in Sum.
func (*Hasher) Sum ¶
Sum implements part of the hash.Hash interface. It appends the digest of the Hasher to the provided buffer and returns it.