shake.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2014 The Go Authors. 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. package sha3
  5. // This file defines the ShakeHash interface, and provides
  6. // functions for creating SHAKE instances, as well as utility
  7. // functions for hashing bytes to arbitrary-length output.
  8. import (
  9. "io"
  10. )
  11. // ShakeHash defines the interface to hash functions that
  12. // support arbitrary-length output.
  13. type ShakeHash interface {
  14. // Write absorbs more data into the hash's state. It panics if input is
  15. // written to it after output has been read from it.
  16. io.Writer
  17. // Read reads more output from the hash; reading affects the hash's
  18. // state. (ShakeHash.Read is thus very different from Hash.Sum)
  19. // It never returns an error.
  20. io.Reader
  21. // Clone returns a copy of the ShakeHash in its current state.
  22. Clone() ShakeHash
  23. // Reset resets the ShakeHash to its initial state.
  24. Reset()
  25. }
  26. func (d *state) Clone() ShakeHash {
  27. return d.clone()
  28. }
  29. // NewShake128 creates a new SHAKE128 variable-output-length ShakeHash.
  30. // Its generic security strength is 128 bits against all attacks if at
  31. // least 32 bytes of its output are used.
  32. func NewShake128() ShakeHash { return &state{rate: 168, dsbyte: 0x1f} }
  33. // NewShake256 creates a new SHAKE128 variable-output-length ShakeHash.
  34. // Its generic security strength is 256 bits against all attacks if
  35. // at least 64 bytes of its output are used.
  36. func NewShake256() ShakeHash { return &state{rate: 136, dsbyte: 0x1f} }
  37. // ShakeSum128 writes an arbitrary-length digest of data into hash.
  38. func ShakeSum128(hash, data []byte) {
  39. h := NewShake128()
  40. h.Write(data)
  41. h.Read(hash)
  42. }
  43. // ShakeSum256 writes an arbitrary-length digest of data into hash.
  44. func ShakeSum256(hash, data []byte) {
  45. h := NewShake256()
  46. h.Write(data)
  47. h.Read(hash)
  48. }