-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
72 lines (60 loc) · 2.06 KB
/
auth.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
// mysqlx - MySQL driver for Go's database/sql package and MySQL X Protocol.
// Copyright (c) 2017-2018 Alexey Palazhchenko
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mysqlx
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
)
// https://github.com/mysql/mysql-server/blob/mysql-5.7.19/rapid/plugin/x/mysqlxtest_src/password_hasher.cc
// https://github.com/mysql/mysql-server/blob/mysql-5.7.19/rapid/plugin/x/mysqlxtest_src/mysql41_hash.cc
func scrambleMySQL41(password string, authData []byte) []byte {
hash1 := sha1.Sum([]byte(password))
hash2 := sha1.Sum(hash1[:])
h := sha1.New()
h.Write(authData)
h.Write(hash2[:])
res := h.Sum(nil)
for i := range res {
res[i] ^= hash1[i]
}
return res
}
func authDataMySQL41(database, username, password string, authData []byte) ([]byte, error) {
if len(authData) != 20 {
return nil, fmt.Errorf("authDataMySQL41: expected authData to has 20 bytes, got %d", len(authData))
}
res := database + "\x00" + username + "\x00"
if password == "" {
return []byte(res), nil
}
res += fmt.Sprintf("*%X", scrambleMySQL41(password, authData))
return []byte(res), nil
}
func scrambleSHA256(password string, authData []byte) []byte {
// SHA256(password) XOR SHA256(SHA256(SHA256(password)) + authData)
hash1 := sha256.Sum256([]byte(password))
hash1h := sha256.Sum256(hash1[:])
h := sha256.New()
h.Write(hash1h[:])
h.Write(authData)
hash2 := h.Sum(nil)
res := make([]byte, sha256.Size)
for i := 0; i < sha256.Size; i++ {
res[i] = hash1[i] ^ hash2[i]
}
return res
}
// https://dev.mysql.com/worklog/task/?id=10992
func authDataSHA256(database, username, password string, authData []byte) ([]byte, error) {
if len(authData) != 20 {
return nil, fmt.Errorf("authDataSHA256: expected authData to has 20 bytes, got %d", len(authData))
}
res := database + "\x00" + username + "\x00"
res += fmt.Sprintf("%X", scrambleSHA256(password, authData))
return []byte(res), nil
}