-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.go
244 lines (200 loc) · 4.2 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bytes"
"errors"
"io/ioutil"
"net/url"
"os"
"strconv"
"strings"
"github.com/tuotoo/qrcode"
"github.com/unix4fun/pemaead"
)
var (
ErrIO = errors.New("I/O error")
ErrCrypto = errors.New("crypto error")
ErrInvalidUrl = errors.New("invalid otp url")
ErrInvalidQr = errors.New("invalid qrcode")
)
// XXX this function is not clean yet.. but does the job for now..
func urlToTotpEntry(otpurl string) (string, *totpEntry, error) {
var issuer string
u, err := url.Parse(otpurl)
if err != nil {
return "", nil, err
}
if u.Host != "totp" {
return "", nil, ErrInvalidQr
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return "", nil, err
}
// issuer
issuerArray := strings.Split(u.Path[1:], ":")
switch len(issuerArray) {
case 1:
issuer = issuerArray[0]
case 2:
issuer = issuerArray[1]
default:
return "", nil, ErrInvalidQr
}
sstr, ok := v["secret"]
if !ok {
return "", nil, ErrInvalidQr
}
dstr, ok := v["digits"]
if !ok {
return "", nil, ErrInvalidQr
}
d, err := strconv.Atoi(dstr[0])
if err != nil {
return "", nil, err
}
pstr, ok := v["period"]
if !ok {
return "", nil, ErrInvalidQr
}
p, err := strconv.Atoi(pstr[0])
if err != nil {
return "", nil, err
}
hstr, ok := v["algorithm"]
if !ok {
return "", nil, ErrInvalidQr
}
h := strings.ToLower(hstr[0])
e := totpEntry{
Secret: sstr[0],
Digit: d,
Period: p,
Hash: h,
}
return issuer, &e, nil
}
func qrRead(filename string) (string, *totpEntry, error) {
fi, err := os.Open(filename)
if err != nil {
return "", nil, err
}
defer fi.Close()
qrmatrix, err := qrcode.Decode(fi)
if err != nil {
return "", nil, err
}
otpurl := qrmatrix.Content
return urlToTotpEntry(otpurl)
}
func fileCreate(fileName string, password []byte) error {
fd, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_EXCL|os.O_SYNC, 0700)
if err != nil {
return err
}
pemfd, err := pemaead.NewWriter(fd, password, pemaead.CipherAESGCM, pemaead.DerivateArgon2)
if err != nil {
return ErrCrypto
}
t := &totpMap{}
tJson, err := t.marshal()
if err != nil {
return err
}
_, err = pemfd.Write(tJson)
if err != nil {
return err
}
return pemfd.Close()
}
func fileRead(fileName string, password []byte) (*totpMap, error) {
fd, err := os.Open(fileName)
if err != nil {
//return nil, err
return nil, err
}
defer fd.Close()
pemfd, err := pemaead.NewReader(fd, password)
if err != nil {
//return nil, err
return nil, ErrCrypto
}
jsonMap, err := ioutil.ReadAll(pemfd)
if err != nil {
//return nil, err
return nil, err
}
return unmarshal(jsonMap)
}
func fileRawRead(filename string, password []byte) ([]byte, error) {
fd, err := os.Open(filename)
if err != nil {
//return nil, err
return nil, err
}
defer fd.Close()
pemfd, err := pemaead.NewReader(fd, password)
if err != nil {
//return nil, err
panic(err)
return nil, ErrCrypto
}
return ioutil.ReadAll(pemfd)
}
// quick hack to make a bytes.Buffer a WriteCloser interface
type wcbuf struct {
b bytes.Buffer
}
func (w *wcbuf) Close() error {
return nil
}
func (w *wcbuf) Write(b []byte) (int, error) {
return w.b.Write(b)
}
func (w *wcbuf) Bytes() []byte {
return w.b.Bytes()
}
func fileRawWrite(filename string, password []byte) ([]byte, error) {
var b wcbuf
fd, err := os.Open(filename)
if err != nil {
//return nil, err
return nil, err
}
defer fd.Close()
fileReadBuf, err := ioutil.ReadAll(fd)
if err != nil {
return nil, err
}
pemfd, err := pemaead.NewWriter(&b, password, pemaead.CipherAESGCM, pemaead.DerivateArgon2)
if err != nil {
return nil, ErrCrypto
}
_, err = pemfd.Write(fileReadBuf)
if err != nil {
return nil, err
}
err = pemfd.Close()
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func fileWrite(fileName string, password []byte, tmap *totpMap) error {
fd, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
return err
}
pemfd, err := pemaead.NewWriter(fd, password, pemaead.CipherAESGCM, pemaead.DerivateArgon2)
if err != nil {
return ErrCrypto
}
tJson, err := tmap.marshal()
if err != nil {
return err
}
_, err = pemfd.Write(tJson)
if err != nil {
return err
}
return pemfd.Close()
}