github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2010/io/encrypt.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build OMIT 6 7 // This code differs from the slides in that it handles errors. 8 9 package main 10 11 import ( 12 "compress/gzip" 13 "crypto/aes" 14 "crypto/cipher" 15 "io" 16 "log" 17 "os" 18 ) 19 20 func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) error { 21 r, err := os.Open(srcfile) 22 if err != nil { 23 return err 24 } 25 var w io.WriteCloser 26 w, err = os.Create(dstfile) 27 if err != nil { 28 return err 29 } 30 defer w.Close() 31 w, err = gzip.NewWriter(w) 32 if err != nil { 33 return err 34 } 35 defer w.Close() 36 c, err := aes.NewCipher(key) 37 if err != nil { 38 return err 39 } 40 _, err = io.Copy(cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}, r) 41 return err 42 } 43 44 func main() { 45 err := EncryptAndGzip( 46 "/tmp/passwd.gz", 47 "/etc/passwd", 48 make([]byte, 16), 49 make([]byte, 16), 50 ) 51 if err != nil { 52 log.Fatal(err) 53 } 54 }