Skip to content

Commit

Permalink
expression: update md5 hash to use hex (#53003)
Browse files Browse the repository at this point in the history
close #53018
  • Loading branch information
qichengzx committed May 7, 2024
1 parent 96f9797 commit 013a4e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/expression/builtin_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"fmt"
"hash"
"io"
Expand Down Expand Up @@ -635,7 +636,8 @@ func (b *builtinMD5Sig) evalString(ctx EvalContext, row chunk.Row) (string, bool
return "", isNull, err
}
sum := md5.Sum([]byte(arg)) // #nosec G401
hexStr := fmt.Sprintf("%x", sum)
hexStr := hex.EncodeToString(sum[:])

return hexStr, false, nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/expression/builtin_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package expression

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
Expand Down Expand Up @@ -498,6 +499,25 @@ func TestMD5Hash(t *testing.T) {
require.NoError(t, err)
}

func MD5HashOld(arg string) string {
sum := md5.Sum([]byte(arg))
return fmt.Sprintf("%x", sum)
}

func MD5HashNew(arg string) string {
sum := md5.Sum([]byte(arg))
return hex.EncodeToString(sum[:])
}

func BenchmarkMD5Hash(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
MD5HashOld("abc")
//MD5HashNew("abc")
}
}

func TestRandomBytes(t *testing.T) {
ctx := createContext(t)

Expand Down

0 comments on commit 013a4e6

Please sign in to comment.