From c03d2436ce866386e6dc905ad30e7433a67c6f2b Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 31 Aug 2023 14:12:57 +0800 Subject: [PATCH] :sparkles: feat: str,byte - add new func: ShortMd5 and update byteutil.Md5 logic --- byteutil/byteutil.go | 21 +++++++++++++++++---- byteutil/byteutil_test.go | 7 +++++++ strutil/{crypto.go => hash.go} | 23 +++++++++-------------- strutil/{crypto_test.go => hash_test.go} | 6 +++++- 4 files changed, 38 insertions(+), 19 deletions(-) rename strutil/{crypto.go => hash.go} (78%) rename strutil/{crypto_test.go => hash_test.go} (81%) diff --git a/byteutil/byteutil.go b/byteutil/byteutil.go index 1f80a80c8..6f3044d25 100644 --- a/byteutil/byteutil.go +++ b/byteutil/byteutil.go @@ -4,6 +4,7 @@ package byteutil import ( "bytes" "crypto/md5" + "encoding/hex" "fmt" "math/rand" "strconv" @@ -14,12 +15,24 @@ import ( func Md5(src any) []byte { h := md5.New() - if s, ok := src.(string); ok { - h.Write([]byte(s)) - } else { + switch val := src.(type) { + case []byte: + h.Write(val) + case string: + h.Write([]byte(val)) + default: h.Write([]byte(fmt.Sprint(src))) } - return h.Sum(nil) + + bs := h.Sum(nil) // cap(bs) == 16 + dst := make([]byte, hex.EncodedLen(len(bs))) + hex.Encode(dst, bs) + return dst +} + +// ShortMd5 Generate a 16-bit md5 bytes. remove first 8 and last 8 bytes from 32-bit md5. +func ShortMd5(src any) []byte { + return Md5(src)[8:24] } // Random bytes generate diff --git a/byteutil/byteutil_test.go b/byteutil/byteutil_test.go index 0aec3519a..916ebf4a2 100644 --- a/byteutil/byteutil_test.go +++ b/byteutil/byteutil_test.go @@ -30,6 +30,13 @@ func TestFirstLine(t *testing.T) { func TestMd5(t *testing.T) { assert.NotEmpty(t, byteutil.Md5("abc")) assert.NotEmpty(t, byteutil.Md5([]int{12, 34})) + + assert.Eq(t, "202cb962ac59075b964b07152d234b70", string(byteutil.Md5("123"))) + assert.Eq(t, "900150983cd24fb0d6963f7d28e17f72", string(byteutil.Md5("abc"))) + + // short md5 + assert.Eq(t, "ac59075b964b0715", string(byteutil.ShortMd5("123"))) + assert.Eq(t, "3cd24fb0d6963f7d", string(byteutil.ShortMd5("abc"))) } func TestAppendAny(t *testing.T) { diff --git a/strutil/crypto.go b/strutil/hash.go similarity index 78% rename from strutil/crypto.go rename to strutil/hash.go index e87e5fff6..9cfa239fc 100644 --- a/strutil/crypto.go +++ b/strutil/hash.go @@ -2,15 +2,15 @@ package strutil import ( "crypto/hmac" - "crypto/md5" "crypto/sha256" "encoding/hex" - "fmt" + + "github.com/gookit/goutil/byteutil" ) // Md5 Generate a 32-bit md5 string func Md5(src any) string { - return hex.EncodeToString(Md5Bytes(src)) + return string(Md5Bytes(src)) } // MD5 Generate a 32-bit md5 string @@ -21,17 +21,12 @@ func GenMd5(src any) string { return Md5(src) } // Md5Bytes Generate a 32-bit md5 bytes func Md5Bytes(src any) []byte { - h := md5.New() - - switch val := src.(type) { - case []byte: - h.Write(val) - case string: - h.Write([]byte(val)) - default: - h.Write([]byte(fmt.Sprint(src))) - } - return h.Sum(nil) + return byteutil.Md5(src) +} + +// ShortMd5 Generate a 16-bit md5 string. remove first 8 and last 8 bytes from 32-bit md5 string. +func ShortMd5(src any) string { + return string(byteutil.ShortMd5(src)) } // HashPasswd for quick hash an input password string diff --git a/strutil/crypto_test.go b/strutil/hash_test.go similarity index 81% rename from strutil/crypto_test.go rename to strutil/hash_test.go index 947e00c84..913ee36a6 100644 --- a/strutil/crypto_test.go +++ b/strutil/hash_test.go @@ -14,9 +14,13 @@ func TestMd5(t *testing.T) { assert.Eq(t, "e10adc3949ba59abbe56e057f20f883e", strutil.MD5([]byte("123456"))) assert.Eq(t, "a906449d5769fa7361d7ecc6aa3f6d28", strutil.GenMd5("123abc")) assert.Eq(t, "289dff07669d7a23de0ef88d2f7129e7", strutil.GenMd5(234)) + + // short md5 + assert.Eq(t, "ac59075b964b0715", strutil.ShortMd5(123)) + assert.Eq(t, "3cd24fb0d6963f7d", strutil.ShortMd5("abc")) } -func TestEncryptPasswd(t *testing.T) { +func TestHashPasswd(t *testing.T) { key := "ot54c" pwd := "abc123456"