config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2012 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 packet
  5. import (
  6. "crypto"
  7. "crypto/rand"
  8. "io"
  9. "time"
  10. )
  11. // Config collects a number of parameters along with sensible defaults.
  12. // A nil *Config is valid and results in all default values.
  13. type Config struct {
  14. // Rand provides the source of entropy.
  15. // If nil, the crypto/rand Reader is used.
  16. Rand io.Reader
  17. // DefaultHash is the default hash function to be used.
  18. // If zero, SHA-256 is used.
  19. DefaultHash crypto.Hash
  20. // DefaultCipher is the cipher to be used.
  21. // If zero, AES-128 is used.
  22. DefaultCipher CipherFunction
  23. // Time returns the current time as the number of seconds since the
  24. // epoch. If Time is nil, time.Now is used.
  25. Time func() time.Time
  26. }
  27. func (c *Config) Random() io.Reader {
  28. if c == nil || c.Rand == nil {
  29. return rand.Reader
  30. }
  31. return c.Rand
  32. }
  33. func (c *Config) Hash() crypto.Hash {
  34. if c == nil || uint(c.DefaultHash) == 0 {
  35. return crypto.SHA256
  36. }
  37. return c.DefaultHash
  38. }
  39. func (c *Config) Cipher() CipherFunction {
  40. if c == nil || uint8(c.DefaultCipher) == 0 {
  41. return CipherAES128
  42. }
  43. return c.DefaultCipher
  44. }
  45. func (c *Config) Now() time.Time {
  46. if c == nil || c.Time == nil {
  47. return time.Now()
  48. }
  49. return c.Time()
  50. }