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.

112 lines
2.7 KiB

  1. // Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
  2. // Use of this source code is governed by a license that can be
  3. // found in the LICENSE file.
  4. // Package skein256 implements the Skein256 hash function
  5. // based on the Threefish256 tweakable block cipher.
  6. package skein256
  7. import (
  8. "hash"
  9. "github.com/aead/skein"
  10. )
  11. // Sum512 computes the 512 bit Skein256 checksum (or MAC if key is set) of msg
  12. // and writes it to out. The key is optional and can be nil.
  13. func Sum512(out *[64]byte, msg, key []byte) {
  14. var out256 [32]byte
  15. s := new(hashFunc)
  16. s.initialize(64, &skein.Config{Key: key})
  17. s.Write(msg)
  18. s.finalizeHash()
  19. s.output(&out256, 0)
  20. copy(out[:], out256[:])
  21. s.output(&out256, 1)
  22. copy(out[32:], out256[:])
  23. }
  24. // Sum384 computes the 384 bit Skein256 checksum (or MAC if key is set) of msg
  25. // and writes it to out. The key is optional and can be nil.
  26. func Sum384(out *[48]byte, msg, key []byte) {
  27. var out256 [32]byte
  28. s := new(hashFunc)
  29. s.initialize(48, &skein.Config{Key: key})
  30. s.Write(msg)
  31. s.finalizeHash()
  32. s.output(&out256, 0)
  33. copy(out[:], out256[:])
  34. s.output(&out256, 1)
  35. copy(out[32:], out256[:16])
  36. }
  37. // Sum256 computes the 256 bit Skein256 checksum (or MAC if key is set) of msg
  38. // and writes it to out. The key is optional and can be nil.
  39. func Sum256(out *[32]byte, msg, key []byte) {
  40. s := new(hashFunc)
  41. s.initialize(32, &skein.Config{Key: key})
  42. s.Write(msg)
  43. s.finalizeHash()
  44. s.output(out, 0)
  45. }
  46. // Sum160 computes the 160 bit Skein256 checksum (or MAC if key is set) of msg
  47. // and writes it to out. The key is optional and can be nil.
  48. func Sum160(out *[20]byte, msg, key []byte) {
  49. var out256 [32]byte
  50. s := new(hashFunc)
  51. s.initialize(20, &skein.Config{Key: key})
  52. s.Write(msg)
  53. s.finalizeHash()
  54. s.output(&out256, 0)
  55. copy(out[:], out256[:20])
  56. }
  57. // Sum returns the Skein256 checksum with the given hash size of msg using the (optional)
  58. // conf for configuration. The hashsize must be > 0.
  59. func Sum(msg []byte, hashsize int, conf *skein.Config) []byte {
  60. s := New(hashsize, conf)
  61. s.Write(msg)
  62. return s.Sum(nil)
  63. }
  64. // New512 returns a hash.Hash computing the Skein256 512 bit checksum.
  65. // The key is optional and turns the hash into a MAC.
  66. func New512(key []byte) hash.Hash {
  67. s := new(hashFunc)
  68. s.initialize(64, &skein.Config{Key: key})
  69. return s
  70. }
  71. // New256 returns a hash.Hash computing the Skein256 256 bit checksum.
  72. // The key is optional and turns the hash into a MAC.
  73. func New256(key []byte) hash.Hash {
  74. s := new(hashFunc)
  75. s.initialize(32, &skein.Config{Key: key})
  76. return s
  77. }
  78. // New returns a hash.Hash computing the Skein256 checksum with the given hash size.
  79. // The conf is optional and configurates the hash.Hash
  80. func New(hashsize int, conf *skein.Config) hash.Hash {
  81. s := new(hashFunc)
  82. s.initialize(hashsize, conf)
  83. return s
  84. }