-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
99 lines (86 loc) · 1.75 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func save(file, passphrase string, sites []Site) error {
// If the file doesn't exist then create it
if _, err := os.Stat(file); os.IsNotExist(err) {
_, err = os.Create(file)
if err != nil {
return err
}
}
// Marshal the JSON
b, err := json.Marshal(sites)
if err != nil {
return err
}
b, err = encrypt(encodeBase64(b), passphrase)
if err != nil {
return err
}
// Compress the contents
var buffer bytes.Buffer
gzip := gzip.NewWriter(&buffer)
if err != nil {
return err
}
gzip.Write(b)
gzip.Close()
// Write to the file
fi, err := os.OpenFile(file, os.O_WRONLY, 0666)
if err != nil {
return err
}
_, err = fi.Write(buffer.Bytes())
if err != nil {
return err
}
fi.Close()
return nil
}
// Read the password book
func read(file, passphrase string) ([]Site, error) {
// If the file doesn't exist yet no worries
if _, err := os.Stat(file); os.IsNotExist(err) {
return []Site{}, nil
}
// Bring in the compressed data
fi, err := os.Open(file)
if err != nil {
return nil, err
}
// Decompress the file contents
gzip, err := gzip.NewReader(fi)
if err != nil {
return nil, err
}
decompressed, err := ioutil.ReadAll(gzip)
gzip.Close()
// Decrypt the contents
decompressed, err = decrypt(decompressed, passphrase)
if err != nil {
return nil, err
}
decompressed = decodeBase64(decompressed)
// Unmarshal the JSON information
var sites []Site
err = json.Unmarshal(decompressed, &sites)
if err != nil {
return nil, err
}
fi.Close()
return sites, nil
}
// Get the book name
func getBookname(profile string) string {
hash := md5.New()
hash.Write([]byte(profile))
return fmt.Sprintf("%x", string(hash.Sum(nil)))
}