config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // DefaultCompressionAlgo is the compression algorithm to be
  27. // applied to the plaintext before encryption. If zero, no
  28. // compression is done.
  29. DefaultCompressionAlgo CompressionAlgo
  30. // CompressionConfig configures the compression settings.
  31. CompressionConfig *CompressionConfig
  32. }
  33. func (c *Config) Random() io.Reader {
  34. if c == nil || c.Rand == nil {
  35. return rand.Reader
  36. }
  37. return c.Rand
  38. }
  39. func (c *Config) Hash() crypto.Hash {
  40. if c == nil || uint(c.DefaultHash) == 0 {
  41. return crypto.SHA256
  42. }
  43. return c.DefaultHash
  44. }
  45. func (c *Config) Cipher() CipherFunction {
  46. if c == nil || uint8(c.DefaultCipher) == 0 {
  47. return CipherAES128
  48. }
  49. return c.DefaultCipher
  50. }
  51. func (c *Config) Now() time.Time {
  52. if c == nil || c.Time == nil {
  53. return time.Now()
  54. }
  55. return c.Time()
  56. }
  57. func (c *Config) Compression() CompressionAlgo {
  58. if c == nil {
  59. return CompressionNone
  60. }
  61. return c.DefaultCompressionAlgo
  62. }