From d240ba058f9e462d73259334970a2c644a56fd6a Mon Sep 17 00:00:00 2001 From: zhanglin33 Date: Wed, 3 Apr 2019 17:07:14 +0800 Subject: [PATCH] providing junit test for hash functions, including RIPEMD160HashFunction, SHA256HashFunction and SM3HashFunction. --- .../classic/RIPEMD160HashFunction.java | 9 +- .../service/classic/SHA256HashFunction.java | 12 +- .../crypto/hash/HashCryptographyImplTest.java | 2 +- .../classic/AESEncryptionFunctionTest.java | 10 + .../classic/ED25519SignatureFunctionTest.java | 10 + .../classic/RIPEMD160HashFunctionTest.java | 173 ++++++++++++++++++ .../classic/SHA256HashFunctionTest.java | 173 ++++++++++++++++++ .../jd/blockchain/crypto/BaseCryptoBytes.java | 2 +- .../jd/blockchain/crypto/hash/HashDigest.java | 4 +- .../crypto/service/sm/SM3HashFunction.java | 11 +- .../service/sm/SM2CyptoFunctionTest.java | 10 + .../service/sm/SM3HashFunctionTest.java | 171 +++++++++++++++++ .../service/sm/SM4EncryptionFunctionTest.java | 10 + .../{smutils => utils}/SM2UtilsTest.java | 10 +- .../{smutils => utils}/SM3UtilsTest.java | 5 +- .../{smutils => utils}/SM4UtilsTest.java | 5 +- source/crypto/pom.xml | 2 +- .../blockchain/ledger/LedgerAccountTest.java | 4 - source/pom.xml | 2 +- 19 files changed, 586 insertions(+), 39 deletions(-) create mode 100644 source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java create mode 100644 source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java create mode 100644 source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java create mode 100644 source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java create mode 100644 source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java create mode 100644 source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java create mode 100644 source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java rename source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/{smutils => utils}/SM2UtilsTest.java (97%) rename source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/{smutils => utils}/SM3UtilsTest.java (95%) rename source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/{smutils => utils}/SM4UtilsTest.java (97%) diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java index f7ffba03..69b64e13 100644 --- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java +++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java @@ -30,7 +30,7 @@ public class RIPEMD160HashFunction implements HashFunction { public HashDigest hash(byte[] data) { if (data == null) { - throw new CryptoException("The input is null!"); + throw new CryptoException("data is null!"); } byte[] digestBytes = RipeMD160Utils.hash(data); @@ -51,7 +51,10 @@ public class RIPEMD160HashFunction implements HashFunction { @Override public HashDigest resolveHashDigest(byte[] digestBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new HashDigest(digestBytes); + if (supportHashDigest(digestBytes)) { + return new HashDigest(digestBytes); + } else { + throw new CryptoException("digestBytes is invalid!"); + } } } diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java index e210bd27..62d4c887 100644 --- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java +++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java @@ -30,7 +30,7 @@ public class SHA256HashFunction implements HashFunction { public HashDigest hash(byte[] data) { if (data == null) { - throw new CryptoException("The input is null!"); + throw new CryptoException("data is null!"); } byte[] digestBytes = ShaUtils.hash_256(data); @@ -50,9 +50,11 @@ public class SHA256HashFunction implements HashFunction { } @Override - public HashDigest resolveHashDigest(byte[] hashDigestBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new HashDigest(hashDigestBytes); + public HashDigest resolveHashDigest(byte[] digestBytes) { + if (supportHashDigest(digestBytes)) { + return new HashDigest(digestBytes); + } else { + throw new CryptoException("digestBytes is invalid!"); + } } - } diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java index cf7ec9b3..062d0cd9 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java @@ -4,7 +4,7 @@ // //import java.util.Random; // -//import com.jd.blockchain.crypto.smutils.hash.SM3Utils; +//import com.jd.blockchain.crypto.utils.hash.SM3Utils; //import com.jd.blockchain.utils.io.BytesUtils; //import com.jd.blockchain.utils.security.RipeMD160Utils; //import com.jd.blockchain.utils.security.ShaUtils; diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java new file mode 100644 index 00000000..fd52129c --- /dev/null +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java @@ -0,0 +1,10 @@ +package test.com.jd.blockchain.crypto.service.classic; + +/** + * @author zhanglin33 + * @title: AESEncryptionFunctionTest + * @description: JunitTest for AESAESEncryptionFunction in SPI mode + * @date 2019-04-01, 13:57 + */ +public class AESEncryptionFunctionTest { +} diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java new file mode 100644 index 00000000..f16bcd52 --- /dev/null +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java @@ -0,0 +1,10 @@ +package test.com.jd.blockchain.crypto.service.classic; + +/** + * @author zhanglin33 + * @title: ED25519SignatureFunctionTest + * @description: JunitTest for ED25519SignatureFunction in SPI mode + * @date 2019-04-01, 14:01 + */ +public class ED25519SignatureFunctionTest { +} diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java new file mode 100644 index 00000000..d33870b8 --- /dev/null +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java @@ -0,0 +1,173 @@ +package test.com.jd.blockchain.crypto.service.classic; + +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoException; +import com.jd.blockchain.crypto.CryptoServiceProviders; +import com.jd.blockchain.crypto.hash.HashDigest; +import com.jd.blockchain.crypto.hash.HashFunction; +import com.jd.blockchain.utils.io.BytesUtils; +import org.junit.Test; + +import java.util.Random; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +/** + * @author zhanglin33 + * @title: RIPEMD160HashFunctionTest + * @description: JunitTest for RIPEMD160HashFunction in SPI mode + * @date 2019-04-01, 14:03 + */ +public class RIPEMD160HashFunctionTest { + + @Test + public void getAlgorithmTest(){ + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("RIPEmd160"); + assertNotNull(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD-160"); + assertNull(algorithm); + } + + @Test + public void hashTest(){ + + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + + byte[] digestBytes = digest.toBytes(); + assertEquals(160 / 8 + 2,digestBytes.length); + assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes)); + + assertEquals(algorithm.code(),digest.getAlgorithm().code()); + assertEquals(algorithm.name(),digest.getAlgorithm().name()); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + data = null; + hashFunction.hash(data); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } + + @Test + public void verifyTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + assertTrue(hashFunction.verify(digest,data)); + } + + @Test + public void supportHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + assertTrue(hashFunction.supportHashDigest(digestBytes)); + + algorithm = CryptoServiceProviders.getAlgorithm("aes"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length); + assertFalse(hashFunction.supportHashDigest(digestBytes)); + } + + @Test + public void resolveHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + + HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); + + assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); + assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); + assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); + assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); + + + algorithm = CryptoServiceProviders.getAlgorithm("aes"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + hashFunction.resolveHashDigest(aesDigestBytes); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + + algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + algoBytes = CryptoAlgorithm.toBytes(algorithm); + rawDigestBytes = digest.getRawDigest(); + byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); + + expectedException = CryptoException.class; + actualEx = null; + try { + hashFunction.resolveHashDigest(ripemd160DigestBytes); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } +} diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java new file mode 100644 index 00000000..7bbe3ff5 --- /dev/null +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java @@ -0,0 +1,173 @@ +package test.com.jd.blockchain.crypto.service.classic; + +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoException; +import com.jd.blockchain.crypto.CryptoServiceProviders; +import com.jd.blockchain.crypto.hash.HashDigest; +import com.jd.blockchain.crypto.hash.HashFunction; +import com.jd.blockchain.utils.io.BytesUtils; +import org.junit.Test; + +import java.util.Random; + +import static org.junit.Assert.*; + +/** + * @author zhanglin33 + * @title: SHA256HashFunctionTest + * @description: JunitTest for SHA256HashFunction in SPI mode + * @date 2019-04-01, 14:21 + */ + +public class SHA256HashFunctionTest { + + @Test + public void getAlgorithmTest(){ + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("SHa256"); + assertNotNull(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("sha-256"); + assertNull(algorithm); + } + + @Test + public void hashTest(){ + + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + + byte[] digestBytes = digest.toBytes(); + assertEquals(256 / 8 + 2,digestBytes.length); + assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes)); + + assertEquals(algorithm.code(),digest.getAlgorithm().code()); + assertEquals(algorithm.name(),digest.getAlgorithm().name()); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + data = null; + hashFunction.hash(data); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } + + @Test + public void verifyTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + assertTrue(hashFunction.verify(digest,data)); + } + + @Test + public void supportHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + assertTrue(hashFunction.supportHashDigest(digestBytes)); + + algorithm = CryptoServiceProviders.getAlgorithm("aes"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length); + assertFalse(hashFunction.supportHashDigest(digestBytes)); + } + + @Test + public void resolveHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + + HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); + + assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); + assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); + assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); + assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); + + + algorithm = CryptoServiceProviders.getAlgorithm("aes"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + hashFunction.resolveHashDigest(aesDigestBytes); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + + algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); + assertNotNull(algorithm); + algoBytes = CryptoAlgorithm.toBytes(algorithm); + rawDigestBytes = digest.getRawDigest(); + byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); + + expectedException = CryptoException.class; + actualEx = null; + try { + hashFunction.resolveHashDigest(ripemd160DigestBytes); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } +} diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java index 6ddfc545..e6bd3d3e 100644 --- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java @@ -55,6 +55,6 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { protected BytesSlice getRawCryptoBytes() { // return resolveRawCryptoBytes(encodedBytes); - return new BytesSlice(getDirectBytes(), 1); + return new BytesSlice(getDirectBytes(), 2); } } diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java index 99cf37bb..8efcd9a8 100644 --- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java @@ -18,8 +18,8 @@ public class HashDigest extends BaseCryptoBytes implements CryptoDigest,Serializ super(); } - public HashDigest(byte[] encodedDigestBytes) { - super(encodedDigestBytes); + public HashDigest(byte[] cryptoBytes) { + super(cryptoBytes); } @Override diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java index b79a0f07..0eb81ef6 100644 --- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java +++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java @@ -29,7 +29,7 @@ public class SM3HashFunction implements HashFunction { public HashDigest hash(byte[] data) { if (data == null) { - throw new CryptoException("The input is null!"); + throw new CryptoException("data is null!"); } byte[] digestBytes = SM3Utils.hash(data); @@ -49,8 +49,11 @@ public class SM3HashFunction implements HashFunction { } @Override - public HashDigest resolveHashDigest(byte[] hashDigestBytes) { - // 由框架调用 support 方法检查有效性,在此不做重复检查; - return new HashDigest(hashDigestBytes); + public HashDigest resolveHashDigest(byte[] digestBytes) { + if (supportHashDigest(digestBytes)) { + return new HashDigest(digestBytes); + } else { + throw new CryptoException("digestBytes is invalid!"); + } } } diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java new file mode 100644 index 00000000..8e52ec27 --- /dev/null +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java @@ -0,0 +1,10 @@ +package test.com.jd.blockchain.crypto.service.sm; + +/** + * @author zhanglin33 + * @title: SM2CyptoFunctionTest + * @description: JunitTest for SM2CyptoFunction in SPI mode + * @date 2019-04-03, 16:32 + */ +public class SM2CyptoFunctionTest { +} diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java new file mode 100644 index 00000000..1aea2610 --- /dev/null +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java @@ -0,0 +1,171 @@ +package test.com.jd.blockchain.crypto.service.sm; + +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoException; +import com.jd.blockchain.crypto.CryptoServiceProviders; +import com.jd.blockchain.crypto.hash.HashDigest; +import com.jd.blockchain.crypto.hash.HashFunction; +import com.jd.blockchain.utils.io.BytesUtils; +import org.junit.Test; + +import java.util.Random; + +import static org.junit.Assert.*; + +/** + * @author zhanglin33 + * @title: SM3HashFunctionTest + * @description: JunitTest for SM3HashFunction in SPI mode + * @date 2019-04-03, 16:33 + */ +public class SM3HashFunctionTest { + @Test + public void getAlgorithmTest(){ + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("Sm3"); + assertNotNull(algorithm); + + assertEquals(hashFunction.getAlgorithm().name(), algorithm.name()); + assertEquals(hashFunction.getAlgorithm().code(), algorithm.code()); + + algorithm = CryptoServiceProviders.getAlgorithm("sm3333"); + assertNull(algorithm); + } + + @Test + public void hashTest(){ + + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + + byte[] digestBytes = digest.toBytes(); + assertEquals(256 / 8 + 2,digestBytes.length); + assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes)); + + assertEquals(algorithm.code(),digest.getAlgorithm().code()); + assertEquals(algorithm.name(),digest.getAlgorithm().name()); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + data = null; + hashFunction.hash(data); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } + + @Test + public void verifyTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + assertTrue(hashFunction.verify(digest,data)); + } + + @Test + public void supportHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + assertTrue(hashFunction.supportHashDigest(digestBytes)); + + algorithm = CryptoServiceProviders.getAlgorithm("sm4"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length); + assertFalse(hashFunction.supportHashDigest(digestBytes)); + } + + @Test + public void resolveHashDigestTest(){ + byte[] data = new byte[1024]; + Random random = new Random(); + random.nextBytes(data); + + CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3"); + assertNotNull(algorithm); + + HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm); + + HashDigest digest = hashFunction.hash(data); + + byte[] digestBytes = digest.toBytes(); + + HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); + + assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); + assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); + assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); + assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); + + + algorithm = CryptoServiceProviders.getAlgorithm("sm4"); + assertNotNull(algorithm); + byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); + byte[] rawDigestBytes = digest.getRawDigest(); + byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); + + Class expectedException = CryptoException.class; + Exception actualEx = null; + try { + hashFunction.resolveHashDigest(aesDigestBytes); + } catch (Exception e) { + actualEx = e; + } + assertNotNull(actualEx); + assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); +// +// algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); +// assertNotNull(algorithm); +// algoBytes = CryptoAlgorithm.toBytes(algorithm); +// rawDigestBytes = digest.getRawDigest(); +// byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); +// +// expectedException = CryptoException.class; +// actualEx = null; +// try { +// hashFunction.resolveHashDigest(ripemd160DigestBytes); +// } catch (Exception e) { +// actualEx = e; +// } +// assertNotNull(actualEx); +// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); + } +} diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java new file mode 100644 index 00000000..4b5e549a --- /dev/null +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java @@ -0,0 +1,10 @@ +package test.com.jd.blockchain.crypto.service.sm; + +/** + * @author zhanglin33 + * @title: SM4EncryptionFunctionTest + * @description: JunitTest for SM4EncryptionFunction in SPI mode + * @date 2019-04-03, 16:35 + */ +public class SM4EncryptionFunctionTest { +} diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM2UtilsTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java similarity index 97% rename from source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM2UtilsTest.java rename to source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java index dec8f2f3..a91faddf 100644 --- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM2UtilsTest.java +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java @@ -1,13 +1,9 @@ -package test.com.jd.blockchain.crypto.smutils; +package test.com.jd.blockchain.crypto.utils; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.jd.blockchain.utils.security.Ed25519Utils; -import net.i2p.crypto.eddsa.EdDSAPrivateKey; -import net.i2p.crypto.eddsa.EdDSAPublicKey; -import net.i2p.crypto.eddsa.KeyPairGenerator; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; @@ -17,10 +13,6 @@ import org.junit.Test; import com.jd.blockchain.crypto.utils.sm.SM2Utils; -import java.security.KeyPair; -import java.util.Arrays; -import java.util.Random; - public class SM2UtilsTest { @Test diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM3UtilsTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java similarity index 95% rename from source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM3UtilsTest.java rename to source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java index 8b155edf..3a5a5bf2 100644 --- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM3UtilsTest.java +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java @@ -1,16 +1,13 @@ -package test.com.jd.blockchain.crypto.smutils; +package test.com.jd.blockchain.crypto.utils; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import com.jd.blockchain.utils.security.ShaUtils; import org.bouncycastle.util.encoders.Hex; import org.junit.Test; import com.jd.blockchain.crypto.utils.sm.SM3Utils; -import java.util.Random; - public class SM3UtilsTest { private static final int SM3DIGEST_LENGTH = 32; diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM4UtilsTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM4UtilsTest.java similarity index 97% rename from source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM4UtilsTest.java rename to source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM4UtilsTest.java index d37238c6..dd379b47 100644 --- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM4UtilsTest.java +++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM4UtilsTest.java @@ -1,16 +1,13 @@ -package test.com.jd.blockchain.crypto.smutils; +package test.com.jd.blockchain.crypto.utils; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import com.jd.blockchain.utils.security.AESUtils; import org.bouncycastle.util.encoders.Hex; import org.junit.Test; import com.jd.blockchain.crypto.utils.sm.SM4Utils; -import java.util.Random; - public class SM4UtilsTest { private static final int KEY_SIZE = 16; diff --git a/source/crypto/pom.xml b/source/crypto/pom.xml index d0ee654b..fb6e71ba 100644 --- a/source/crypto/pom.xml +++ b/source/crypto/pom.xml @@ -14,7 +14,7 @@ crypto-framework crypto-classic crypto-sm - crypto-jni-clib + crypto-adv diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java index aac4dd23..ab8ca77e 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java @@ -2,17 +2,13 @@ package test.com.jd.blockchain.ledger; import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.DataContractRegistry; -import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.AccountHeader; -import com.jd.blockchain.ledger.ParticipantNode; import com.jd.blockchain.ledger.UserInfo; import com.jd.blockchain.ledger.core.AccountSet; -import com.jd.blockchain.ledger.core.LedgerAdminAccount; -import com.jd.blockchain.ledger.core.LedgerMetadata; import com.jd.blockchain.utils.Bytes; import org.junit.Before; diff --git a/source/pom.xml b/source/pom.xml index 37592211..2f1a1d74 100644 --- a/source/pom.xml +++ b/source/pom.xml @@ -277,7 +277,7 @@ org.bouncycastle bcprov-jdk15on - 1.59 + 1.61