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.

113 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 skein1024 implements the Skein1024 hash function
  5. // based on the Threefish1024 tweakable block cipher.
  6. package skein1024
  7. import (
  8. "hash"
  9. "github.com/aead/skein"
  10. )
  11. // Sum512 computes the 512 bit Skein1024 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 out1024 [128]byte
  15. s := new(hashFunc)
  16. s.initialize(64, &skein.Config{Key: key})
  17. s.Write(msg)
  18. s.finalizeHash()
  19. s.output(&out1024, 0)
  20. copy(out[:], out1024[:64])
  21. }
  22. // Sum384 computes the 384 bit Skein1024 checksum (or MAC if key is set) of msg
  23. // and writes it to out. The key is optional and can be nil.
  24. func Sum384(out *[48]byte, msg, key []byte) {
  25. var out1024 [128]byte
  26. s := new(hashFunc)
  27. s.initialize(48, &skein.Config{Key: key})
  28. s.Write(msg)
  29. s.finalizeHash()
  30. s.output(&out1024, 0)
  31. copy(out[:], out1024[:48])
  32. }
  33. // Sum256 computes the 256 bit Skein1024 checksum (or MAC if key is set) of msg
  34. // and writes it to out. The key is optional and can be nil.
  35. func Sum256(out *[32]byte, msg, key []byte) {
  36. var out1024 [128]byte
  37. s := new(hashFunc)
  38. s.initialize(32, &skein.Config{Key: key})
  39. s.Write(msg)
  40. s.finalizeHash()
  41. s.output(&out1024, 0)
  42. copy(out[:], out1024[:32])
  43. }
  44. // Sum160 computes the 160 bit Skein1024 checksum (or MAC if key is set) of msg
  45. // and writes it to out. The key is optional and can be nil.
  46. func Sum160(out *[20]byte, msg, key []byte) {
  47. var out1024 [128]byte
  48. s := new(hashFunc)
  49. s.initialize(20, &skein.Config{Key: key})
  50. s.Write(msg)
  51. s.finalizeHash()
  52. s.output(&out1024, 0)
  53. copy(out[:], out1024[:20])
  54. }
  55. // Sum returns the Skein1024 checksum with the given hash size of msg using the (optional)
  56. // conf for configuration. The hashsize must be > 0.
  57. func Sum(msg []byte, hashsize int, conf *skein.Config) []byte {
  58. s := New(hashsize, conf)
  59. s.Write(msg)
  60. return s.Sum(nil)
  61. }
  62. // New512 returns a hash.Hash computing the Skein1024 512 bit checksum.
  63. // The key is optional and turns the hash into a MAC.
  64. func New512(key []byte) hash.Hash {
  65. s := new(hashFunc)
  66. s.initialize(64, &skein.Config{Key: key})
  67. return s
  68. }
  69. // New256 returns a hash.Hash computing the Skein1024 256 bit checksum.
  70. // The key is optional and turns the hash into a MAC.
  71. func New256(key []byte) hash.Hash {
  72. s := new(hashFunc)
  73. s.initialize(32, &skein.Config{Key: key})
  74. return s
  75. }
  76. // New returns a hash.Hash computing the Skein1024 checksum with the given hash size.
  77. // The conf is optional and configurates the hash.Hash
  78. func New(hashsize int, conf *skein.Config) hash.Hash {
  79. s := new(hashFunc)
  80. s.initialize(hashsize, conf)
  81. return s
  82. }