Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: update md5 hash to use hex #53003

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have both benchmarks? I.e. BenchmarkMD5HashOld and BenchmarkMD5HashNew.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have both benchmarks? I.e. BenchmarkMD5HashOld and BenchmarkMD5HashNew.

I understand that generally only one benchmark method is used, with two specific methods executed inside, one improved and one old, and then the results are compared using the official tool benchstat

}
}

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

Expand Down