-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
86 lines (71 loc) · 1.78 KB
/
data.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/binary"
"encoding/json"
"io/ioutil"
"strings"
"golang.org/x/crypto/pbkdf2"
)
type Account struct {
Secret string `json:"secret"`
Label string `json:"label"`
Digits uint `json:"digits"`
Period uint `json:"period"`
Type string `json:"type"`
}
func decrypt(ciphertext []byte, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, err
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
func ReadRawFile(filename string) (uint32, []byte, []byte, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return 0, nil, nil, err
}
iterations := binary.BigEndian.Uint32(bytes[0:4])
salt := bytes[4:16]
encryptedData := bytes[16:]
return iterations, salt, encryptedData, nil
}
func ReadDatabase(filename string, password string) ([]Account, error) {
iterations, salt, encryptedData, err := ReadRawFile(filename)
if err != nil {
return nil, err
}
key := pbkdf2.Key([]byte(password), salt, int(iterations), 32, sha1.New)
data, err := decrypt(encryptedData, key)
if err != nil {
return nil, err
}
accounts := make([]Account, 0)
json.NewDecoder(strings.NewReader(string(data))).Decode(&accounts)
return accounts, nil
}
func FindAccountByLabel(accounts []Account, label string) *Account {
labelLower := strings.ToLower(label)
for _, entry := range accounts {
if strings.Contains(strings.ToLower(entry.Label), labelLower) {
return &entry
}
}
return nil
}