本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

99 lines
2.7KB

  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. // ReuseSignatures tells us to reuse existing Signatures
  47. // on serialized output.
  48. ReuseSignaturesOnSerialize bool
  49. }
  50. func (c *Config) Random() io.Reader {
  51. if c == nil || c.Rand == nil {
  52. return rand.Reader
  53. }
  54. return c.Rand
  55. }
  56. func (c *Config) Hash() crypto.Hash {
  57. if c == nil || uint(c.DefaultHash) == 0 {
  58. return crypto.SHA256
  59. }
  60. return c.DefaultHash
  61. }
  62. func (c *Config) Cipher() CipherFunction {
  63. if c == nil || uint8(c.DefaultCipher) == 0 {
  64. return CipherAES128
  65. }
  66. return c.DefaultCipher
  67. }
  68. func (c *Config) Now() time.Time {
  69. if c == nil || c.Time == nil {
  70. return time.Now()
  71. }
  72. return c.Time()
  73. }
  74. func (c *Config) Compression() CompressionAlgo {
  75. if c == nil {
  76. return CompressionNone
  77. }
  78. return c.DefaultCompressionAlgo
  79. }
  80. func (c *Config) PasswordHashIterations() int {
  81. if c == nil || c.S2KCount == 0 {
  82. return 0
  83. }
  84. return c.S2KCount
  85. }
  86. func (c *Config) ReuseSignatures() bool {
  87. return c != nil && c.ReuseSignaturesOnSerialize
  88. }
上海开阖软件有限公司 沪ICP备12045867号-1