-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
179 lines (167 loc) · 4.32 KB
/
utils.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
package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"github.com/manifoldco/promptui"
"github.com/stefins/glitzy/src/models"
"golang.org/x/crypto/scrypt"
"gorm.io/gorm"
)
// GetInfo input the user info from terminal
func GetInfo(db *gorm.DB) *models.User {
MainPassword := AddOrCheckMainPassword(db)
ServiceName := GetNormalString("Service Name ")
Username := GetNormalString("Username ")
Password := getProtectedString("Password")
cipherText, err := Encrypt([]byte(MainPassword), []byte(Password))
if err != nil {
log.Fatalf("%v", err)
}
return &models.User{Name: ServiceName, Username: Username, Password: hex.EncodeToString(cipherText)}
}
// GetNormalString input normal text from terminal
func GetNormalString(promptText string) string {
validate := func(input string) error {
if len(input) < 4 {
return errors.New("must be more than 4 characters")
}
return nil
}
prompt := promptui.Prompt{
Label: promptText,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
log.Fatalf("%v", err)
}
return result
}
// getProtectedString input protected text from terminal
func getProtectedString(promptText string) string {
validate := func(input string) error {
if len(input) < 6 {
return errors.New("password must have more than 6 characters")
}
return nil
}
prompt := promptui.Prompt{
Label: promptText,
Validate: validate,
Mask: '*',
}
result, err := prompt.Run()
if err != nil {
log.Fatalf("%v", err)
}
return result
}
// AddOrCheckMainPassword add a main password to the application
func AddOrCheckMainPassword(db *gorm.DB) string {
if (db.Find(&models.Main{}).RowsAffected == 0) {
fmt.Println("Main Password not configured!")
mainPass := getProtectedString("Enter the Main Password")
mainPassRe := getProtectedString("Enter the Main Password again")
if mainPass != mainPassRe {
log.Fatalf("Password Mismatch!")
}
hash := sha256.New()
hash.Write([]byte(mainPass))
err := db.Create(&models.Main{PasswordHash: hex.EncodeToString(hash.Sum(nil))}).Error
if err != nil {
log.Fatalf("%v", err)
}
return mainPassRe
} else {
var dbInfo models.Main
db.First(&dbInfo)
userInputMainPass := getProtectedString("Enter the Main Password")
hash := sha256.New()
hash.Write([]byte(userInputMainPass))
if dbInfo.PasswordHash == hex.EncodeToString(hash.Sum(nil)) {
fmt.Println("Authenticated with Main Password")
} else {
fmt.Println("Invalid Password!")
os.Exit(0)
}
return userInputMainPass
}
}
// Encrypt is used to encrypt the clear text password
func Encrypt(key, data []byte) ([]byte, error) {
key, salt, err := DeriveKey(key, nil)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
ciphertext = append(ciphertext, salt...)
return ciphertext, nil
}
// Decrypt is used to decrypt the cipher with the password
func Decrypt(key, data []byte) ([]byte, error) {
salt, data := data[len(data)-32:], data[:len(data)-32]
key, _, err := DeriveKey(key, salt)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce, ciphertext := data[:gcm.NonceSize()], data[gcm.NonceSize():]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
// DeriveKey derive a key with the password
func DeriveKey(password, salt []byte) ([]byte, []byte, error) {
if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
return nil, nil, err
}
}
key, err := scrypt.Key(password, salt, 32768, 8, 1, 32)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}
// AddOrCreateConfigDir creates the .glitzy folder in the home directory
func AddOrCreateConfigDir() error {
HOME := os.Getenv("HOME")
if _, err := os.Stat(HOME + "/.glitzy"); err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(HOME+"/.glitzy", 0755)
if err != nil {
return err
}
}
}
return nil
}