Skip to content

Commit

Permalink
✨ feat: str,byte - add new func: ShortMd5 and update byteutil.Md5 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2023
1 parent c8acbff commit c03d243
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
21 changes: 17 additions & 4 deletions byteutil/byteutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package byteutil
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand"
"strconv"
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions byteutil/byteutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 9 additions & 14 deletions strutil/crypto.go → strutil/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion strutil/crypto_test.go → strutil/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit c03d243

Please sign in to comment.