hashes.go 2.1 KB

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