From 74bc2530997881309c6577fa68353742597a76b7 Mon Sep 17 00:00:00 2001 From: zhanglin33 Date: Mon, 3 Jun 2019 11:34:53 +0800 Subject: [PATCH] replace .getBytes() with BytesUtils.toBytes(), again --- .../jd/blockchain/crypto/ecvrf/VRFTest.java | 3 +- .../blockchain/crypto/mpc/MultiSumTest.java | 7 +- .../crypto/utils/classic/AESUtilsTest.java | 88 +++++++++---------- .../utils/classic/RIPEMD160UtilsTest.java | 5 +- .../crypto/CryptoKeySerializationTest.java | 4 +- .../blockchain/crypto/utils/SM2UtilsTest.java | 9 +- .../blockchain/crypto/utils/SM3UtilsTest.java | 9 +- 7 files changed, 64 insertions(+), 61 deletions(-) diff --git a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/ecvrf/VRFTest.java b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/ecvrf/VRFTest.java index 2ee66422..426c3cf4 100644 --- a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/ecvrf/VRFTest.java +++ b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/ecvrf/VRFTest.java @@ -2,6 +2,7 @@ package test.com.jd.blockchain.crypto.ecvrf; import com.jd.blockchain.crypto.CryptoException; import com.jd.blockchain.crypto.ecvrf.VRF; +import com.jd.blockchain.utils.io.BytesUtils; import org.junit.Test; import static org.junit.Assert.assertNotNull; @@ -13,7 +14,7 @@ public class VRFTest { @Test public void testVRF() { - byte[] msg = "你好".getBytes(); + byte[] msg = BytesUtils.toBytes("你好"); //初始化一个异常 Exception actualEx = null; diff --git a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java index 6c04a74a..5b5e8128 100644 --- a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java +++ b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java @@ -4,6 +4,7 @@ import com.jd.blockchain.crypto.mpc.MultiSum; import com.jd.blockchain.crypto.paillier.PaillierPrivateKeyParameters; import com.jd.blockchain.crypto.paillier.PaillierPublicKeyParameters; import com.jd.blockchain.crypto.paillier.PaillierUtils; +import com.jd.blockchain.utils.io.BytesUtils; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.junit.Test; @@ -26,9 +27,9 @@ public class MultiSumTest { int int3 = 600; int sum = 666; - byte[] id1 = "1".getBytes(); - byte[] id2 = "2".getBytes(); - byte[] id3 = "3".getBytes(); + byte[] id1 = BytesUtils.toBytes("1"); + byte[] id2 = BytesUtils.toBytes("2"); + byte[] id3 = BytesUtils.toBytes("3"); MultiSum.generateEphemeralKeyPair(); byte[] ePubKey1 = MultiSum.getEPubKey(); diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/AESUtilsTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/AESUtilsTest.java index 145229bd..78f36df5 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/AESUtilsTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/AESUtilsTest.java @@ -22,7 +22,7 @@ public class AESUtilsTest { public void generateKeyTest(){ byte[] key = AESUtils.generateKey(); assertEquals(16,key.length); - key = AESUtils.generateKey("abc".getBytes()); + key = AESUtils.generateKey(BytesUtils.toBytes("abc")); assertArrayEquals( Hex.decode("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".substring(0,32)),key); } @@ -32,12 +32,12 @@ public class AESUtilsTest { public void encryptTest(){ String plaintext = "abc"; String key = "1234567890123456"; - System.out.println(key.getBytes().length); String iv = "1234567890123456"; String expectedCiphertextIn2ndBlock = "f479efae2d41d23227f61e675fced95c"; - byte[] ciphertext = AESUtils.encrypt(plaintext.getBytes(),key.getBytes(),iv.getBytes()); - byte[] expectedCiphertext = BytesUtils.concat(iv.getBytes(),Hex.decode(expectedCiphertextIn2ndBlock)); + byte[] ciphertext = AESUtils.encrypt(BytesUtils.toBytes(plaintext), + BytesUtils.toBytes(key), BytesUtils.toBytes(iv)); + byte[] expectedCiphertext = BytesUtils.concat(BytesUtils.toBytes(iv),Hex.decode(expectedCiphertextIn2ndBlock)); assertArrayEquals(expectedCiphertext,ciphertext); } @@ -56,45 +56,43 @@ public class AESUtilsTest { assertArrayEquals(data,plaintext); } -// -// -// @Test -// public void encryptingPerformance() { -// -// byte[] data = new byte[1000]; -// Random random = new Random(); -// random.nextBytes(data); -// -// byte[] aesCiphertext = null; -// -// int count = 100000; -// -// -// byte[] aesKey = AESUtils.generateKey(); -// -// System.out.println("=================== do AES encrypt test ==================="); -// for (int r = 0; r < 5; r++) { -// System.out.println("------------- round[" + r + "] --------------"); -// long startTS = System.currentTimeMillis(); -// for (int i = 0; i < count; i++) { -// aesCiphertext = AESUtils.encrypt(data, aesKey); -// } -// long elapsedTS = System.currentTimeMillis() - startTS; -// System.out.println(String.format("AES Encrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, -// (count * 1000.00D) / elapsedTS)); -// } -// -// -// System.out.println("=================== do AES decrypt test ==================="); -// for (int r = 0; r < 5; r++) { -// System.out.println("------------- round[" + r + "] --------------"); -// long startTS = System.currentTimeMillis(); -// for (int i = 0; i < count; i++) { -// AESUtils.decrypt(aesCiphertext, aesKey); -// } -// long elapsedTS = System.currentTimeMillis() - startTS; -// System.out.println(String.format("AES Decrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, -// (count * 1000.00D) / elapsedTS)); -// } -// } + + + public void encryptingPerformance() { + + byte[] data = new byte[1000]; + Random random = new Random(); + random.nextBytes(data); + + byte[] aesCiphertext = null; + + int count = 100000; + + + byte[] aesKey = AESUtils.generateKey(); + + System.out.println("=================== do AES encrypt test ==================="); + for (int r = 0; r < 5; r++) { + System.out.println("------------- round[" + r + "] --------------"); + long startTS = System.currentTimeMillis(); + for (int i = 0; i < count; i++) { + aesCiphertext = AESUtils.encrypt(data, aesKey); + } + long elapsedTS = System.currentTimeMillis() - startTS; + System.out.println(String.format("AES Encrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, + (count * 1000.00D) / elapsedTS)); + } + + System.out.println("=================== do AES decrypt test ==================="); + for (int r = 0; r < 5; r++) { + System.out.println("------------- round[" + r + "] --------------"); + long startTS = System.currentTimeMillis(); + for (int i = 0; i < count; i++) { + AESUtils.decrypt(aesCiphertext, aesKey); + } + long elapsedTS = System.currentTimeMillis() - startTS; + System.out.println(String.format("AES Decrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, + (count * 1000.00D) / elapsedTS)); + } + } } diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RIPEMD160UtilsTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RIPEMD160UtilsTest.java index 28285055..a3baef5d 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RIPEMD160UtilsTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RIPEMD160UtilsTest.java @@ -1,6 +1,7 @@ package test.com.jd.blockchain.crypto.utils.classic; import com.jd.blockchain.crypto.utils.classic.RIPEMD160Utils; +import com.jd.blockchain.utils.io.BytesUtils; import org.bouncycastle.util.encoders.Hex; import org.junit.Test; @@ -17,8 +18,8 @@ public class RIPEMD160UtilsTest { @Test public void hashTest() { - byte[] data1 = "a".getBytes(); - byte[] data2 = "abc".getBytes(); + byte[] data1 = BytesUtils.toBytes("a"); + byte[] data2 = BytesUtils.toBytes("abc"); byte[] result1 = RIPEMD160Utils.hash(data1); byte[] result2 = RIPEMD160Utils.hash(data2); diff --git a/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java b/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java index 9093ecf4..d2d1d314 100644 --- a/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java +++ b/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java @@ -23,7 +23,7 @@ public class CryptoKeySerializationTest { CryptoAlgorithm algorithm = CryptoAlgorithmDefinition.defineSignature("TEST", false, (byte) 123); // Simulate a public key with a random number; - byte[] rawBytes = UUID.randomUUID().toString().getBytes(); + byte[] rawBytes = BytesUtils.toBytes(UUID.randomUUID().toString()); PubKey pubKey = new PubKey(algorithm, rawBytes); @@ -51,7 +51,7 @@ public class CryptoKeySerializationTest { CryptoAlgorithm algorithm = CryptoAlgorithmDefinition.defineSignature("TEST", false, (byte) 123); // Simulate a public key with a random number; - byte[] rawBytes = UUID.randomUUID().toString().getBytes(); + byte[] rawBytes = BytesUtils.toBytes(UUID.randomUUID().toString()); PrivKey privKey = new PrivKey(algorithm, rawBytes); diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java index 04dd5ca5..11d6839a 100644 --- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.jd.blockchain.utils.io.BytesUtils; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; @@ -62,7 +63,7 @@ public class SM2UtilsTest { String expectedS = "cc8d096578f7dd2669ac1ac42f7e722bcfa42b9e0be0b1b5df7ca0b53fdd5750"; byte[] privKeyBytes = Hex.decode(expectedPrivateKey); - byte[] messageBytes = expectedMessage.getBytes(); + byte[] messageBytes = BytesUtils.toBytes(expectedMessage); byte[] signature = SM2Utils.sign(messageBytes,privKeyBytes,new TestRandomBigInteger(expectedRandomness, 16),expectedIdentifier); assertArrayEquals(Hex.decode(expectedR+expectedS),signature); @@ -78,7 +79,7 @@ public class SM2UtilsTest { String expectedS = "cc8d096578f7dd2669ac1ac42f7e722bcfa42b9e0be0b1b5df7ca0b53fdd5750"; byte[] pubKeyBytes = Hex.decode(expectedPublicKey); - byte[] messageBytes = expectedMessage.getBytes(); + byte[] messageBytes = BytesUtils.toBytes(expectedMessage); byte[] signatureBytes = Hex.decode(expectedR + expectedS); boolean isVerified = SM2Utils.verify(messageBytes,pubKeyBytes,signatureBytes,expectedIdentifier); @@ -96,7 +97,7 @@ public class SM2UtilsTest { String expectedC3 = "59983c18f809e262923c53aec295d30383b54e39d609d160afcb1908d0bd8766"; byte[] pubKeyBytes = Hex.decode(expectedPublicKey); - byte[] messageBytes = expectedMessage.getBytes(); + byte[] messageBytes = BytesUtils.toBytes(expectedMessage); byte[] ciphertext = SM2Utils.encrypt(messageBytes,pubKeyBytes,new TestRandomBigInteger(expectedRandomness, 16)); assertArrayEquals(Hex.decode(expectedC1 + expectedC3 + expectedC2),ciphertext); @@ -117,7 +118,7 @@ public class SM2UtilsTest { byte[] ciphertext = Hex.decode(expectedC1 + expectedC3 + expectedC2); byte[] plaintext = SM2Utils.decrypt(ciphertext,privKeyBytes); - assertArrayEquals(expectedMessage.getBytes(),plaintext); + assertArrayEquals(BytesUtils.toBytes(expectedMessage),plaintext); } // @Test diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java index 3a5a5bf2..b79c202e 100644 --- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java @@ -3,6 +3,7 @@ package test.com.jd.blockchain.crypto.utils; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import com.jd.blockchain.utils.io.BytesUtils; import org.bouncycastle.util.encoders.Hex; import org.junit.Test; @@ -20,12 +21,12 @@ public class SM3UtilsTest { String expectedResult1="66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0" ; String expectedResult2="debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732"; - byte[] testString1Bytes = testString1.getBytes(); - byte[] testString2Bytes = testString2.getBytes(); + byte[] testString1Bytes = BytesUtils.toBytes(testString1); + byte[] testString2Bytes = BytesUtils.toBytes(testString2); byte[] hash1 = SM3Utils.hash(testString1Bytes); byte[] hash2 = SM3Utils.hash(testString2Bytes); - byte[] expectedResult1Bytes = expectedResult1.getBytes(); - byte[] expectedResult2Bytes = expectedResult2.getBytes(); + byte[] expectedResult1Bytes = BytesUtils.toBytes(expectedResult1); + byte[] expectedResult2Bytes = BytesUtils.toBytes(expectedResult2); assertEquals(hash1.length, SM3DIGEST_LENGTH); assertEquals(hash2.length, SM3DIGEST_LENGTH); assertArrayEquals(hash1, Hex.decode(expectedResult1Bytes));