package hmacx import ( "testing" ) func TestHMACSHA256(t *testing.T) { message := "hello world" key := "secret_key" // 计算HMAC-SHA256 hmac1 := HMACSHA256(message, key) // 再次计算相同的消息和密钥 hmac2 := HMACSHA256(message, key) // 相同的消息和密钥应该产生相同的HMAC if hmac1 != hmac2 { t.Errorf("相同输入产生了不同的HMAC: %s vs %s", hmac1, hmac2) } // 不同的消息应该产生不同的HMAC hmac3 := HMACSHA256("different message", key) if hmac1 == hmac3 { t.Errorf("不同消息产生了相同的HMAC") } // 不同的密钥应该产生不同的HMAC hmac4 := HMACSHA256(message, "different_key") if hmac1 == hmac4 { t.Errorf("不同密钥产生了相同的HMAC") } } func TestHMACSHA512(t *testing.T) { message := "hello world" key := "secret_key" // 计算HMAC-SHA512 hmac1 := HMACSHA512(message, key) // 再次计算相同的消息和密钥 hmac2 := HMACSHA512(message, key) // 相同的消息和密钥应该产生相同的HMAC if hmac1 != hmac2 { t.Errorf("相同输入产生了不同的HMAC: %s vs %s", hmac1, hmac2) } // 不同的消息应该产生不同的HMAC hmac3 := HMACSHA512("different message", key) if hmac1 == hmac3 { t.Errorf("不同消息产生了相同的HMAC") } // 不同的密钥应该产生不同的HMAC hmac4 := HMACSHA512(message, "different_key") if hmac1 == hmac4 { t.Errorf("不同密钥产生了相同的HMAC") } } func TestHMACSHA256VsSHA512(t *testing.T) { message := "hello world" key := "secret_key" // 计算HMAC-SHA256和HMAC-SHA512 hmac256 := HMACSHA256(message, key) hmac512 := HMACSHA512(message, key) // SHA256和SHA512应该产生不同的HMAC if hmac256 == hmac512 { t.Errorf("SHA256和SHA512产生了相同的HMAC") } }