From c95cd73c8029b37c6fe38d604c783bfb1b0b0a32 Mon Sep 17 00:00:00 2001 From: deatil <2217957370@qq.com> Date: Tue, 10 Dec 2024 17:50:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash/hmac.go | 5 ++++- hash/hmac_test.go | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hash/hmac.go b/hash/hmac.go index 8667c29..5a46bce 100644 --- a/hash/hmac.go +++ b/hash/hmac.go @@ -14,6 +14,7 @@ import ( "golang.org/x/crypto/sha3" "golang.org/x/crypto/ripemd160" "github.com/deatil/go-hash/md2" + "github.com/deatil/go-hash/sm3" ) var ( @@ -39,9 +40,10 @@ func init() { RegisterHmacHash(HmacSHA3_384, sha3.New384) RegisterHmacHash(HmacSHA3_512, sha3.New512) RegisterHmacHash(HmacRIPEMD160, ripemd160.New) + RegisterHmacHash(HmacSM3, sm3.New) } -// hmac 用到的 hash +// hmac hash type type HmacHash uint const ( @@ -61,6 +63,7 @@ const ( HmacSHA3_384 HmacSHA3_512 HmacRIPEMD160 + HmacSM3 ) var hmacHashes = make(map[HmacHash]func() hash.Hash) diff --git a/hash/hmac_test.go b/hash/hmac_test.go index 617874a..6e0f606 100644 --- a/hash/hmac_test.go +++ b/hash/hmac_test.go @@ -20,6 +20,7 @@ var hmacTests = []struct { {"SHA512", HmacSHA512, "sdf,gsdgfsdfg123156", "pass123", "aaf38f79d73a28a02c8ae26635e90a5b716f84d3365a1aa4333f86a99d8a85774ec3eb3c1e4d8e05df393ae7c60dd024feacafbd3564395fee52874902e54a93"}, {"RIPEMD160", HmacRIPEMD160, "sdf,gsdgfsdfg123156", "pass123", "a6fde50583758876022d48f90db93bcfedfe9615"}, {"SHA3_256", HmacSHA3_256, "sdffgsdgfsdfg123156rt5", "pass123", "6d9504b686faeeac4eb66f96603c55f4033181da671644357b7f40cd838311ac"}, + {"HmacSM3", HmacSM3, "sdffgsdgfsdfg123156rt5", "pass123", "997f778d1ecbd92845c0061b4d1d658ef84170bb57efbabc7cd4ef5c953f8446"}, } func Test_Hmac(t *testing.T) { @@ -31,7 +32,7 @@ func Test_Hmac(t *testing.T) { t.Run(fmt.Sprintf("Hmac_test_%d", index), func(t *testing.T) { assertError(e.Error, "Hmac-" + test.name) - assert(test.output, e.ToHexString(), "Hmac-" + test.name) + assert(e.ToHexString(), test.output, "Hmac-" + test.name) }) } } @@ -46,7 +47,7 @@ func Test_NewHmac(t *testing.T) { t.Run(fmt.Sprintf("NewHmac_test_%d", index), func(t *testing.T) { assertError(e.Error, "NewHmac" + test.name) - assert(test.output, e.ToHexString(), "NewHmac" + test.name) + assert(e.ToHexString(), test.output, "NewHmac" + test.name) }) } }