Documentation
¶
Overview ¶
Package sealer provides transparent compression and encryption of data.
Example ¶
package main
import (
"bytes"
cryptoRand "crypto/rand"
"fmt"
"io"
"github.com/andreyvit/sealer"
)
func main() {
const prefixLen = 32
prefix := make([]byte, prefixLen)
copy(prefix, "MY_DATA_FORMAT_HEADER_GOES_HERE!")
key := &sealer.Key{}
copy(key.ID[:], "YA_CAN_PUT_WHATEVER_YA_WANT_HERE")
_, err := io.ReadFull(cryptoRand.Reader, key.Key[:])
if err != nil {
panic(err)
}
// generate non-random compressible data to demonstrate compression
data := make([]byte, 200)
for i := range data {
data[i] = byte(i)
}
var sealed bytes.Buffer
var expectedData bytes.Buffer
{ // Sealing
w, err := sealer.Seal(&sealed, key, prefix, sealer.SealOptions{})
if err != nil {
panic(err)
}
var totalUncompressedSize int
for range 100 {
_, err := w.Write(data)
if err != nil {
panic(err)
}
totalUncompressedSize += len(data)
expectedData.Write(data)
}
// Very important to close the writer to write the final chunk.
err = w.Close()
if err != nil {
panic(err)
}
fmt.Printf("%d bytes input => %d bytes sealed\n", totalUncompressedSize, sealed.Len())
}
{ // Opening
fmt.Printf("Preparing to open:\n")
actualPrefix := make([]byte, prefixLen)
_, err := io.ReadFull(&sealed, actualPrefix)
if err != nil {
panic(err)
}
fmt.Printf("prefix = %s\n", actualPrefix)
o, err := sealer.Prepare(&sealed, actualPrefix)
if err != nil {
panic(err)
}
fmt.Printf("key ID = %s\n", o.KeyID[:])
r, err := o.Open(key)
if err != nil {
panic(err)
}
var opened bytes.Buffer
_, err = io.Copy(&opened, r)
if err != nil {
panic(err)
}
if !bytes.Equal(opened.Bytes(), expectedData.Bytes()) {
fmt.Println("data mismatch!")
}
}
}
Output: 20000 bytes input => 389 bytes sealed Preparing to open: prefix = MY_DATA_FORMAT_HEADER_GOES_HERE! key ID = YA_CAN_PUT_WHATEVER_YA_WANT_HERE
Index ¶
Examples ¶
Constants ¶
View Source
const ( // KeySize is the length of Cacha20-Poly1305 key (32 bytes). KeySize = chacha20poly1305.KeySize // IDSize is the length of a user-defined key ID used by this package // (32 bytes). IDSize = 32 )
View Source
const DefaultChunkSize int = 32 * 1024
DefaultChunkSize is the default value of SealOptions.ChunkSize used by the sealer.
View Source
const MaxChunkSize int = 1024 * 1024
MaxChunkSize is the maximum value of SealOptions.ChunkSize that can be used by the sealer, and the maximum size that opener will accept, in order to avoid DoS attacks when reading untrusted files.
Variables ¶
View Source
var ( ErrInvalidKeyString = errors.New("invalid key string") ErrKeyNameTooLong = errors.New("key name too long") ErrInvalidKeyName = errors.New("invalid key name") )
View Source
var ( ErrChunkSizeTooLarge = errors.New("chunk size too large") ErrUnsupportedVersion = errors.New("unsupported or corrupted sealed file") )
Functions ¶
func PrintableID ¶ added in v0.2.0
Types ¶
type Key ¶
Key is a user-provided encrypted key. It is used once per sealing operation, to encapsulate (i.e. encrypt) an ephemeral file key. You can generate the key bytes by reading from crypto/rand.Reader. NIST recommends that you limit using a single key to no more than 2^32 Seal operations.
type Openable ¶
Click to show internal directories.
Click to hide internal directories.