Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👔 up: str - update bytes util and add new hash utils
- HashPasswd - VerifyPasswd
- Loading branch information
Showing
5 changed files
with
85 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package strutil | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/md5" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
// Md5 Generate a 32-bit md5 string | ||
func Md5(src any) string { | ||
return hex.EncodeToString(Md5Bytes(src)) | ||
} | ||
|
||
// MD5 Generate a 32-bit md5 string | ||
func MD5(src any) string { return Md5(src) } | ||
|
||
// GenMd5 Generate a 32-bit md5 string | ||
func GenMd5(src any) string { return Md5(src) } | ||
|
||
// Md5Bytes Generate a 32-bit md5 bytes | ||
func Md5Bytes(src any) []byte { | ||
h := md5.New() | ||
if s, ok := src.(string); ok { | ||
h.Write([]byte(s)) | ||
} else { | ||
h.Write([]byte(fmt.Sprint(src))) | ||
} | ||
return h.Sum(nil) | ||
} | ||
|
||
// HashPasswd for quick hash an input password string | ||
func HashPasswd(pwd, key string) string { | ||
hm := hmac.New(sha256.New, []byte(key)) | ||
hm.Write([]byte(pwd)) | ||
|
||
return hex.EncodeToString(hm.Sum(nil)) | ||
} | ||
|
||
// VerifyPasswd for quick verify input password is valid | ||
// | ||
// - pwdMAC from db or config, generated by EncryptPasswd() | ||
func VerifyPasswd(pwdMAC, pwd, key string) bool { | ||
decBts, err := hex.DecodeString(pwdMAC) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
hm := hmac.New(sha256.New, []byte(key)) | ||
hm.Write([]byte(pwd)) | ||
|
||
return hmac.Equal(decBts, hm.Sum(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package strutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/dump" | ||
"github.com/gookit/goutil/strutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestMd5(t *testing.T) { | ||
assert.Eq(t, "e10adc3949ba59abbe56e057f20f883e", strutil.Md5("123456")) | ||
assert.Eq(t, "e10adc3949ba59abbe56e057f20f883e", strutil.MD5("123456")) | ||
assert.Eq(t, "a906449d5769fa7361d7ecc6aa3f6d28", strutil.GenMd5("123abc")) | ||
assert.Eq(t, "289dff07669d7a23de0ef88d2f7129e7", strutil.GenMd5(234)) | ||
} | ||
|
||
func TestEncryptPasswd(t *testing.T) { | ||
key := "ot54c" | ||
pwd := "abc123456" | ||
|
||
msgMac := strutil.HashPasswd(pwd, key) | ||
dump.P(msgMac) | ||
assert.NotEmpty(t, msgMac) | ||
assert.True(t, strutil.VerifyPasswd(msgMac, pwd, key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters