This patch implements the facilities needed to compress data before encryption as allowed (and recommended) by RFC 4880. The new functionality is then used to add support for compressing data during symmetric encryption (openpgp.SymmetricallyEncrypt()). For now, compression defaults to off. Also, only the ZIP and ZLIB compression schemes are supported by this patch. Resulting output tested/verified using GPG. https://gist.github.com/marete/6189760 is a small program that can be used to test that the output of various compression/encryption settings can be read by GPG or other RFC 4880 programs. Upon review, I will follow this patch with 2 others: a) Add support for compression during public key encryption (openpgp.Encrypt()) b) Enable compression by default (subject to the restrictions of the "Compression Preferences" section in RFC 4880). R=golang-dev, agl CC=golang-dev https://golang.org/cl/12685044
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package packet
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/rand"
|
|
"io"
|
|
"time"
|
|
)
|
|
|
|
// Config collects a number of parameters along with sensible defaults.
|
|
// A nil *Config is valid and results in all default values.
|
|
type Config struct {
|
|
// Rand provides the source of entropy.
|
|
// If nil, the crypto/rand Reader is used.
|
|
Rand io.Reader
|
|
// DefaultHash is the default hash function to be used.
|
|
// If zero, SHA-256 is used.
|
|
DefaultHash crypto.Hash
|
|
// DefaultCipher is the cipher to be used.
|
|
// If zero, AES-128 is used.
|
|
DefaultCipher CipherFunction
|
|
// Time returns the current time as the number of seconds since the
|
|
// epoch. If Time is nil, time.Now is used.
|
|
Time func() time.Time
|
|
// DefaultCompressionAlgo is the compression algorithm to be
|
|
// applied to the plaintext before encryption. If zero, no
|
|
// compression is done.
|
|
DefaultCompressionAlgo CompressionAlgo
|
|
// CompressionConfig configures the compression settings.
|
|
CompressionConfig *CompressionConfig
|
|
}
|
|
|
|
func (c *Config) Random() io.Reader {
|
|
if c == nil || c.Rand == nil {
|
|
return rand.Reader
|
|
}
|
|
return c.Rand
|
|
}
|
|
|
|
func (c *Config) Hash() crypto.Hash {
|
|
if c == nil || uint(c.DefaultHash) == 0 {
|
|
return crypto.SHA256
|
|
}
|
|
return c.DefaultHash
|
|
}
|
|
|
|
func (c *Config) Cipher() CipherFunction {
|
|
if c == nil || uint8(c.DefaultCipher) == 0 {
|
|
return CipherAES128
|
|
}
|
|
return c.DefaultCipher
|
|
}
|
|
|
|
func (c *Config) Now() time.Time {
|
|
if c == nil || c.Time == nil {
|
|
return time.Now()
|
|
}
|
|
return c.Time()
|
|
}
|
|
|
|
func (c *Config) Compression() CompressionAlgo {
|
|
if c == nil {
|
|
return CompressionNone
|
|
}
|
|
return c.DefaultCompressionAlgo
|
|
}
|