github.com/turingchain2020/turingchain@v1.1.21/common/crypto/sha3/shake.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright 2014 The Go Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 9 package sha3 10 11 // This file defines the ShakeHash interface, and provides 12 // functions for creating SHAKE instances, as well as utility 13 // functions for hashing bytes to arbitrary-length output. 14 15 import ( 16 "io" 17 ) 18 19 // ShakeHash defines the interface to hash functions that 20 // support arbitrary-length output. 21 type ShakeHash interface { 22 // Write absorbs more data into the hash's state. It panics if input is 23 // written to it after output has been read from it. 24 io.Writer 25 26 // Read reads more output from the hash; reading affects the hash's 27 // state. (ShakeHash.Read is thus very different from Hash.Sum) 28 // It never returns an error. 29 io.Reader 30 31 // Clone returns a copy of the ShakeHash in its current state. 32 Clone() ShakeHash 33 34 // Reset resets the ShakeHash to its initial state. 35 Reset() 36 } 37 38 func (d *state) Clone() ShakeHash { 39 return d.clone() 40 } 41 42 // NewShake128 creates a new SHAKE128 variable-output-length ShakeHash. 43 // Its generic security strength is 128 bits against all attacks if at 44 // least 32 bytes of its output are used. 45 func NewShake128() ShakeHash { return &state{rate: 168, dsbyte: 0x1f} } 46 47 // NewShake256 creates a new SHAKE128 variable-output-length ShakeHash. 48 // Its generic security strength is 256 bits against all attacks if 49 // at least 64 bytes of its output are used. 50 func NewShake256() ShakeHash { return &state{rate: 136, dsbyte: 0x1f} } 51 52 // ShakeSum128 writes an arbitrary-length digest of data into hash. 53 func ShakeSum128(hash, data []byte) { 54 h := NewShake128() 55 h.Write(data) 56 h.Read(hash) 57 } 58 59 // ShakeSum256 writes an arbitrary-length digest of data into hash. 60 func ShakeSum256(hash, data []byte) { 61 h := NewShake256() 62 h.Write(data) 63 h.Read(hash) 64 }