-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypt.go
221 lines (175 loc) · 4.86 KB
/
crypt.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
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"io"
"strings"
)
var (
cryptKey []byte // a key used in crypto/cipher algorithm
)
// SetKey changes a key that package using for crypto/cipher algorithm
func SetKey(key []byte) error {
if diff := aes.BlockSize - len(key)%aes.BlockSize; diff > 0 {
for diff > 0 {
key = append(key, 0)
diff--
}
}
cryptKey = key
return nil
}
// GetKey returns a key used in crypto/cipher algorithm
func GetKey() []byte {
if cryptKey == nil {
// ignore error - no logging ability
_ = SetKey([]byte("hard-coded key:)"))
}
return cryptKey
}
// EncryptString encrypts string with crypto/cipher, salting it and makes base64.StdEncoding, returns blank string if encoding fails
func EncryptString(data string) string {
// cypher encryption
result, err := EncryptData([]byte(data))
if err != nil {
return ""
}
// salting
salt := []byte{':'}
if cryptKey := GetKey(); len(cryptKey) > 0 {
salt = append(salt, cryptKey[0])
}
result = append(result, salt...)
//base64 encoding
return base64.StdEncoding.EncodeToString(result)
}
// DecryptString decodes base64.StdEncoding string un-salting it and then decrypts it with crypto/cipher, returns original value or error
func DecryptString(data string) string {
// base64 decoding
decodedData, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return data
}
// checking and removing salt
salt := ":"
if cryptKey := GetKey(); len(cryptKey) > 0 {
salt += string(cryptKey[0])
}
saltIdx := len(decodedData) - len(salt)
if saltIdx < 0 || salt != string(decodedData[saltIdx:]) {
return data
}
decodedData = decodedData[0:saltIdx]
// making cypher decryption
result, err := DecryptData(decodedData)
if err != nil {
return data
}
return string(result)
}
// EncryptData encrypts given data with crypto/cipher algorithm
func EncryptData(data []byte) ([]byte, error) {
var buffer bytes.Buffer
writer, err := EncryptWriter(&buffer)
if err != nil {
return nil, err
}
_, err = writer.Write(data)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// DecryptData decrypts given data with crypto/cipher algorithm
func DecryptData(encodedData []byte) ([]byte, error) {
result := make([]byte, len(encodedData))
reader, err := EncryptReader(bytes.NewReader(encodedData))
if err != nil {
return nil, err
}
_, err = reader.Read(result)
if err != nil {
return nil, err
}
return result, nil
}
// EncryptReader decrypts given stream with crypto/cipher algorithm
func EncryptReader(rawReader io.Reader) (io.Reader, error) {
cryptKey := GetKey()
cipherBlock, err := aes.NewCipher(cryptKey)
if err != nil {
return nil, err
}
iv := cryptKey[:aes.BlockSize]
stream := cipher.NewOFB(cipherBlock, iv)
return &cipher.StreamReader{S: stream, R: rawReader}, nil
}
// EncryptWriter encrypts given stream with crypto/cipher algorithm
func EncryptWriter(rawWriter io.Writer) (io.Writer, error) {
cryptKey := GetKey()
cipherBlock, err := aes.NewCipher(cryptKey)
if err != nil {
return nil, err
}
iv := cryptKey[:aes.BlockSize]
stream := cipher.NewOFB(cipherBlock, iv)
return &cipher.StreamWriter{S: stream, W: rawWriter}, nil
}
// CryptToURLString encrypts given string with base64 and hex encoding
func CryptToURLString(raw []byte) string {
result := hex.EncodeToString([]byte(base64.StdEncoding.EncodeToString(raw)))
return result
}
// CryptAsURLString encrypts given string with base64 and hex encoding
func CryptAsURLString(rawString string) string {
result := hex.EncodeToString([]byte(base64.StdEncoding.EncodeToString([]byte(rawString))))
return result
}
// DecryptURLString decode given encoded string and returns decoded value
func DecryptURLString(encodedString string) (string, error) {
var result string
data, err := hex.DecodeString(encodedString)
if err != nil {
return result, err
}
data, err = base64.StdEncoding.DecodeString(string(data))
if err != nil {
return result, err
}
if data != nil {
result = string(data)
}
return result, nil
}
// PasswordEncode encode inputed password with using salt, if no salt it will use default one
func PasswordEncode(password string, salt string) string {
hasher := md5.New()
if salt == "" {
salt := ":"
if len(password) > 2 {
salt += password[0:1]
}
// ignore error - no logging ability
_, _ = hasher.Write([]byte(password + salt))
} else {
// ignore error - no logging ability
_, _ = hasher.Write([]byte(salt + password))
}
return hex.EncodeToString(hasher.Sum(nil))
}
// PasswordCheck compare inputed password with stored one
func PasswordCheck(password string, input string) bool {
password = strings.TrimSpace(password)
input = strings.TrimSpace(input)
salt := ""
tmp := strings.Split(password, ":")
if len(tmp) == 2 {
password = tmp[0]
salt = tmp[1]
}
return PasswordEncode(input, salt) == password
}