diff --git a/sm4/sm4.go b/sm4/sm4.go index 0e301deb..83a8b576 100644 --- a/sm4/sm4.go +++ b/sm4/sm4.go @@ -355,17 +355,11 @@ func Sm4Ecb(key []byte, in []byte, mode bool) (out []byte, err error) { } if mode { for i := 0; i < len(inData)/16; i++ { - in_tmp := inData[i*16 : i*16+16] - out_tmp := make([]byte, 16) - c.Encrypt(out_tmp, in_tmp) - copy(out[i*16:i*16+16], out_tmp) + c.Encrypt(out[i*16:i*16+16], inData[i*16:i*16+16]) } } else { for i := 0; i < len(inData)/16; i++ { - in_tmp := inData[i*16 : i*16+16] - out_tmp := make([]byte, 16) - c.Decrypt(out_tmp, in_tmp) - copy(out[i*16:i*16+16], out_tmp) + c.Decrypt(out[i*16:i*16+16], inData[i*16:i*16+16]) } out, _ = pkcs7UnPadding(out) } diff --git a/sm4/sm4_test.go b/sm4/sm4_test.go index 6115ba14..e3b7b7c5 100644 --- a/sm4/sm4_test.go +++ b/sm4/sm4_test.go @@ -16,6 +16,7 @@ limitations under the License. package sm4 import ( + "bytes" "fmt" "reflect" "testing" @@ -151,3 +152,27 @@ func testCompare(key1, key2 []byte) bool { } return true } + +func BenchmarkSm4EcbEncode(b *testing.B) { + key := []byte("0123456789abcdef") + data := bytes.Repeat([]byte("0123456789abcdef"), 10) + + for i := 0; i < b.N; i++ { + _, err := Sm4Ecb(key, data, true) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkSm4EcbDecode(b *testing.B) { + key := []byte("0123456789abcdef") + data := []byte("13278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b8613278c63b7305e3c131cdbbbe6f59b86d14d7078d2f3a67fe00d3221dbb1d556") + + for i := 0; i < b.N; i++ { + _, err := Sm4Ecb(key, data, false) + if err != nil { + b.Fatal(err) + } + } +}