hashes.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 provides functions for creating instances of the SHA-3
  6. // and SHAKE hash functions, as well as utility functions for hashing
  7. // bytes.
  8. import (
  9. "hash"
  10. )
  11. // NewKeccak256 creates a new Keccak-256 hash.
  12. func NewKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
  13. // New224 creates a new SHA3-224 hash.
  14. // Its generic security strength is 224 bits against preimage attacks,
  15. // and 112 bits against collision attacks.
  16. func New224() hash.Hash { return &state{rate: 144, outputLen: 28, dsbyte: 0x06} }
  17. // New256 creates a new SHA3-256 hash.
  18. // Its generic security strength is 256 bits against preimage attacks,
  19. // and 128 bits against collision attacks.
  20. func New256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x06} }
  21. // New384 creates a new SHA3-384 hash.
  22. // Its generic security strength is 384 bits against preimage attacks,
  23. // and 192 bits against collision attacks.
  24. func New384() hash.Hash { return &state{rate: 104, outputLen: 48, dsbyte: 0x06} }
  25. // New512 creates a new SHA3-512 hash.
  26. // Its generic security strength is 512 bits against preimage attacks,
  27. // and 256 bits against collision attacks.
  28. func New512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x06} }
  29. // Sum224 returns the SHA3-224 digest of the data.
  30. func Sum224(data []byte) (digest [28]byte) {
  31. h := New224()
  32. h.Write(data)
  33. h.Sum(digest[:0])
  34. return
  35. }
  36. // Sum256 returns the SHA3-256 digest of the data.
  37. func Sum256(data []byte) (digest [32]byte) {
  38. h := New256()
  39. h.Write(data)
  40. h.Sum(digest[:0])
  41. return
  42. }
  43. // Sum384 returns the SHA3-384 digest of the data.
  44. func Sum384(data []byte) (digest [48]byte) {
  45. h := New384()
  46. h.Write(data)
  47. h.Sum(digest[:0])
  48. return
  49. }
  50. // Sum512 returns the SHA3-512 digest of the data.
  51. func Sum512(data []byte) (digest [64]byte) {
  52. h := New512()
  53. h.Write(data)
  54. h.Sum(digest[:0])
  55. return
  56. }