hashes.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "crypto"
  10. "hash"
  11. )
  12. func init() {
  13. crypto.RegisterHash(crypto.SHA3_224, New224)
  14. crypto.RegisterHash(crypto.SHA3_256, New256)
  15. crypto.RegisterHash(crypto.SHA3_384, New384)
  16. crypto.RegisterHash(crypto.SHA3_512, New512)
  17. }
  18. // New224 creates a new SHA3-224 hash.
  19. // Its generic security strength is 224 bits against preimage attacks,
  20. // and 112 bits against collision attacks.
  21. func New224() hash.Hash { return &state{rate: 144, outputLen: 28, dsbyte: 0x06} }
  22. // New256 creates a new SHA3-256 hash.
  23. // Its generic security strength is 256 bits against preimage attacks,
  24. // and 128 bits against collision attacks.
  25. func New256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x06} }
  26. // New384 creates a new SHA3-384 hash.
  27. // Its generic security strength is 384 bits against preimage attacks,
  28. // and 192 bits against collision attacks.
  29. func New384() hash.Hash { return &state{rate: 104, outputLen: 48, dsbyte: 0x06} }
  30. // New512 creates a new SHA3-512 hash.
  31. // Its generic security strength is 512 bits against preimage attacks,
  32. // and 256 bits against collision attacks.
  33. func New512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x06} }
  34. // Sum224 returns the SHA3-224 digest of the data.
  35. func Sum224(data []byte) (digest [28]byte) {
  36. h := New224()
  37. h.Write(data)
  38. h.Sum(digest[:0])
  39. return
  40. }
  41. // Sum256 returns the SHA3-256 digest of the data.
  42. func Sum256(data []byte) (digest [32]byte) {
  43. h := New256()
  44. h.Write(data)
  45. h.Sum(digest[:0])
  46. return
  47. }
  48. // Sum384 returns the SHA3-384 digest of the data.
  49. func Sum384(data []byte) (digest [48]byte) {
  50. h := New384()
  51. h.Write(data)
  52. h.Sum(digest[:0])
  53. return
  54. }
  55. // Sum512 returns the SHA3-512 digest of the data.
  56. func Sum512(data []byte) (digest [64]byte) {
  57. h := New512()
  58. h.Write(data)
  59. h.Sum(digest[:0])
  60. return
  61. }