Commit 894222f9 authored by Andrew Gerrand's avatar Andrew Gerrand

doc/talks/io2010: handle the errors

R=golang-dev, dsymonds, dsymonds, r
CC=golang-dev
https://golang.org/cl/4771041
parent 42effdf0
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// This code differs from the slides in that it handles errors.
package main package main
import ( import (
...@@ -9,32 +11,75 @@ import ( ...@@ -9,32 +11,75 @@ import (
"crypto/cipher" "crypto/cipher"
"compress/gzip" "compress/gzip"
"io" "io"
"log"
"os" "os"
) )
func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) { func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) os.Error {
r, _ := os.Open(srcfile) r, err := os.Open(srcfile)
if err != nil {
return err
}
var w io.Writer var w io.Writer
w, _ = os.Create(dstfile) w, err = os.Create(dstfile)
c, _ := aes.NewCipher(key) if err != nil {
return err
}
c, err := aes.NewCipher(key)
if err != nil {
return err
}
w = cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w} w = cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}
w2, _ := gzip.NewWriter(w) w2, err := gzip.NewWriter(w)
io.Copy(w2, r) if err != nil {
w2.Close() return err
}
defer w2.Close()
_, err = io.Copy(w2, r)
return err
} }
func DecryptAndGunzip(dstfile, srcfile string, key, iv []byte) { func DecryptAndGunzip(dstfile, srcfile string, key, iv []byte) os.Error {
f, _ := os.Open(srcfile) f, err := os.Open(srcfile)
if err != nil {
return err
}
defer f.Close() defer f.Close()
c, _ := aes.NewCipher(key) c, err := aes.NewCipher(key)
if err != nil {
return err
}
r := cipher.StreamReader{S: cipher.NewOFB(c, iv), R: f} r := cipher.StreamReader{S: cipher.NewOFB(c, iv), R: f}
r2, _ := gzip.NewReader(r) r2, err := gzip.NewReader(r)
w, _ := os.Create(dstfile) if err != nil {
return err
}
w, err := os.Create(dstfile)
if err != nil {
return err
}
defer w.Close() defer w.Close()
io.Copy(w, r2) _, err = io.Copy(w, r2)
return err
} }
func main() { func main() {
EncryptAndGzip("/tmp/passwd.gz", "/etc/passwd", make([]byte, 16), make([]byte, 16)) err := EncryptAndGzip(
DecryptAndGunzip("/dev/stdout", "/tmp/passwd.gz", make([]byte, 16), make([]byte, 16)) "/tmp/passwd.gz",
"/etc/passwd",
make([]byte, 16),
make([]byte, 16),
)
if err != nil {
log.Fatal(err)
}
err = DecryptAndGunzip(
"/dev/stdout",
"/tmp/passwd.gz",
make([]byte, 16),
make([]byte, 16),
)
if err != nil {
log.Fatal(err)
}
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// This code differs from the slides in that it handles errors.
package main package main
import ( import (
...@@ -9,20 +11,42 @@ import ( ...@@ -9,20 +11,42 @@ import (
"crypto/cipher" "crypto/cipher"
"compress/gzip" "compress/gzip"
"io" "io"
"log"
"os" "os"
) )
func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) { func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) os.Error {
r, _ := os.Open(srcfile) r, err := os.Open(srcfile)
if err != nil {
return err
}
var w io.WriteCloser var w io.WriteCloser
w, _ = os.Create(dstfile) w, err = os.Create(dstfile)
if err != nil {
return err
}
defer w.Close() defer w.Close()
w, _ = gzip.NewWriter(w) w, err = gzip.NewWriter(w)
if err != nil {
return err
}
defer w.Close() defer w.Close()
c, _ := aes.NewCipher(key) c, err := aes.NewCipher(key)
io.Copy(cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}, r) if err != nil {
return err
}
_, err = io.Copy(cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}, r)
return err
} }
func main() { 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)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment