forked from 2tvenom/myreplication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswd.go
31 lines (29 loc) · 753 Bytes
/
passwd.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
package myreplication
import "crypto/sha1"
//copied from from github.com/ziutek/mymysql/native/passwd.go
func encryptedPasswd(password string, scramble []byte) (out []byte) {
if len(password) == 0 {
return
}
// stage1_hash = SHA1(password)
// SHA1 encode
crypt := sha1.New()
crypt.Write([]byte(password))
stg1Hash := crypt.Sum(nil)
// token = SHA1(SHA1(stage1_hash), scramble) XOR stage1_hash
// SHA1 encode again
crypt.Reset()
crypt.Write(stg1Hash)
stg2Hash := crypt.Sum(nil)
// SHA1 2nd hash and scramble
crypt.Reset()
crypt.Write(scramble)
crypt.Write(stg2Hash)
stg3Hash := crypt.Sum(nil)
// XOR with first hash
out = make([]byte, len(scramble))
for ii := range scramble {
out[ii] = stg3Hash[ii] ^ stg1Hash[ii]
}
return
}