gfcp_entropy.go 856 B

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