Browse Source

providing junit test for hash functions, including RIPEMD160HashFunction, SHA256HashFunction and SM3HashFunction.

tags/1.0.0
zhanglin33 6 years ago
parent
commit
d240ba058f
19 changed files with 586 additions and 39 deletions
  1. +6
    -3
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java
  2. +7
    -5
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java
  3. +1
    -1
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java
  4. +10
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
  5. +10
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java
  6. +173
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java
  7. +173
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java
  8. +1
    -1
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java
  9. +2
    -2
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java
  10. +7
    -4
      source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java
  11. +10
    -0
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java
  12. +171
    -0
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java
  13. +10
    -0
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java
  14. +1
    -9
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java
  15. +1
    -4
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java
  16. +1
    -4
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM4UtilsTest.java
  17. +1
    -1
      source/crypto/pom.xml
  18. +0
    -4
      source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java
  19. +1
    -1
      source/pom.xml

+ 6
- 3
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java View File

@@ -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!");
}
}
}

+ 7
- 5
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java View File

@@ -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!");
}
}

}

+ 1
- 1
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java View File

@@ -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;


+ 10
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java View File

@@ -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 {
}

+ 10
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java View File

@@ -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 {
}

+ 173
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java View File

@@ -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()));
}
}

+ 173
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java View File

@@ -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()));
}
}

+ 1
- 1
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java View File

@@ -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);
}
}

+ 2
- 2
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/hash/HashDigest.java View File

@@ -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


+ 7
- 4
source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java View File

@@ -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!");
}
}
}

+ 10
- 0
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java View File

@@ -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 {
}

+ 171
- 0
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java View File

@@ -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()));
}
}

+ 10
- 0
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java View File

@@ -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 {
}

source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM2UtilsTest.java → source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM2UtilsTest.java View File

@@ -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

source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM3UtilsTest.java → source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM3UtilsTest.java View File

@@ -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;

source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/smutils/SM4UtilsTest.java → source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/utils/SM4UtilsTest.java View File

@@ -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;

+ 1
- 1
source/crypto/pom.xml View File

@@ -14,7 +14,7 @@
<module>crypto-framework</module>
<module>crypto-classic</module>
<module>crypto-sm</module>
<module>crypto-jni-clib</module>
<!-- <module>crypto-jni-clib</module> -->
<module>crypto-adv</module>
</modules>


+ 0
- 4
source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAccountTest.java View File

@@ -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;


+ 1
- 1
source/pom.xml View File

@@ -277,7 +277,7 @@
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
<version>1.61</version>
</dependency>

<dependency>


Loading…
Cancel
Save