summaryrefslogtreecommitdiff
path: root/doc/talks/io2010/encrypt.go
diff options
context:
space:
mode:
Diffstat (limited to 'doc/talks/io2010/encrypt.go')
-rw-r--r--doc/talks/io2010/encrypt.go40
1 files changed, 32 insertions, 8 deletions
diff --git a/doc/talks/io2010/encrypt.go b/doc/talks/io2010/encrypt.go
index e5ab3fc59..c6508bba1 100644
--- a/doc/talks/io2010/encrypt.go
+++ b/doc/talks/io2010/encrypt.go
@@ -2,27 +2,51 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// This code differs from the slides in that it handles errors.
+
package main
import (
"crypto/aes"
- "crypto/block"
+ "crypto/cipher"
"compress/gzip"
"io"
+ "log"
"os"
)
-func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) {
- r, _ := os.Open(srcfile, os.O_RDONLY, 0)
+func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) os.Error {
+ r, err := os.Open(srcfile)
+ if err != nil {
+ return err
+ }
var w io.WriteCloser
- w, _ = os.Open(dstfile, os.O_WRONLY|os.O_CREATE, 0666)
+ w, err = os.Create(dstfile)
+ if err != nil {
+ return err
+ }
defer w.Close()
- w, _ = gzip.NewDeflater(w)
+ w, err = gzip.NewWriter(w)
+ if err != nil {
+ return err
+ }
defer w.Close()
- c, _ := aes.NewCipher(key)
- io.Copy(block.NewCBCEncrypter(c, iv, w), r)
+ 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
}
func main() {
- EncryptAndGzip("/tmp/passwd.gz", "/etc/passwd", make([]byte, 16), make([]byte, 16))
+ err := EncryptAndGzip(
+ "/tmp/passwd.gz",
+ "/etc/passwd",
+ make([]byte, 16),
+ make([]byte, 16),
+ )
+ if err != nil {
+ log.Fatal(err)
+ }
}