gfcp_entropy.go 880 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>.
  2. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  3. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  4. // Copyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. package gfcp
  11. import (
  12. "crypto/rand"
  13. "io"
  14. )
  15. // Entropy defines a entropy source
  16. type Entropy interface {
  17. Init()
  18. Fill(
  19. nonce []byte,
  20. )
  21. }
  22. // Nonce ...
  23. type Nonce struct {
  24. seed []byte
  25. }
  26. // Init ...
  27. func (
  28. n *Nonce,
  29. ) Init() {
  30. }
  31. // Fill ...
  32. func (
  33. n *Nonce,
  34. ) Fill(
  35. nonce []byte,
  36. ) {
  37. var err error
  38. if n.seed[0] == 0 {
  39. _, err = io.ReadFull(
  40. rand.Reader,
  41. n.seed,
  42. )
  43. if err != nil {
  44. panic(
  45. "io.ReadFull failure",
  46. )
  47. }
  48. }
  49. copy(
  50. nonce,
  51. n.seed,
  52. )
  53. }