shake

package
v0.0.0-...-0310684 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: BSD-3-Clause, BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package shake provides implementation of SHA-3 and cSHAKE This code has been copied from golang.org/x/crypto/sha3 and heavily modified. This version doesn't use heap when computing cSHAKE. It makes it possible to allocate heap once when object is created and then reuse heap allocated structures in subsequent calls.

Example (Mac)
k := []byte("this is a secret key; you should generate a strong random key that's at least 32 bytes long")
buf := []byte("and this is some data to authenticate")
// A MAC with 32 bytes of output has 256-bit security strength -- if you use at least a 32-byte-long key.
h := make([]byte, 32)
d := NewShake256()
// Write the key into the hash.
_, _ = d.Write(k)
// Now write the data.
_, _ = d.Write(buf)
// Read 32 bytes of output from the hash into h.
_, _ = d.Read(h)
fmt.Printf("%x\n", h)
Output:

78de2974bd2711d5549ffd32b753ef0f5fa80a0db2556db60f0987eb8a9218ff
Example (Sum)
buf := []byte("some data to hash")
// A hash needs to be 64 bytes long to have 256-bit collision resistance.
h := make([]byte, 64)
// Compute a 64-byte hash of buf and put it in h.
shake := NewShake256()
_, _ = shake.Write(buf)
_, _ = shake.Read(h)
fmt.Printf("%x\n", h)
Output:

0f65fe41fc353e52c55667bb9e2b27bfcc8476f2c413e9437d272ee3194a4e3146d05ec04a25d16b8f577c19b82d16b1424c3e022e783d2b4da98de3658d363d

Index

Examples

Constants

This section is empty.

Variables

View Source
var RC = [24]uint64{
	0x0000000000000001,
	0x0000000000008082,
	0x800000000000808A,
	0x8000000080008000,
	0x000000000000808B,
	0x0000000080000001,
	0x8000000080008081,
	0x8000000000008009,
	0x000000000000008A,
	0x0000000000000088,
	0x0000000080008009,
	0x000000008000000A,
	0x000000008000808B,
	0x800000000000008B,
	0x8000000000008089,
	0x8000000000008003,
	0x8000000000008002,
	0x8000000000000080,
	0x000000000000800A,
	0x800000008000000A,
	0x8000000080008081,
	0x8000000000008080,
	0x0000000080000001,
	0x8000000080008008,
}

RC stores the round constants for use in the ι step.

Functions

func KeccakF1600

func KeccakF1600(a *[25]uint64)

func NewLegacyKeccak256

func NewLegacyKeccak256() hash.Hash

Only use this function if you require compatibility with an existing cryptosystem that uses non-standard padding. All other users should use New256 instead.

Types

type Shake

type Shake struct {
	// contains filtered or unexported fields
}
Example
out := make([]byte, 32)
msg := []byte("The quick brown fox jumps over the lazy dog")

// Example 1: Simple Shake
c1 := NewShake256()
_, _ = c1.Write(msg)
_, _ = c1.Read(out)
fmt.Println(hex.EncodeToString(out))
Output:

2f671343d9b2e1604dc9dcf0753e5fe15c7c64a0d283cbbf722d411a0e36f6ca

func NewShake128

func NewShake128() Shake

NewShake128 creates a new SHAKE128 variable-output-length Shake. Its generic security strength is 128 bits against all attacks if at least 32 bytes of its output are used.

func NewShake256

func NewShake256() Shake

NewShake256 creates a new SHAKE256 variable-output-length Shake. Its generic security strength is 256 bits against all attacks if at least 64 bytes of its output are used.

func (*Shake) BlockSize

func (d *Shake) BlockSize() int

BlockSize returns the rate of sponge underlying this hash function.

func (*Shake) Clone

func (d *Shake) Clone() (ret Shake)

func (*Shake) Init128

func (d *Shake) Init128()

func (*Shake) Read

func (d *Shake) Read(out []byte) (n int, err error)

Read squeezes an arbitrary number of bytes from the sponge.

func (*Shake) Reset

func (d *Shake) Reset()

Reset clears the internal state by zeroing the sponge state and the byte buffer, and setting Sponge.state to absorbing.

func (*Shake) Size

func (d *Shake) Size() int

Size returns the output size of the hash function in bytes.

func (*Shake) Sum

func (d *Shake) Sum(in []byte) []byte

Sum applies padding to the hash state and then squeezes out the desired number of output bytes.

func (*Shake) Write

func (d *Shake) Write(p []byte) (int, error)

Write absorbs more data into the hash's state. It produces an error if more data is written to the ShakeHash after writing.

Jump to

Keyboard shortcuts

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