Skip to content

Commit

Permalink
auth: fix iterations decode error in hashCrypt (#43578)
Browse files Browse the repository at this point in the history
close #43576
  • Loading branch information
asjdf committed May 10, 2023
1 parent ec03200 commit bfa042b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions parser/auth/caching_sha2.go
Expand Up @@ -169,7 +169,7 @@ func hashCrypt(plaintext string, salt []byte, iterations int, hash func([]byte)
// 22
buf := bytes.NewBuffer(make([]byte, 0, 100))
buf.Write([]byte{'$', 'A', '$'})
rounds := fmt.Sprintf("%03d", iterations/ITERATION_MULTIPLIER)
rounds := fmt.Sprintf("%03X", iterations/ITERATION_MULTIPLIER)
buf.WriteString(rounds)
buf.Write([]byte{'$'})
buf.Write(salt)
Expand Down Expand Up @@ -201,7 +201,7 @@ func CheckHashingPassword(pwhash []byte, password string, hash string) (bool, er
return false, errors.New("digest type is incompatible")
}

iterations, err := strconv.Atoi(string(pwhashParts[2]))
iterations, err := strconv.ParseInt(string(pwhashParts[2]), 16, 64)
if err != nil {
return false, errors.New("failed to decode iterations")
}
Expand All @@ -211,9 +211,9 @@ func CheckHashingPassword(pwhash []byte, password string, hash string) (bool, er
var newHash string
switch hash {
case mysql.AuthCachingSha2Password:
newHash = hashCrypt(password, salt, iterations, Sha256Hash)
newHash = hashCrypt(password, salt, int(iterations), Sha256Hash)
case mysql.AuthTiDBSM3Password:
newHash = hashCrypt(password, salt, iterations, Sm3Hash)
newHash = hashCrypt(password, salt, int(iterations), Sm3Hash)
}

return bytes.Equal(pwhash, []byte(newHash)), nil
Expand Down
2 changes: 1 addition & 1 deletion parser/auth/caching_sha2_test.go
Expand Up @@ -54,7 +54,7 @@ func TestCheckShaPasswordDigestTypeIncompatible(t *testing.T) {

func TestCheckShaPasswordIterationsInvalid(t *testing.T) {
pwd := "not_foobar"
pwhash, _ := hex.DecodeString("24412430304124031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
pwhash, _ := hex.DecodeString("24412430304724031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
_, err := CheckHashingPassword(pwhash, pwd, mysql.AuthCachingSha2Password)
require.Error(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion parser/auth/tidb_sm3_test.go
Expand Up @@ -69,7 +69,7 @@ func TestCheckSM3PasswordDigestTypeIncompatible(t *testing.T) {

func TestCheckSM3PasswordIterationsInvalid(t *testing.T) {
pwd := "not_foobar"
pwhash, _ := hex.DecodeString("24412430304124031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
pwhash, _ := hex.DecodeString("24412430304724031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
_, err := CheckHashingPassword(pwhash, pwd, mysql.AuthTiDBSM3Password)
require.Error(t, err)
}
Expand Down

0 comments on commit bfa042b

Please sign in to comment.