• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-sql-driver / mysql / 14665995848

25 Apr 2025 01:39PM UTC coverage: 82.772% (-0.2%) from 82.961%
14665995848

Pull #1696

github

rusher
use named constants for caching_sha2_password
Pull Request #1696: Authentication

321 of 387 new or added lines in 9 files covered. (82.95%)

7 existing lines in 3 files now uncovered.

3315 of 4005 relevant lines covered (82.77%)

2440698.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/auth_mysql_native.go
1
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2
//
3
// Copyright 2023 The Go-MySQL-Driver Authors. All rights reserved.
4
//
5
// This Source Code Form is subject to the terms of the Mozilla Public
6
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7
// You can obtain one at http://mozilla.org/MPL/2.0/.
8

9
package mysql
10

11
import "crypto/sha1"
12

13
// NativePasswordPlugin implements the mysql_native_password authentication
14
type NativePasswordPlugin struct {
15
        SimpleAuth
16
}
17

18
func init() {
32✔
19
        RegisterAuthPlugin(&NativePasswordPlugin{})
32✔
20
}
32✔
21

22
func (p *NativePasswordPlugin) PluginName() string {
32✔
23
        return "mysql_native_password"
32✔
24
}
32✔
25

26
func (p *NativePasswordPlugin) InitAuth(authData []byte, cfg *Config) ([]byte, error) {
11,265✔
27
        if !cfg.AllowNativePasswords {
11,329✔
28
                return nil, ErrNativePassword
64✔
29
        }
64✔
30
        if cfg.Passwd == "" {
11,286✔
31
                return nil, nil
85✔
32
        }
85✔
33
        return p.scramblePassword(authData[:20], cfg.Passwd), nil
11,116✔
34
}
35

36
// Hash password using 4.1+ method (SHA1)
37
func (p *NativePasswordPlugin) scramblePassword(scramble []byte, password string) []byte {
11,116✔
38
        if len(password) == 0 {
11,116✔
NEW
39
                return nil
×
NEW
40
        }
×
41

42
        // stage1Hash = SHA1(password)
43
        crypt := sha1.New()
11,116✔
44
        crypt.Write([]byte(password))
11,116✔
45
        stage1 := crypt.Sum(nil)
11,116✔
46

11,116✔
47
        // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
11,116✔
48
        // inner Hash
11,116✔
49
        crypt.Reset()
11,116✔
50
        crypt.Write(stage1)
11,116✔
51
        hash := crypt.Sum(nil)
11,116✔
52

11,116✔
53
        // outer Hash
11,116✔
54
        crypt.Reset()
11,116✔
55
        crypt.Write(scramble)
11,116✔
56
        crypt.Write(hash)
11,116✔
57
        scramble = crypt.Sum(nil)
11,116✔
58

11,116✔
59
        // token = scrambleHash XOR stage1Hash
11,116✔
60
        for i := range scramble {
233,436✔
61
                scramble[i] ^= stage1[i]
222,320✔
62
        }
222,320✔
63
        return scramble
11,116✔
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc