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

« back to all changes in this revision

Viewing changes to doc/talks/io2010/decrypt.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.Writer
24
 
        w, err = os.Create(dstfile)
25
 
        if err != nil {
26
 
                return err
27
 
        }
28
 
        c, err := aes.NewCipher(key)
29
 
        if err != nil {
30
 
                return err
31
 
        }
32
 
        w = cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}
33
 
        w2, err := gzip.NewWriter(w)
34
 
        if err != nil {
35
 
                return err
36
 
        }
37
 
        defer w2.Close()
38
 
        _, err = io.Copy(w2, r)
39
 
        return err
40
 
}
41
 
 
42
 
func DecryptAndGunzip(dstfile, srcfile string, key, iv []byte) error {
43
 
        f, err := os.Open(srcfile)
44
 
        if err != nil {
45
 
                return err
46
 
        }
47
 
        defer f.Close()
48
 
        c, err := aes.NewCipher(key)
49
 
        if err != nil {
50
 
                return err
51
 
        }
52
 
        r := cipher.StreamReader{S: cipher.NewOFB(c, iv), R: f}
53
 
        r2, err := gzip.NewReader(r)
54
 
        if err != nil {
55
 
                return err
56
 
        }
57
 
        w, err := os.Create(dstfile)
58
 
        if err != nil {
59
 
                return err
60
 
        }
61
 
        defer w.Close()
62
 
        _, err = io.Copy(w, r2)
63
 
        return err
64
 
}
65
 
 
66
 
func main() {
67
 
        err := EncryptAndGzip(
68
 
                "/tmp/passwd.gz",
69
 
                "/etc/passwd",
70
 
                make([]byte, 16),
71
 
                make([]byte, 16),
72
 
        )
73
 
        if err != nil {
74
 
                log.Fatal(err)
75
 
        }
76
 
        err = DecryptAndGunzip(
77
 
                "/dev/stdout",
78
 
                "/tmp/passwd.gz",
79
 
                make([]byte, 16),
80
 
                make([]byte, 16),
81
 
        )
82
 
        if err != nil {
83
 
                log.Fatal(err)
84
 
        }
85
 
}