~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to doc/talks/io2010/encrypt.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
// This code differs from the slides in that it handles errors.
6
 
 
7
 
package main
8
 
 
9
 
import (
10
 
        "crypto/aes"
11
 
        "crypto/cipher"
12
 
        "compress/gzip"
13
 
        "io"
14
 
        "log"
15
 
        "os"
16
 
)
17
 
 
18
 
func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) error {
19
 
        r, err := os.Open(srcfile)
20
 
        if err != nil {
21
 
                return err
22
 
        }
23
 
        var w io.WriteCloser
24
 
        w, err = os.Create(dstfile)
25
 
        if err != nil {
26
 
                return err
27
 
        }
28
 
        defer w.Close()
29
 
        w, err = gzip.NewWriter(w)
30
 
        if err != nil {
31
 
                return err
32
 
        }
33
 
        defer w.Close()
34
 
        c, err := aes.NewCipher(key)
35
 
        if err != nil {
36
 
                return err
37
 
        }
38
 
        _, err = io.Copy(cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}, r)
39
 
        return err
40
 
}
41
 
 
42
 
func main() {
43
 
        err := EncryptAndGzip(
44
 
                "/tmp/passwd.gz",
45
 
                "/etc/passwd",
46
 
                make([]byte, 16),
47
 
                make([]byte, 16),
48
 
        )
49
 
        if err != nil {
50
 
                log.Fatal(err)
51
 
        }
52
 
}