2010-06-08 17:00:04 -06:00
|
|
|
// Copyright 2010 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.
|
|
|
|
|
2011-07-18 22:10:12 -06:00
|
|
|
// This code differs from the slides in that it handles errors.
|
|
|
|
|
2010-06-08 17:00:04 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
2011-07-12 17:40:49 -06:00
|
|
|
"crypto/cipher"
|
2010-06-08 17:00:04 -06:00
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2011-07-18 22:10:12 -06:00
|
|
|
"log"
|
2010-06-08 17:00:04 -06:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) error {
|
2011-07-18 22:10:12 -06:00
|
|
|
r, err := os.Open(srcfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2010-06-08 17:00:04 -06:00
|
|
|
var w io.WriteCloser
|
2011-07-18 22:10:12 -06:00
|
|
|
w, err = os.Create(dstfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2010-06-08 17:00:04 -06:00
|
|
|
defer w.Close()
|
2011-07-18 22:10:12 -06:00
|
|
|
w, err = gzip.NewWriter(w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2010-06-08 17:00:04 -06:00
|
|
|
defer w.Close()
|
2011-07-18 22:10:12 -06:00
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}, r)
|
|
|
|
return err
|
2010-06-08 17:00:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2011-07-18 22:10:12 -06:00
|
|
|
err := EncryptAndGzip(
|
|
|
|
"/tmp/passwd.gz",
|
|
|
|
"/etc/passwd",
|
|
|
|
make([]byte, 16),
|
|
|
|
make([]byte, 16),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2010-06-08 17:00:04 -06:00
|
|
|
}
|