forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pingcap#304 from pingcap/shenli/hash-password
*: Avoid store plaintext password in mysql.user table.
- Loading branch information
Showing
5 changed files
with
124 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2013 The ql Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSES/QL-LICENSE file. | ||
|
||
// Copyright 2015 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/hex" | ||
|
||
"github.com/juju/errors" | ||
) | ||
|
||
// CalcPassword is the algorithm convert hashed password to auth string. | ||
// See: https://dev.mysql.com/doc/internals/en/secure-password-authentication.html | ||
// SHA1( password ) XOR SHA1( "20-bytes random data from server" <concat> SHA1( SHA1( password ) ) ) | ||
func CalcPassword(scramble, sha1pwd []byte) []byte { | ||
if len(sha1pwd) == 0 { | ||
return nil | ||
} | ||
// scrambleHash = SHA1(scramble + SHA1(sha1pwd)) | ||
// inner Hash | ||
hash := Sha1Hash(sha1pwd) | ||
// outer Hash | ||
crypt := sha1.New() | ||
crypt.Write(scramble) | ||
crypt.Write(hash) | ||
scramble = crypt.Sum(nil) | ||
// token = scrambleHash XOR stage1Hash | ||
for i := range scramble { | ||
scramble[i] ^= sha1pwd[i] | ||
} | ||
return scramble | ||
} | ||
|
||
// Sha1Hash is an util function to calculate sha1 hash. | ||
func Sha1Hash(bs []byte) []byte { | ||
crypt := sha1.New() | ||
crypt.Write(bs) | ||
return crypt.Sum(nil) | ||
} | ||
|
||
// EncodePassword converts plaintext password to hashed hex string. | ||
func EncodePassword(pwd string) string { | ||
hash := Sha1Hash([]byte(pwd)) | ||
return hex.EncodeToString(hash) | ||
} | ||
|
||
// DecodePassword converts hex string password to byte array. | ||
func DecodePassword(pwd string) ([]byte, error) { | ||
x, err := hex.DecodeString(pwd) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return x, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2015 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
) | ||
|
||
var _ = Suite(&testAuthSuite{}) | ||
|
||
type testAuthSuite struct { | ||
} | ||
|
||
func (s *testAuthSuite) TestEncodePassword(c *C) { | ||
pwd := "123" | ||
c.Assert(EncodePassword(pwd), Equals, "40bd001563085fc35165329ea1ff5c5ecbdbbeef") | ||
} | ||
|
||
func (s *testAuthSuite) TestDecodePassword(c *C) { | ||
x, err := DecodePassword(EncodePassword("123")) | ||
c.Assert(err, IsNil) | ||
c.Assert(x, DeepEquals, Sha1Hash([]byte("123"))) | ||
} | ||
|
||
func (s *testAuthSuite) TestCalcPassword(c *C) { | ||
salt := []byte{116, 32, 122, 120, 2, 51, 33, 66, 47, 85, 34, 39, 84, 58, 108, 14, 62, 47, 120, 126} | ||
pwd := Sha1Hash([]byte("123")) | ||
checkAuth := []byte{126, 168, 249, 64, 180, 223, 60, 240, 69, 249, 184, 57, 21, 34, 214, 219, 8, 193, 208, 55} | ||
c.Assert(CalcPassword(salt, pwd), DeepEquals, checkAuth) | ||
} |