本站源代码
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.

54 lines
1.1KB

  1. // Copyright 2017 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 argon2
  5. import (
  6. "encoding/binary"
  7. "hash"
  8. "golang.org/x/crypto/blake2b"
  9. )
  10. // blake2bHash computes an arbitrary long hash value of in
  11. // and writes the hash to out.
  12. func blake2bHash(out []byte, in []byte) {
  13. var b2 hash.Hash
  14. if n := len(out); n < blake2b.Size {
  15. b2, _ = blake2b.New(n, nil)
  16. } else {
  17. b2, _ = blake2b.New512(nil)
  18. }
  19. var buffer [blake2b.Size]byte
  20. binary.LittleEndian.PutUint32(buffer[:4], uint32(len(out)))
  21. b2.Write(buffer[:4])
  22. b2.Write(in)
  23. if len(out) <= blake2b.Size {
  24. b2.Sum(out[:0])
  25. return
  26. }
  27. outLen := len(out)
  28. b2.Sum(buffer[:0])
  29. b2.Reset()
  30. copy(out, buffer[:32])
  31. out = out[32:]
  32. for len(out) > blake2b.Size {
  33. b2.Write(buffer[:])
  34. b2.Sum(buffer[:0])
  35. copy(out, buffer[:32])
  36. out = out[32:]
  37. b2.Reset()
  38. }
  39. if outLen%blake2b.Size > 0 { // outLen > 64
  40. r := ((outLen + 31) / 32) - 2 // ⌈τ /32⌉-2
  41. b2, _ = blake2b.New(outLen-32*r, nil)
  42. }
  43. b2.Write(buffer[:])
  44. b2.Sum(out[:0])
  45. }
上海开阖软件有限公司 沪ICP备12045867号-1