shake

package
v0.41.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package shake provides hash.Hash compatible wrappers for SHAKE XOFs.

SHAKE (Secure Hash Algorithm Keccak) is an extendable-output function (XOF) that can produce outputs of arbitrary length. This package adapts SHAKE128 and SHAKE256 to the standard hash.Hash interface by fixing the output size.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSHAKE128

func NewSHAKE128(outputSize int) hash.Hash

NewSHAKE128 creates a hash.Hash wrapper for SHAKE128 with the specified output size.

SHAKE128 provides 128-bit security. Common output sizes:

  • 16 bytes (128 bits): matches security level
  • 32 bytes (256 bits): extended output
Example
// Create a SHAKE128 hash with 32-byte output
h := NewSHAKE128(32)
h.Write([]byte("Hello, World!"))
_ = h.Sum(nil)

// Can call Sum multiple times
_ = h.Sum(nil)

// Continue writing
h.Write([]byte(" More data"))
_ = h.Sum(nil)

func NewSHAKE256

func NewSHAKE256(outputSize int) hash.Hash

NewSHAKE256 creates a hash.Hash wrapper for SHAKE256 with the specified output size.

SHAKE256 provides 256-bit security. Common output sizes:

  • 32 bytes (256 bits): matches security level
  • 64 bytes (512 bits): extended output
Example
// Create a SHAKE256 hash with 64-byte output
h := NewSHAKE256(64)
h.Write([]byte("Secure message"))
_ = h.Sum(nil)

Types

type HashAdapter

type HashAdapter struct {
	// contains filtered or unexported fields
}

HashAdapter adapts a SHAKE XOF to the hash.Hash interface with fixed output size.

The adapter maintains idempotency of Sum() by cloning the internal state before reading output, ensuring multiple Sum() calls produce the same result without affecting subsequent Write() operations.

func (*HashAdapter) BlockSize

func (h *HashAdapter) BlockSize() int

BlockSize returns the hash's underlying block size.

SHAKE128 has a block size of 168 bytes. SHAKE256 has a block size of 136 bytes.

func (*HashAdapter) Reset

func (h *HashAdapter) Reset()

Reset resets the hash to its initial state.

func (*HashAdapter) Size

func (h *HashAdapter) Size() int

Size returns the number of bytes Sum will return.

func (*HashAdapter) Sum

func (h *HashAdapter) Sum(b []byte) []byte

Sum appends the current hash to b and returns the resulting slice.

Sum does not change the underlying hash state, allowing it to be called multiple times with the same result. This is achieved by marshaling and unmarshaling the internal XOF state.

Example
h := NewSHAKE128(32)
h.Write([]byte("data"))

// Sum can be called multiple times
sum1 := h.Sum(nil)
sum2 := h.Sum(nil)

// Results are identical
_ = bytes.Equal(sum1, sum2) // true

// Can append to existing slice
prefix := []byte("hash:")
result := h.Sum(prefix)
_ = result // []byte("hash:" + sum1...)

func (*HashAdapter) Write

func (h *HashAdapter) Write(p []byte) (n int, err error)

Write adds data to the running hash. It never returns an error.

Write can be called multiple times to feed data incrementally.

Jump to

Keyboard shortcuts

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