config.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // S2KCount is only used for symmetric encryption. It
  33. // determines the strength of the passphrase stretching when
  34. // the said passphrase is hashed to produce a key. S2KCount
  35. // should be between 1024 and 65011712, inclusive. If Config
  36. // is nil or S2KCount is 0, the value 65536 used. Not all
  37. // values in the above range can be represented. S2KCount will
  38. // be rounded up to the next representable value if it cannot
  39. // be encoded exactly. When set, it is strongly encrouraged to
  40. // use a value that is at least 65536. See RFC 4880 Section
  41. // 3.7.1.3.
  42. S2KCount int
  43. // RSABits is the number of bits in new RSA keys made with NewEntity.
  44. // If zero, then 2048 bit keys are created.
  45. RSABits int
  46. }
  47. func (c *Config) Random() io.Reader {
  48. if c == nil || c.Rand == nil {
  49. return rand.Reader
  50. }
  51. return c.Rand
  52. }
  53. func (c *Config) Hash() crypto.Hash {
  54. if c == nil || uint(c.DefaultHash) == 0 {
  55. return crypto.SHA256
  56. }
  57. return c.DefaultHash
  58. }
  59. func (c *Config) Cipher() CipherFunction {
  60. if c == nil || uint8(c.DefaultCipher) == 0 {
  61. return CipherAES128
  62. }
  63. return c.DefaultCipher
  64. }
  65. func (c *Config) Now() time.Time {
  66. if c == nil || c.Time == nil {
  67. return time.Now()
  68. }
  69. return c.Time()
  70. }
  71. func (c *Config) Compression() CompressionAlgo {
  72. if c == nil {
  73. return CompressionNone
  74. }
  75. return c.DefaultCompressionAlgo
  76. }
  77. func (c *Config) PasswordHashIterations() int {
  78. if c == nil || c.S2KCount == 0 {
  79. return 0
  80. }
  81. return c.S2KCount
  82. }