sha3df

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

SHA-3 Derived Functions

Go Reference

The following SHA-3 Derived Functions specified in NIST SP 800-185 are implemented:

  • ParallelHash

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SumParallelHash128

func SumParallelHash128(data []byte, blockSize, length int, S []byte) []byte

SumParallelHash128 applies the cSHAKE128 extendable output function to data in blocks of blockSize bytes, and returns an output of the given length in bytes. S is a customization byte string used for domain separation.

func SumParallelHash256

func SumParallelHash256(data []byte, blockSize, length int, S []byte) []byte

SumParallelHash256 applies the cSHAKE256 extendable output function to data in blocks of blockSize bytes, and returns an output of the given length in bytes. S is a customization byte string used for domain separation.

Types

type ParallelHash

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

ParallelHash is an instance of a ParallelHash function specified by the NIST SP 800-185 standard.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/shizhMSFT/sha3df"
)

func main() {
	// Create a new ParallelHash instance with the desired block size
	blockSize := 8
	ph := sha3df.NewParallelHash256(blockSize, nil)

	// Prepare the data to be hashed
	data := []byte{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
	}
	blockCount := (len(data) + blockSize - 1) / blockSize

	// Parallel hashing individual blocks with multiple goroutines
	blockHashes := make([][]byte, blockCount)
	var wg sync.WaitGroup
	for i := range blockCount {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			var block []byte
			if i == blockCount-1 {
				block = data[i*blockSize:]
			} else {
				block = data[i*blockSize : (i+1)*blockSize]
			}
			blockHashes[i] = ph.SumBlock(block)
		}(i)
	}
	wg.Wait()

	// Commit the hashes and retrieve the final hash
	ph.WriteBlockHashes(blockHashes)
	hash := make([]byte, 64)
	ph.Commit(len(hash))
	ph.Read(hash)

	// Print the final hash
	fmt.Printf("%x", hash)
}
Output:
bc1ef124da34495e948ead207dd9842235da432d2bbc54b4c110e64c451105531b7f2a3e0ce055c02805e7c2de1fb746af97a1dd01f43b824e31b87612410429

func NewParallelHash128

func NewParallelHash128(blockSize int, S []byte) *ParallelHash

NewParallelHash128 creates a new ParallelHash128. blockSize is the block size in bytes for parallel hashing. S is a customization byte string used for domain separation.

func NewParallelHash256

func NewParallelHash256(blockSize int, S []byte) *ParallelHash

NewParallelHash256 creates a new ParallelHash256. blockSize is the block size in bytes for parallel hashing. S is a customization byte string used for domain separation.

func (*ParallelHash) BlockSize

func (ph *ParallelHash) BlockSize() int

BlockSize returns the block size in bytes for parallel hashing.

func (*ParallelHash) Commit

func (ph *ParallelHash) Commit(length int) error

Commit finalizes the hash and prepares it for reading. Setting the length to 0 makes ParallelHash a ParallelHashXOR function with arbitrary-length output.

func (*ParallelHash) Read

func (ph *ParallelHash) Read(p []byte) (n int, err error)

Read squeezes more output from the ParallelHash.

func (*ParallelHash) Reset

func (ph *ParallelHash) Reset()

Reset resets the ParallelHash to its initial state.

func (*ParallelHash) SumBlock

func (ph *ParallelHash) SumBlock(data []byte) []byte

SumBlock applies the cSHAKE extendable output function to a block of data and returns the hash of the block.

func (*ParallelHash) WriteBlockHash

func (ph *ParallelHash) WriteBlockHash(h []byte) error

WriteBlockHash writes the hash of a block to the ParallelHash.

func (*ParallelHash) WriteBlockHashes

func (ph *ParallelHash) WriteBlockHashes(hashes [][]byte) error

WriteBlockHashes writes multiple block hashes to the ParallelHash.

Jump to

Keyboard shortcuts

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