-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
81 lines (62 loc) · 1.47 KB
/
crypto.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
package retrologin
import (
crand "crypto/rand"
"errors"
"regexp"
"strings"
)
func decryptedPassword(encryptedPassword, key string) (string, error) {
if key == "" {
return "", errors.New("key is empty")
}
if len(encryptedPassword)%2 != 0 ||
!regexp.MustCompile(`^[a-zA-Z\d\-_]{2,64}$`).MatchString(encryptedPassword) {
return "", errors.New("the encrypted password is malformed")
}
hash := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
hashMap := make(map[rune]rune)
for i, v := range hash {
hashMap[v] = rune(i)
}
var pPass, pKey rune
var aPass, aKey, anb, anb2, sum1, sum2 int
sb := &strings.Builder{}
for i := 0; i < len(encryptedPassword); i += 2 {
pKey = rune(key[i/2])
anb = int(hashMap[rune(encryptedPassword[i])])
anb2 = int(hashMap[rune(encryptedPassword[i+1])])
sum1 = anb + len(hash)
sum2 = anb2 + len(hash)
aPass = sum1 - int(pKey)
if aPass < 0 {
aPass += len(hash)
}
aPass *= 16
aKey = sum2 - int(pKey)
if aKey < 0 {
aKey += len(hash)
}
pPass = rune(aPass + aKey)
sb.WriteRune(pPass)
}
return sb.String(), nil
}
func randomSalt(n int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyz"
p, err := randomBytes(n)
if err != nil {
return "", err
}
for i, v := range p {
p[i] = charset[v%byte(len(charset))]
}
return string(p), nil
}
func randomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := crand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}