// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sha3

import (
	
	
	
)

// ShakeHash defines the interface to hash functions that support
// arbitrary-length output. When used as a plain [hash.Hash], it
// produces minimum-length outputs that provide full-strength generic
// security.
type ShakeHash interface {
	hash.Hash

	// Read reads more output from the hash; reading affects the hash's
	// state. (ShakeHash.Read is thus very different from Hash.Sum.)
	// It never returns an error, but subsequent calls to Write or Sum
	// will panic.
	io.Reader

	// Clone returns a copy of the ShakeHash in its current state.
	Clone() ShakeHash
}

// NewShake128 creates a new SHAKE128 variable-output-length ShakeHash.
// Its generic security strength is 128 bits against all attacks if at
// least 32 bytes of its output are used.
func () ShakeHash {
	return &shakeWrapper{sha3.NewSHAKE128(), 32, false, sha3.NewSHAKE128}
}

// NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
// Its generic security strength is 256 bits against all attacks if
// at least 64 bytes of its output are used.
func () ShakeHash {
	return &shakeWrapper{sha3.NewSHAKE256(), 64, false, sha3.NewSHAKE256}
}

// NewCShake128 creates a new instance of cSHAKE128 variable-output-length ShakeHash,
// a customizable variant of SHAKE128.
// N is used to define functions based on cSHAKE, it can be empty when plain cSHAKE is
// desired. S is a customization byte string used for domain separation - two cSHAKE
// computations on same input with different S yield unrelated outputs.
// When N and S are both empty, this is equivalent to NewShake128.
func (,  []byte) ShakeHash {
	return &shakeWrapper{sha3.NewCSHAKE128(, ), 32, false, func() *sha3.SHAKE {
		return sha3.NewCSHAKE128(, )
	}}
}

// NewCShake256 creates a new instance of cSHAKE256 variable-output-length ShakeHash,
// a customizable variant of SHAKE256.
// N is used to define functions based on cSHAKE, it can be empty when plain cSHAKE is
// desired. S is a customization byte string used for domain separation - two cSHAKE
// computations on same input with different S yield unrelated outputs.
// When N and S are both empty, this is equivalent to NewShake256.
func (,  []byte) ShakeHash {
	return &shakeWrapper{sha3.NewCSHAKE256(, ), 64, false, func() *sha3.SHAKE {
		return sha3.NewCSHAKE256(, )
	}}
}

// ShakeSum128 writes an arbitrary-length digest of data into hash.
func (,  []byte) {
	 := NewShake128()
	.Write()
	.Read()
}

// ShakeSum256 writes an arbitrary-length digest of data into hash.
func (,  []byte) {
	 := NewShake256()
	.Write()
	.Read()
}

// shakeWrapper adds the Size, Sum, and Clone methods to a sha3.SHAKE
// to implement the ShakeHash interface.
type shakeWrapper struct {
	*sha3.SHAKE
	outputLen int
	squeezing bool
	newSHAKE  func() *sha3.SHAKE
}

func ( *shakeWrapper) ( []byte) ( int,  error) {
	.squeezing = true
	return .SHAKE.Read()
}

func ( *shakeWrapper) () ShakeHash {
	 := .newSHAKE()
	,  := .MarshalBinary()
	if  != nil {
		panic() // unreachable
	}
	if  := .UnmarshalBinary();  != nil {
		panic() // unreachable
	}
	return &shakeWrapper{, .outputLen, .squeezing, .newSHAKE}
}

func ( *shakeWrapper) () int { return .outputLen }

func ( *shakeWrapper) ( []byte) []byte {
	if .squeezing {
		panic("sha3: Sum after Read")
	}
	 := make([]byte, .outputLen)
	// Clone the state so that we don't affect future Write calls.
	 := .Clone()
	.Read()
	return append(, ...)
}