You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
1.9 KiB

  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 {
  33. if h := newShake128Asm(); h != nil {
  34. return h
  35. }
  36. return &state{rate: 168, dsbyte: 0x1f}
  37. }
  38. // NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
  39. // Its generic security strength is 256 bits against all attacks if
  40. // at least 64 bytes of its output are used.
  41. func NewShake256() ShakeHash {
  42. if h := newShake256Asm(); h != nil {
  43. return h
  44. }
  45. return &state{rate: 136, dsbyte: 0x1f}
  46. }
  47. // ShakeSum128 writes an arbitrary-length digest of data into hash.
  48. func ShakeSum128(hash, data []byte) {
  49. h := NewShake128()
  50. h.Write(data)
  51. h.Read(hash)
  52. }
  53. // ShakeSum256 writes an arbitrary-length digest of data into hash.
  54. func ShakeSum256(hash, data []byte) {
  55. h := NewShake256()
  56. h.Write(data)
  57. h.Read(hash)
  58. }