@@ -30,7 +30,7 @@ public class RIPEMD160HashFunction implements HashFunction { | |||||
public HashDigest hash(byte[] data) { | public HashDigest hash(byte[] data) { | ||||
if (data == null) { | if (data == null) { | ||||
throw new CryptoException("The input is null!"); | |||||
throw new CryptoException("data is null!"); | |||||
} | } | ||||
byte[] digestBytes = RipeMD160Utils.hash(data); | byte[] digestBytes = RipeMD160Utils.hash(data); | ||||
@@ -51,7 +51,10 @@ public class RIPEMD160HashFunction implements HashFunction { | |||||
@Override | @Override | ||||
public HashDigest resolveHashDigest(byte[] digestBytes) { | public HashDigest resolveHashDigest(byte[] digestBytes) { | ||||
// 由框架调用 support 方法检查有效性,在此不做重复检查; | |||||
return new HashDigest(digestBytes); | |||||
if (supportHashDigest(digestBytes)) { | |||||
return new HashDigest(digestBytes); | |||||
} else { | |||||
throw new CryptoException("digestBytes is invalid!"); | |||||
} | |||||
} | } | ||||
} | } |
@@ -30,7 +30,7 @@ public class SHA256HashFunction implements HashFunction { | |||||
public HashDigest hash(byte[] data) { | public HashDigest hash(byte[] data) { | ||||
if (data == null) { | if (data == null) { | ||||
throw new CryptoException("The input is null!"); | |||||
throw new CryptoException("data is null!"); | |||||
} | } | ||||
byte[] digestBytes = ShaUtils.hash_256(data); | byte[] digestBytes = ShaUtils.hash_256(data); | ||||
@@ -50,9 +50,11 @@ public class SHA256HashFunction implements HashFunction { | |||||
} | } | ||||
@Override | @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!"); | |||||
} | |||||
} | } | ||||
} | } |
@@ -4,7 +4,7 @@ | |||||
// | // | ||||
//import java.util.Random; | //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.io.BytesUtils; | ||||
//import com.jd.blockchain.utils.security.RipeMD160Utils; | //import com.jd.blockchain.utils.security.RipeMD160Utils; | ||||
//import com.jd.blockchain.utils.security.ShaUtils; | //import com.jd.blockchain.utils.security.ShaUtils; | ||||
@@ -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 { | |||||
} |
@@ -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 { | |||||
} |
@@ -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())); | |||||
} | |||||
} |
@@ -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())); | |||||
} | |||||
} |
@@ -55,6 +55,6 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { | |||||
protected BytesSlice getRawCryptoBytes() { | protected BytesSlice getRawCryptoBytes() { | ||||
// return resolveRawCryptoBytes(encodedBytes); | // return resolveRawCryptoBytes(encodedBytes); | ||||
return new BytesSlice(getDirectBytes(), 1); | |||||
return new BytesSlice(getDirectBytes(), 2); | |||||
} | } | ||||
} | } |
@@ -18,8 +18,8 @@ public class HashDigest extends BaseCryptoBytes implements CryptoDigest,Serializ | |||||
super(); | super(); | ||||
} | } | ||||
public HashDigest(byte[] encodedDigestBytes) { | |||||
super(encodedDigestBytes); | |||||
public HashDigest(byte[] cryptoBytes) { | |||||
super(cryptoBytes); | |||||
} | } | ||||
@Override | @Override | ||||
@@ -29,7 +29,7 @@ public class SM3HashFunction implements HashFunction { | |||||
public HashDigest hash(byte[] data) { | public HashDigest hash(byte[] data) { | ||||
if (data == null) { | if (data == null) { | ||||
throw new CryptoException("The input is null!"); | |||||
throw new CryptoException("data is null!"); | |||||
} | } | ||||
byte[] digestBytes = SM3Utils.hash(data); | byte[] digestBytes = SM3Utils.hash(data); | ||||
@@ -49,8 +49,11 @@ public class SM3HashFunction implements HashFunction { | |||||
} | } | ||||
@Override | @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!"); | |||||
} | |||||
} | } | ||||
} | } |
@@ -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 { | |||||
} |
@@ -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())); | |||||
} | |||||
} |
@@ -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 { | |||||
} |
@@ -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.assertArrayEquals; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertTrue; | 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.AsymmetricCipherKeyPair; | ||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters; | import org.bouncycastle.crypto.params.ECPrivateKeyParameters; | ||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters; | import org.bouncycastle.crypto.params.ECPublicKeyParameters; | ||||
@@ -17,10 +13,6 @@ import org.junit.Test; | |||||
import com.jd.blockchain.crypto.utils.sm.SM2Utils; | import com.jd.blockchain.crypto.utils.sm.SM2Utils; | ||||
import java.security.KeyPair; | |||||
import java.util.Arrays; | |||||
import java.util.Random; | |||||
public class SM2UtilsTest { | public class SM2UtilsTest { | ||||
@Test | @Test |
@@ -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.assertArrayEquals; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import com.jd.blockchain.utils.security.ShaUtils; | |||||
import org.bouncycastle.util.encoders.Hex; | import org.bouncycastle.util.encoders.Hex; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import com.jd.blockchain.crypto.utils.sm.SM3Utils; | import com.jd.blockchain.crypto.utils.sm.SM3Utils; | ||||
import java.util.Random; | |||||
public class SM3UtilsTest { | public class SM3UtilsTest { | ||||
private static final int SM3DIGEST_LENGTH = 32; | private static final int SM3DIGEST_LENGTH = 32; |
@@ -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.assertArrayEquals; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import com.jd.blockchain.utils.security.AESUtils; | |||||
import org.bouncycastle.util.encoders.Hex; | import org.bouncycastle.util.encoders.Hex; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import com.jd.blockchain.crypto.utils.sm.SM4Utils; | import com.jd.blockchain.crypto.utils.sm.SM4Utils; | ||||
import java.util.Random; | |||||
public class SM4UtilsTest { | public class SM4UtilsTest { | ||||
private static final int KEY_SIZE = 16; | private static final int KEY_SIZE = 16; |
@@ -14,7 +14,7 @@ | |||||
<module>crypto-framework</module> | <module>crypto-framework</module> | ||||
<module>crypto-classic</module> | <module>crypto-classic</module> | ||||
<module>crypto-sm</module> | <module>crypto-sm</module> | ||||
<module>crypto-jni-clib</module> | |||||
<!-- <module>crypto-jni-clib</module> --> | |||||
<module>crypto-adv</module> | <module>crypto-adv</module> | ||||
</modules> | </modules> | ||||
@@ -2,17 +2,13 @@ package test.com.jd.blockchain.ledger; | |||||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | ||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | import com.jd.blockchain.binaryproto.DataContractRegistry; | ||||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||||
import com.jd.blockchain.crypto.PubKey; | import com.jd.blockchain.crypto.PubKey; | ||||
import com.jd.blockchain.crypto.hash.HashDigest; | import com.jd.blockchain.crypto.hash.HashDigest; | ||||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | ||||
import com.jd.blockchain.crypto.service.sm.SMCryptoService; | import com.jd.blockchain.crypto.service.sm.SMCryptoService; | ||||
import com.jd.blockchain.ledger.AccountHeader; | import com.jd.blockchain.ledger.AccountHeader; | ||||
import com.jd.blockchain.ledger.ParticipantNode; | |||||
import com.jd.blockchain.ledger.UserInfo; | import com.jd.blockchain.ledger.UserInfo; | ||||
import com.jd.blockchain.ledger.core.AccountSet; | 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 com.jd.blockchain.utils.Bytes; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
@@ -277,7 +277,7 @@ | |||||
<dependency> | <dependency> | ||||
<groupId>org.bouncycastle</groupId> | <groupId>org.bouncycastle</groupId> | ||||
<artifactId>bcprov-jdk15on</artifactId> | <artifactId>bcprov-jdk15on</artifactId> | ||||
<version>1.59</version> | |||||
<version>1.61</version> | |||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||