forked from mindoc-org/mindoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
146 lines (119 loc) · 3.03 KB
/
password.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
package utils
import (
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"io"
mt "math/rand"
"strconv"
"strings"
)
const (
saltSize = 16
delmiter = "$"
stretching_password = 500
salt_local_secret = "ahfw*&TGdsfnbi*^Wt"
)
//加密密码
func PasswordHash(pass string) (string, error) {
saltSecret, err := salt_secret()
if err != nil {
return "", err
}
salt, err := salt(salt_local_secret + saltSecret)
if err != nil {
return "", err
}
interation := randInt(1, 20)
hash, err := hash(pass, saltSecret, salt, int64(interation))
if err != nil {
return "", err
}
interationString := strconv.Itoa(interation)
password := saltSecret + delmiter + interationString + delmiter + hash + delmiter + salt
return password, nil
}
//校验密码是否有效
func PasswordVerify(hashing string, pass string) (bool, error) {
data := trimSaltHash(hashing)
interation, _ := strconv.ParseInt(data["interation_string"], 10, 64)
has, err := hash(pass, data["salt_secret"], data["salt"], int64(interation))
if err != nil {
return false, err
}
if (data["salt_secret"] + delmiter + data["interation_string"] + delmiter + has + delmiter + data["salt"]) == hashing {
return true, nil
} else {
return false, nil
}
}
func hash(pass string, salt_secret string, salt string, interation int64) (string, error) {
var passSalt = salt_secret + pass + salt + salt_secret + pass + salt + pass + pass + salt
var i int
hashPass := salt_local_secret
hashStart := sha512.New()
hashCenter := sha256.New()
hashOutput := sha256.New224()
i = 0
for i <= stretching_password {
i = i + 1
_, err := hashStart.Write([]byte(passSalt + hashPass))
if err != nil {
return "", err
}
hashPass = hex.EncodeToString(hashStart.Sum(nil))
}
i = 0
for int64(i) <= interation {
i = i + 1
hashPass = hashPass + hashPass
}
i = 0
for i <= stretching_password {
i = i + 1
_, err := hashCenter.Write([]byte(hashPass + salt_secret))
if err != nil {
return "", err
}
hashPass = hex.EncodeToString(hashCenter.Sum(nil))
}
if _,err := hashOutput.Write([]byte(hashPass + salt_local_secret)); err != nil {
return "", err
}
hashPass = hex.EncodeToString(hashOutput.Sum(nil))
return hashPass, nil
}
func trimSaltHash(hash string) map[string]string {
str := strings.Split(hash, delmiter)
return map[string]string{
"salt_secret": str[0],
"interation_string": str[1],
"hash": str[2],
"salt": str[3],
}
}
func salt(secret string) (string, error) {
buf := make([]byte, saltSize, saltSize+md5.Size)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
return "", err
}
hash := md5.New()
hash.Write(buf)
hash.Write([]byte(secret))
return hex.EncodeToString(hash.Sum(buf)), nil
}
func salt_secret() (string, error) {
rb := make([]byte, randInt(10, 100))
_, err := rand.Read(rb)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(rb), nil
}
func randInt(min int, max int) int {
return min + mt.Intn(max-min)
}