hashes.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // New224 creates a new SHA3-224 hash.
  12. // Its generic security strength is 224 bits against preimage attacks,
  13. // and 112 bits against collision attacks.
  14. func New224() hash.Hash {
  15. if h := new224Asm(); h != nil {
  16. return h
  17. }
  18. return &state{rate: 144, outputLen: 28, dsbyte: 0x06}
  19. }
  20. // New256 creates a new SHA3-256 hash.
  21. // Its generic security strength is 256 bits against preimage attacks,
  22. // and 128 bits against collision attacks.
  23. func New256() hash.Hash {
  24. if h := new256Asm(); h != nil {
  25. return h
  26. }
  27. return &state{rate: 136, outputLen: 32, dsbyte: 0x06}
  28. }
  29. // New384 creates a new SHA3-384 hash.
  30. // Its generic security strength is 384 bits against preimage attacks,
  31. // and 192 bits against collision attacks.
  32. func New384() hash.Hash {
  33. if h := new384Asm(); h != nil {
  34. return h
  35. }
  36. return &state{rate: 104, outputLen: 48, dsbyte: 0x06}
  37. }
  38. // New512 creates a new SHA3-512 hash.
  39. // Its generic security strength is 512 bits against preimage attacks,
  40. // and 256 bits against collision attacks.
  41. func New512() hash.Hash {
  42. if h := new512Asm(); h != nil {
  43. return h
  44. }
  45. return &state{rate: 72, outputLen: 64, dsbyte: 0x06}
  46. }
  47. // NewLegacyKeccak256 creates a new Keccak-256 hash.
  48. //
  49. // Only use this function if you require compatibility with an existing cryptosystem
  50. // that uses non-standard padding. All other users should use New256 instead.
  51. func NewLegacyKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
  52. // NewLegacyKeccak512 creates a new Keccak-512 hash.
  53. //
  54. // Only use this function if you require compatibility with an existing cryptosystem
  55. // that uses non-standard padding. All other users should use New512 instead.
  56. func NewLegacyKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} }
  57. // Sum224 returns the SHA3-224 digest of the data.
  58. func Sum224(data []byte) (digest [28]byte) {
  59. h := New224()
  60. h.Write(data)
  61. h.Sum(digest[:0])
  62. return
  63. }
  64. // Sum256 returns the SHA3-256 digest of the data.
  65. func Sum256(data []byte) (digest [32]byte) {
  66. h := New256()
  67. h.Write(data)
  68. h.Sum(digest[:0])
  69. return
  70. }
  71. // Sum384 returns the SHA3-384 digest of the data.
  72. func Sum384(data []byte) (digest [48]byte) {
  73. h := New384()
  74. h.Write(data)
  75. h.Sum(digest[:0])
  76. return
  77. }
  78. // Sum512 returns the SHA3-512 digest of the data.
  79. func Sum512(data []byte) (digest [64]byte) {
  80. h := New512()
  81. h.Write(data)
  82. h.Sum(digest[:0])
  83. return
  84. }