-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.go
executable file
·286 lines (236 loc) · 5.75 KB
/
utilities.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package secure
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"strings"
"golang.org/x/crypto/scrypt"
)
// EncodeBase64 encodes data in base64 format.
func EncodeBase64(data []byte) []byte {
enc := base64.StdEncoding
buf := make([]byte, enc.EncodedLen(len(data)))
enc.Encode(buf, data)
return buf
}
// DecodeBase64 decodes data in base64 format.
func DecodeBase64(data []byte) ([]byte, error) {
enc := base64.StdEncoding
dbuf := make([]byte, enc.DecodedLen(len(data)))
n, err := enc.Decode(dbuf, []byte(data))
return dbuf[:n], err
}
// ScryptDeriveKey derives a 32-bytes key from a variable length password.
func ScryptDeriveKey(password []byte, 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
}
// RSAGenerateKeyPair generates a key pair from a variable bit size.
func RSAGenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
privKey, _ := rsa.GenerateKey(rand.Reader, bits)
return privKey, &privKey.PublicKey
}
// RSAExportPrivateKeyAsPEM encodes a private key in a PEM block.
func RSAExportPrivateKeyAsPEM(privKey *rsa.PrivateKey) []byte {
privBytes := x509.MarshalPKCS1PrivateKey(privKey)
privPem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privBytes,
},
)
return privPem
}
// RSAParsePrivateKeyFromPEM decodes a private key from a PEM block.
func RSAParsePrivateKeyFromPEM(privPem []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privPem)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privKey, nil
}
// RSAExportPublicKeyAsPEM encodes a public key in a PEM block.
func RSAExportPublicKeyAsPEM(pubKey *rsa.PublicKey) ([]byte, error) {
pubBytes, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return nil, err
}
pubPem := pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubBytes,
},
)
return pubPem, nil
}
// RSAParsePublicKeyFromPEM decodes a public key from a PEM block.
func RSAParsePublicKeyFromPEM(pubPem []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(pubPem)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
switch pubKey := pubKey.(type) {
case *rsa.PublicKey:
return pubKey, nil
default:
break
}
return nil, errors.New("key type is not RSA")
}
// GenerateRandomBytes generates securely random bytes.
func GenerateRandomBytes(length int) ([]byte, error) {
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return nil, err
}
return b, nil
}
// GenerateRandomString generates a random string.
func GenerateRandomString(length int, upperCase bool, lowerCase bool, digits bool, symbols bool) (string, error) {
var chars = ""
if upperCase {
chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
if lowerCase {
chars += "abcdefghijklmnopqrstuvwxyz"
}
if digits {
chars += "0123456789"
}
if symbols {
chars += "[]!\"#$%&'()*+,-./:;<=>?@\\^_`{|}~"
}
if length == 0 {
length = 16
}
clen := len(chars)
if clen < 2 || clen > 256 {
return "", errors.New("wrong charset length")
}
maxrb := 255 - (256 % clen)
b := make([]byte, length)
r := make([]byte, length+(length/4))
i := 0
for {
if _, err := rand.Read(r); err != nil {
return "", errors.New("error reading random bytes")
}
for _, rb := range r {
c := int(rb)
if c > maxrb {
continue
}
b[i] = chars[c%clen]
i++
if i == length {
return string(b), nil
}
}
}
}
// GenerateRandomStringURLSafe generates a URL-safe, base64 encoded, random string.
func GenerateRandomStringURLSafe(length int) (string, error) {
b, err := GenerateRandomBytes(length)
return base64.URLEncoding.EncodeToString(b), err
}
// GenerateHumanPassword generates a human readable password.
func GenerateHumanPassword(letters int, digits int) (string, error) {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
if letters == 0 && digits == 0 {
letters = 4
digits = 4
}
length := letters + digits
clen := len(chars)
if clen < 2 || clen > 256 {
return "", errors.New("wrong charset length")
}
maxrb := 255 - (256 % clen)
bl := make([]byte, letters)
bn := make([]byte, digits)
r := make([]byte, length+(length/4))
il := 0
in := 0
for {
if _, err := rand.Read(r); err != nil {
return "", errors.New("error reading random bytes")
}
for _, rb := range r {
c := int(rb)
if c > maxrb {
continue
}
cc := chars[c%clen]
switch charType(cc) {
case "vowel":
if il > 0 {
if (charType(bl[il-1])) != "vowel" && il < letters {
bl[il] = cc
il++
}
} else {
if letters > 0 {
bl[0] = cc
il++
}
}
case "digit":
if in < digits {
bn[in] = cc
in++
}
case "consonant":
if il > 0 {
if (charType(bl[il-1])) != "consonant" && il < letters {
bl[il] = cc
il++
}
} else {
if letters > 0 {
bl[0] = cc
il++
}
}
}
if il+in == length {
b := string(bl) + string(bn)
if c%2 == 0 {
b = string(bn) + string(bl)
}
return b, nil
}
}
}
}
// charType returns if a char is a vowel, a consonant or a digit.
func charType(char byte) string {
const vowels = "aeiou"
const digits = "0123456789"
if strings.Contains(vowels, string(char)) {
return "vowel"
} else if strings.Contains(digits, string(char)) {
return "digit"
} else {
return "consonant"
}
}