Browse Source

Fixed compilation errors due to merging develop branch;

tags/1.0.0
huanghaiquan 6 years ago
parent
commit
f58c94b445
8 changed files with 0 additions and 1855 deletions
  1. +0
    -223
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/AsymmtricCryptographyImpl.java
  2. +0
    -82
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/HashCryptographyImpl.java
  3. +0
    -130
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/asymmetric/JNIED25519SignatureFunction.java
  4. +0
    -53
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNIRIPEMD160HashFunction.java
  5. +0
    -53
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNISHA256HashFunction.java
  6. +0
    -35
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/symmetric/SymmetricCryptography.java
  7. +0
    -946
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java
  8. +0
    -333
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java

+ 0
- 223
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/AsymmtricCryptographyImpl.java View File

@@ -1,223 +0,0 @@
package com.jd.blockchain.crypto.impl;

import com.jd.blockchain.crypto.Ciphertext;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.asymmetric.AsymmetricEncryptionFunction;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.PrivKey;
import com.jd.blockchain.crypto.asymmetric.PubKey;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.impl.def.asymmetric.ED25519SignatureFunction;
import com.jd.blockchain.crypto.impl.sm.asymmetric.SM2CryptoFunction;

public class AsymmtricCryptographyImpl implements AsymmetricCryptography {

private static final SignatureFunction ED25519_SIGF = new ED25519SignatureFunction();

private static final SignatureFunction SM2_SIGF = new SM2CryptoFunction();

// private static final SignatureFunction JNIED25519_SIGF = new JNIED25519SignatureFunction();

private static final AsymmetricEncryptionFunction SM2_ENCF = new SM2CryptoFunction();

/**
* 封装了非对称密码算法对应的密钥生成算法
*/
@Override
public CryptoKeyPair generateKeyPair(CryptoAlgorithm algorithm) {

//判断算法是签名算法还是非对称加密算法,并根据算法生成密钥对,否则抛出异常
if (algorithm.isSignable() && algorithm.isAsymmetric()){
return getSignatureFunction(algorithm).generateKeyPair();
}
else if (algorithm.isEncryptable() && algorithm.isAsymmetric()){
return getAsymmetricEncryptionFunction(algorithm).generateKeyPair();
}
else throw new IllegalArgumentException("The specified algorithm is not signature or asymmetric encryption algorithm!");
}

@Override
public SignatureFunction getSignatureFunction(CryptoAlgorithm algorithm) {
//遍历签名算法,如果满足,则返回实例
switch (algorithm) {
case ED25519:
return ED25519_SIGF;
case SM2:
return SM2_SIGF;
// case JNIED25519:
// return JNIED25519_SIGF;
default:
break;
}
throw new IllegalArgumentException("The specified algorithm is not signature algorithm!");
}

@Override
public boolean verify(byte[] digestBytes, byte[] pubKeyBytes, byte[] data) {

//得到SignatureDigest类型的签名摘要,并得到算法标识
SignatureDigest signatureDigest = resolveSignatureDigest(digestBytes);
CryptoAlgorithm algorithm = signatureDigest.getAlgorithm();
PubKey pubKey = resolvePubKey(pubKeyBytes);

//验证两个输入中算法标识一致,否则抛出异常
if (algorithm != signatureDigest.getAlgorithm())
throw new IllegalArgumentException("Digest's algorithm and key's are not matching!");

//根据算法标识,调用对应算法实例来验证签名摘要
return getSignatureFunction(algorithm).verify(signatureDigest,pubKey,data);
}

@Override
public AsymmetricEncryptionFunction getAsymmetricEncryptionFunction(CryptoAlgorithm algorithm) {
//遍历非对称加密算法,如果满足,则返回实例
switch (algorithm) {
case SM2:
return SM2_ENCF;
default:
break;
}
throw new IllegalArgumentException("The specified algorithm is not asymmetric encryption algorithm!");
}

@Override
public byte[] decrypt(byte[] privKeyBytes, byte[] ciphertextBytes) {

//分别得到PrivKey和Ciphertext类型的密钥和密文,以及privKey对应的算法
PrivKey privKey = resolvePrivKey(privKeyBytes);
Ciphertext ciphertext = resolveCiphertext(ciphertextBytes);
CryptoAlgorithm algorithm = privKey.getAlgorithm();

//验证两个输入中算法标识一致,否则抛出异常
if (algorithm != ciphertext.getAlgorithm())
throw new IllegalArgumentException("Ciphertext's algorithm and key's are not matching!");

//根据算法标识,调用对应算法实例来计算返回明文
return getAsymmetricEncryptionFunction(algorithm).decrypt(privKey,ciphertext);
}

@Override
public Ciphertext resolveCiphertext(byte[] ciphertextBytes) {
Ciphertext ciphertext = tryResolveCiphertext(ciphertextBytes);
if (ciphertext == null)
throw new IllegalArgumentException("This ciphertextBytes cannot be resolved!");
else return ciphertext;
}

@Override
public Ciphertext tryResolveCiphertext(byte[] ciphertextBytes) {
//遍历非对称加密算法,如果满足,则返回解析结果
if (SM2_ENCF.supportCiphertext(ciphertextBytes)){
return SM2_ENCF.resolveCiphertext(ciphertextBytes);
}
//否则返回null
return null;
}

@Override
public SignatureDigest resolveSignatureDigest(byte[] digestBytes) {
SignatureDigest signatureDigest = tryResolveSignatureDigest(digestBytes);
if (signatureDigest == null)
throw new IllegalArgumentException("This digestBytes cannot be resolved!");
else return signatureDigest;
}

@Override
public SignatureDigest tryResolveSignatureDigest(byte[] digestBytes) {
//遍历签名算法,如果满足,则返回解析结果
if (ED25519_SIGF.supportDigest(digestBytes)){
return ED25519_SIGF.resolveDigest(digestBytes);
}
if (SM2_SIGF.supportDigest(digestBytes)){
return SM2_SIGF.resolveDigest(digestBytes);
}
// if (JNIED25519_SIGF.supportDigest(digestBytes)){
// return JNIED25519_SIGF.resolveDigest(digestBytes);
// }
//否则返回null
return null;
}

@Override
public byte[] retrievePubKeyBytes(byte[] privKeyBytes) {
byte[] pubKeyBytes = tryRetrievePubKeyBytes(privKeyBytes);
if (pubKeyBytes == null)
throw new IllegalArgumentException("The specified algorithm in privKeyBytes is not signature or asymmetric encryption algorithm!");
else return pubKeyBytes;
}

@Override
public byte[] tryRetrievePubKeyBytes(byte[] privKeyBytes) {
//解析私钥获得算法标识
CryptoAlgorithm algorithm = resolvePrivKey(privKeyBytes).getAlgorithm();

//判断算法是签名算法还是非对称加密算法,并根据算法生成密钥对,否则抛出异常
if (algorithm.isSignable() && algorithm.isAsymmetric()){
return getSignatureFunction(algorithm).retrievePubKeyBytes(privKeyBytes);
}
else if (algorithm.isEncryptable() && algorithm.isAsymmetric()){
return getAsymmetricEncryptionFunction(algorithm).retrievePubKeyBytes(privKeyBytes);
}
//否则返回null
return null;
}

@Override
public PubKey resolvePubKey(byte[] pubKeyBytes) {
PubKey pubKey = tryResolvePubKey(pubKeyBytes);
if (pubKey == null)
throw new IllegalArgumentException("This pubKeyBytes cannot be resolved!");
else return pubKey;

}

@Override
public PubKey tryResolvePubKey(byte[] pubKeyBytes) {
//遍历签名算法,如果满足,则返回解析结果
if (ED25519_SIGF.supportPubKey(pubKeyBytes)){
return ED25519_SIGF.resolvePubKey(pubKeyBytes);
}
if (SM2_SIGF.supportPubKey(pubKeyBytes)){
return SM2_SIGF.resolvePubKey(pubKeyBytes);
}
// if (JNIED25519_SIGF.supportPubKey(pubKeyBytes)){
// return JNIED25519_SIGF.resolvePubKey(pubKeyBytes);
// }
//遍历非对称加密算法,如果满足,则返回解析结果
if (SM2_ENCF.supportPubKey(pubKeyBytes)){
return SM2_ENCF.resolvePubKey(pubKeyBytes);
}
//否则返回null
return null;
}

@Override
public PrivKey resolvePrivKey(byte[] privKeyBytes) {
PrivKey privKey = tryResolvePrivKey(privKeyBytes);
if (privKey == null)
throw new IllegalArgumentException("This privKeyBytes cannot be resolved!");
else return privKey;
}

@Override
public PrivKey tryResolvePrivKey(byte[] privKeyBytes) {
//遍历签名算法,如果满足,则返回解析结果
if (ED25519_SIGF.supportPrivKey(privKeyBytes)){
return ED25519_SIGF.resolvePrivKey(privKeyBytes);
}
if (SM2_SIGF.supportPrivKey(privKeyBytes)){
return SM2_SIGF.resolvePrivKey(privKeyBytes);
}
// if (JNIED25519_SIGF.supportPrivKey(privKeyBytes)){
// return JNIED25519_SIGF.resolvePrivKey(privKeyBytes);
// }
//遍历非对称加密算法,如果满足,则返回解析结果
if (SM2_ENCF.supportPrivKey(privKeyBytes)){
return SM2_ENCF.resolvePrivKey(privKeyBytes);
}
//否则返回null
return null;
}
}

+ 0
- 82
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/HashCryptographyImpl.java View File

@@ -1,82 +0,0 @@
package com.jd.blockchain.crypto.impl;

import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.hash.HashCryptography;
import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.crypto.impl.def.hash.RIPEMD160HashFunction;
import com.jd.blockchain.crypto.impl.def.hash.SHA256HashFunction;
import com.jd.blockchain.crypto.impl.sm.hash.SM3HashFunction;

public class HashCryptographyImpl implements HashCryptography {

private static final HashFunction SHA256_FUNC = new SHA256HashFunction();
private static final HashFunction RIPEMD160_FUNC = new RIPEMD160HashFunction();
private static final HashFunction SM3_FUNC = new SM3HashFunction();

// private static final HashFunction JNISHA256_FUNC = new JNISHA256HashFunction();
// private static final HashFunction JNIRIPEMD160_FUNC = new JNIRIPEMD160HashFunction();

@Override
public HashFunction getFunction(CryptoAlgorithm algorithm) {

// 遍历哈希算法,如果满足,则返回实例
switch (algorithm) {
case SHA256:
return SHA256_FUNC;
case RIPEMD160:
return RIPEMD160_FUNC;
case SM3:
return SM3_FUNC;
// case JNISHA256:
// return JNISHA256_FUNC;
// case JNIRIPEMD160:
// return JNIRIPEMD160_FUNC;
default:
break;
}
throw new IllegalArgumentException("The specified algorithm is not hash algorithm!");
}

@Override
public boolean verify(byte[] digestBytes, byte[] data) {
HashDigest hashDigest = resolveHashDigest(digestBytes);
return verify(hashDigest,data);
}

@Override
public boolean verify(HashDigest digest, byte[] data) {
CryptoAlgorithm algorithm = digest.getAlgorithm();
return getFunction(algorithm).verify(digest, data);
}

@Override
public HashDigest resolveHashDigest(byte[] digestBytes) {
HashDigest hashDigest = tryResolveHashDigest(digestBytes);
if (hashDigest == null)
throw new IllegalArgumentException("This digestBytes cannot be resolved!");
else return hashDigest;
}

@Override
public HashDigest tryResolveHashDigest(byte[] digestBytes) {
//遍历哈希函数,如果满足,则返回解析结果
if (SHA256_FUNC.supportHashDigest(digestBytes)) {
return SHA256_FUNC.resolveHashDigest(digestBytes);
}
if (RIPEMD160_FUNC.supportHashDigest(digestBytes)) {
return RIPEMD160_FUNC.resolveHashDigest(digestBytes);
}
if (SM3_FUNC.supportHashDigest(digestBytes)) {
return SM3_FUNC.resolveHashDigest(digestBytes);
}
// if (JNISHA256_FUNC.supportHashDigest(digestBytes)) {
// return JNISHA256_FUNC.resolveHashDigest(digestBytes);
// }
// if (JNIRIPEMD160_FUNC.supportHashDigest(digestBytes)) {
// return JNIRIPEMD160_FUNC.resolveHashDigest(digestBytes);
// }
//否则返回null
return null;
}
}

+ 0
- 130
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/asymmetric/JNIED25519SignatureFunction.java View File

@@ -1,130 +0,0 @@
//package com.jd.blockchain.crypto.impl.jni.asymmetric;
//
//import com.jd.blockchain.crypto.CryptoAlgorithm;
//import com.jd.blockchain.crypto.asymmetric.*;
//import com.jd.blockchain.crypto.jniutils.asymmetric.JNIED25519Utils;
//
//import static com.jd.blockchain.crypto.CryptoAlgorithm.JNIED25519;
//import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_BYTES;
//import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
//import static com.jd.blockchain.crypto.CryptoKeyType.PUB_KEY;
//import static com.jd.blockchain.crypto.base.BaseCryptoKey.KEY_TYPE_BYTES;
//
//public class JNIED25519SignatureFunction implements SignatureFunction {
//
// private static final int PUBKEY_SIZE = 32;
// private static final int PRIVKEY_SIZE = 32;
// private static final int DIGEST_SIZE = 64;
//
// private static final int PUBKEY_LENGTH = ALGORYTHM_BYTES + KEY_TYPE_BYTES + PUBKEY_SIZE;
// private static final int PRIVKEY_LENGTH = ALGORYTHM_BYTES + KEY_TYPE_BYTES + PRIVKEY_SIZE;
// private static final int SIGNATUREDIGEST_LENGTH = ALGORYTHM_BYTES + DIGEST_SIZE;
//
// public JNIED25519SignatureFunction() {
// }
//
// @Override
// public SignatureDigest sign(PrivKey privKey, byte[] data) {
//
// if (data == null)
// throw new IllegalArgumentException("This data is null!");
//
// JNIED25519Utils ed25519 = new JNIED25519Utils();
//
// byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
// byte[] rawPubKeyBytes = ed25519.getPubKey(rawPrivKeyBytes);
//
// // 验证原始私钥长度为256比特,即32字节
// if (rawPrivKeyBytes.length != PRIVKEY_SIZE)
// throw new IllegalArgumentException("This key has wrong format!");
//
// // 验证密钥数据的算法标识对应JNIED25519签名算法
// if (privKey.getAlgorithm() != JNIED25519)
// throw new IllegalArgumentException("This key is not ED25519 private key!");
//
// // 调用JNIED25519签名算法计算签名结果
// return new SignatureDigest(JNIED25519, ed25519.sign(data, rawPrivKeyBytes, rawPubKeyBytes));
// }
//
// @Override
// public boolean verify(SignatureDigest digest, PubKey pubKey, byte[] data) {
//
// JNIED25519Utils ed25519 = new JNIED25519Utils();
//
// byte[] rawPubKeyBytes = pubKey.getRawKeyBytes();
// byte[] rawDigestBytes = digest.getRawDigest();
//
// // 验证原始公钥长度为256比特,即32字节
// if (rawPubKeyBytes.length != PUBKEY_SIZE)
// throw new IllegalArgumentException("This key has wrong format!");
//
// // 验证密钥数据的算法标识对应JNIED25519签名算法
// if (pubKey.getAlgorithm() != JNIED25519)
// throw new IllegalArgumentException("This key is not ED25519 public key!");
//
// // 验证密文数据的算法标识对应JNIED25519签名算法,并且原始摘要长度为64字节
// if (digest.getAlgorithm() != JNIED25519 || rawDigestBytes.length != DIGEST_SIZE)
// throw new IllegalArgumentException("This is not ED25519 signature digest!");
//
// // 调用JNIED25519验签算法验证签名结果
// return ed25519.verify(data, rawPubKeyBytes, rawDigestBytes);
// }
//
// @Override
// public boolean supportPrivKey(byte[] privKeyBytes) {
// // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应JNIED25519签名算法,并且密钥类型是私钥
// return privKeyBytes.length == PRIVKEY_LENGTH
// && privKeyBytes[0] == JNIED25519.CODE && privKeyBytes[1] == PRIV_KEY.CODE;
// }
//
// @Override
// public PrivKey resolvePrivKey(byte[] privKeyBytes) {
// // 由框架调用 support 方法检查有效性,在此不做重复检查;
// return new PrivKey(privKeyBytes);
// }
//
// @Override
// public boolean supportPubKey(byte[] pubKeyBytes) {
// // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应JNIED25519签名算法,并且密钥类型是公钥
// return pubKeyBytes.length == PUBKEY_LENGTH &&
// pubKeyBytes[0] == JNIED25519.CODE && pubKeyBytes[1] == PUB_KEY.CODE;
//
// }
//
// @Override
// public PubKey resolvePubKey(byte[] pubKeyBytes) {
// // 由框架调用 support 方法检查有效性,在此不做重复检查;
// return new PubKey(pubKeyBytes);
// }
//
// @Override
// public boolean supportDigest(byte[] digestBytes) {
// // 验证输入字节数组长度=算法标识长度+摘要长度,字节数组的算法标识对应JNIED25519算法
// return digestBytes.length == SIGNATUREDIGEST_LENGTH && digestBytes[0] == JNIED25519.CODE;
// }
//
// @Override
// public SignatureDigest resolveDigest(byte[] digestBytes) {
// // 由框架调用 support 方法检查有效性,在此不做重复检查;
// return new SignatureDigest(digestBytes);
// }
//
// @Override
// public CryptoAlgorithm getAlgorithm() {
// return JNIED25519;
// }
//
// @Override
// public CryptoKeyPair generateKeyPair() {
//
// JNIED25519Utils ed25519 = new JNIED25519Utils();
// byte[] rawPrivKeyBytes = new byte[PRIVKEY_SIZE];
// byte[] rawPubKeyBytes = new byte[PUBKEY_SIZE];
//
// // 调用JNIED25519算法的密钥生成算法生成公私钥对
// ed25519.generateKeyPair(rawPrivKeyBytes, rawPubKeyBytes);
//
// return new CryptoKeyPair(new PubKey(JNIED25519, rawPubKeyBytes), new PrivKey(JNIED25519, rawPrivKeyBytes));
//
// }
//}

+ 0
- 53
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNIRIPEMD160HashFunction.java View File

@@ -1,53 +0,0 @@
//package com.jd.blockchain.crypto.impl.jni.hash;
//
//import com.jd.blockchain.crypto.CryptoAlgorithm;
//import com.jd.blockchain.crypto.hash.HashDigest;
//import com.jd.blockchain.crypto.hash.HashFunction;
//import com.jd.blockchain.crypto.jniutils.hash.JNIRIPEMD160Utils;
//
//import java.util.Arrays;
//
//import static com.jd.blockchain.crypto.CryptoAlgorithm.JNIRIPEMD160;
//import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_BYTES;
//
//public class JNIRIPEMD160HashFunction implements HashFunction {
//
// private static final int DIGEST_BYTES = 160 / 8;
//
// private static final int DIGEST_LENGTH = ALGORYTHM_BYTES + DIGEST_BYTES;
//
// @Override
// public CryptoAlgorithm getAlgorithm() {
// return JNIRIPEMD160;
// }
//
// @Override
// public HashDigest hash(byte[] data) {
//
// if (data == null)
// throw new IllegalArgumentException("This data is null!");
//
// JNIRIPEMD160Utils ripemd160 = new JNIRIPEMD160Utils();
// byte[] digestBytes = ripemd160.hash(data);
// return new HashDigest(JNIRIPEMD160, digestBytes);
// }
//
// @Override
// public boolean verify(HashDigest digest, byte[] data) {
// HashDigest hashDigest = hash(data);
// return Arrays.equals(hashDigest.toBytes(), digest.toBytes());
// }
//
// @Override
// public boolean supportHashDigest(byte[] digestBytes) {
// // 验证输入字节数组长度=算法标识长度+摘要长度,字节数组的算法标识对应JNIRIPEMD160算法
// return digestBytes.length == DIGEST_LENGTH && JNIRIPEMD160.CODE == digestBytes[0];
// }
//
// @Override
// public HashDigest resolveHashDigest(byte[] digestBytes) {
// // 由框架调用 support 方法检查有效性,在此不做重复检查;
// return new HashDigest(digestBytes);
// }
//}
//

+ 0
- 53
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNISHA256HashFunction.java View File

@@ -1,53 +0,0 @@
//package com.jd.blockchain.crypto.impl.jni.hash;
//
//import com.jd.blockchain.crypto.CryptoAlgorithm;
//import com.jd.blockchain.crypto.hash.HashDigest;
//import com.jd.blockchain.crypto.hash.HashFunction;
//import com.jd.blockchain.crypto.jniutils.hash.JNISHA256Utils;
//
//import java.util.Arrays;
//
//import static com.jd.blockchain.crypto.CryptoAlgorithm.JNISHA256;
//import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_BYTES;
//
//public class JNISHA256HashFunction implements HashFunction {
//
// private static final int DIGEST_BYTES = 256/8;
//
// private static final int DIGEST_LENGTH = ALGORYTHM_BYTES + DIGEST_BYTES;
//
// @Override
// public CryptoAlgorithm getAlgorithm() {
// return JNISHA256;
// }
//
// @Override
// public HashDigest hash(byte[] data) {
//
// if (data == null)
// throw new IllegalArgumentException("This data is null!");
//
// JNISHA256Utils sha256 = new JNISHA256Utils();
// byte[] digestBytes = sha256.hash(data);
// return new HashDigest(JNISHA256,digestBytes);
// }
//
// @Override
// public boolean verify(HashDigest digest, byte[] data) {
// HashDigest hashDigest=hash(data);
// return Arrays.equals(hashDigest.toBytes(),digest.toBytes());
// }
//
// @Override
// public boolean supportHashDigest(byte[] digestBytes) {
// // 验证输入字节数组长度=算法标识长度+摘要长度,字节数组的算法标识对应JNISHA256算法
// return digestBytes.length == DIGEST_LENGTH && JNISHA256.CODE == digestBytes[0];
// }
//
// @Override
// public HashDigest resolveHashDigest(byte[] hashDigestBytes) {
// // 由框架调用 support 方法检查有效性,在此不做重复检查;
// return new HashDigest(hashDigestBytes);
// }
//
//}

+ 0
- 35
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/symmetric/SymmetricCryptography.java View File

@@ -1,35 +0,0 @@
//package com.jd.blockchain.crypto.symmetric;
//
//import com.jd.blockchain.crypto.Ciphertext;
//import com.jd.blockchain.crypto.CryptoAlgorithm;
//import com.jd.blockchain.crypto.SymmetricKey;
//
//public interface SymmetricCryptography {
//
// /**
// * 生成密钥对;
// *
// * @param algorithm
// * @return
// */
// SymmetricKey generateKey(CryptoAlgorithm algorithm);
//
// /**
// * 获取签名方法;
// *
// * @param algorithm
// * @return
// */
// SymmetricEncryptionFunction getSymmetricEncryptionFunction(CryptoAlgorithm algorithm);
//
// byte[] decrypt(byte[] symmetricKeyBytes,byte[] ciphertextBytes);
//
// Ciphertext resolveCiphertext(byte[] ciphertextBytes);
//
// Ciphertext tryResolveCiphertext(byte[] ciphertextBytes);
//
// SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes);
//
// SymmetricKey tryResolveSymmetricKey(byte[] symmetricKeyBytes);
//
//}

+ 0
- 946
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java View File

@@ -1,946 +0,0 @@
package test.com.jd.blockchain.crypto.asymmetric;

import com.jd.blockchain.crypto.Ciphertext;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoException;
import com.jd.blockchain.crypto.CryptoKeyType;
import com.jd.blockchain.crypto.asymmetric.*;
import com.jd.blockchain.crypto.impl.AsymmtricCryptographyImpl;
import com.jd.blockchain.utils.io.BytesUtils;

import org.junit.Test;

import java.util.Random;

import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PUB_KEY;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;

public class AsymmtricCryptographyImplTest {

@Test
public void testGenerateKeyPair() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;
CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm);

assertNotNull(keyPair);

PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();

assertNotNull(pubKey);
assertNotNull(privKey);

assertEquals(algorithm,pubKey.getAlgorithm());
assertEquals(algorithm,privKey.getAlgorithm());

assertEquals(32,pubKey.getRawKeyBytes().length);
assertEquals(32,privKey.getRawKeyBytes().length);

byte[] pubKeyBytes = pubKey.toBytes();
byte[] privKeyBytes = privKey.toBytes();

assertEquals(32+1+1,pubKeyBytes.length);
assertEquals(32+1+1,privKeyBytes.length);

assertEquals(CryptoAlgorithm.ED25519.CODE,pubKeyBytes[0]);
assertEquals(CryptoAlgorithm.ED25519.CODE,privKeyBytes[0]);
assertEquals(CryptoAlgorithm.ED25519, CryptoAlgorithm.valueOf(pubKey.getAlgorithm().CODE));
assertEquals(CryptoAlgorithm.ED25519, CryptoAlgorithm.valueOf(privKey.getAlgorithm().CODE));

assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[1]);
assertEquals(privKey.getKeyType().CODE,privKeyBytes[1]);


//test SM2
algorithm = CryptoAlgorithm.SM2;
keyPair = asymmetricCrypto.generateKeyPair(algorithm);

assertNotNull(keyPair);

pubKey = keyPair.getPubKey();
privKey = keyPair.getPrivKey();

assertNotNull(pubKey);
assertNotNull(privKey);

assertEquals(algorithm,pubKey.getAlgorithm());
assertEquals(algorithm,privKey.getAlgorithm());

assertEquals(65,pubKey.getRawKeyBytes().length);
assertEquals(32,privKey.getRawKeyBytes().length);

pubKeyBytes = pubKey.toBytes();
privKeyBytes = privKey.toBytes();

assertEquals(32+1+1,privKeyBytes.length);
assertEquals(65+1+1,pubKeyBytes.length);

assertEquals(CryptoAlgorithm.SM2.CODE,pubKeyBytes[0]);
assertEquals(CryptoAlgorithm.SM2.CODE,privKeyBytes[0]);
assertEquals(CryptoAlgorithm.SM2, CryptoAlgorithm.valueOf(pubKey.getAlgorithm().CODE));
assertEquals(CryptoAlgorithm.SM2, CryptoAlgorithm.valueOf(privKey.getAlgorithm().CODE));

assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[1]);
assertEquals(privKey.getKeyType().CODE,privKeyBytes[1]);


//test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
// keyPair = asymmetricCrypto.generateKeyPair(algorithm);
//
// assertNotNull(keyPair);
//
// pubKey = keyPair.getPubKey();
// privKey = keyPair.getPrivKey();
//
// assertNotNull(pubKey);
// assertNotNull(privKey);
//
// assertEquals(algorithm,pubKey.getAlgorithm());
// assertEquals(algorithm,privKey.getAlgorithm());
//
// assertEquals(32,pubKey.getRawKeyBytes().length);
// assertEquals(32,privKey.getRawKeyBytes().length);
//
// pubKeyBytes = pubKey.toBytes();
// privKeyBytes = privKey.toBytes();
//
// assertEquals(32+1+1,pubKeyBytes.length);
// assertEquals(32+1+1,privKeyBytes.length);
//
// assertEquals(CryptoAlgorithm.JNIED25519.CODE,pubKeyBytes[0]);
// assertEquals(CryptoAlgorithm.JNIED25519.CODE,privKeyBytes[0]);
// assertEquals(CryptoAlgorithm.JNIED25519, CryptoAlgorithm.valueOf(pubKey.getAlgorithm().CODE));
// assertEquals(CryptoAlgorithm.JNIED25519, CryptoAlgorithm.valueOf(privKey.getAlgorithm().CODE));
//
// assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[1]);
// assertEquals(privKey.getKeyType().CODE,privKeyBytes[1]);
}

@Test
public void testGetSignatureFunction() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random random = new Random();


//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

// 测试256字节的消息进行签名
byte[] data = new byte[256];
random.nextBytes(data);
verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,null);

//错误的算法标识
verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,32,32,64,IllegalArgumentException.class);

data = null;
verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,NullPointerException.class);


//test SM2
algorithm = CryptoAlgorithm.SM2;

// 测试256字节的消息进行签名
data = new byte[256];
random.nextBytes(data);
verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,65,32,64,null);

//错误的算法标识
verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,65,32,64,IllegalArgumentException.class);

data = null;
verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,65,32,64,NullPointerException.class);


// //test JNNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// // 测试256字节的消息进行签名
// data = new byte[256];
// random.nextBytes(data);
// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,null);
//
// //错误的算法标识
// verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,32,32,64,IllegalArgumentException.class);
//
// data = null;
// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,IllegalArgumentException.class);
}

private void verifyGetSignatureFunction(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, byte[] data,
int expectedPubKeyLength, int expectedPrivKeyLength,
int expectedSignatureDigestLength, Class<?> expectedException){

//初始化一个异常
Exception actualEx = null;

try {
SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm);

assertNotNull(sf);

CryptoKeyPair keyPair = sf.generateKeyPair();
PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes();
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
byte[] pubKeyBytes = pubKey.toBytes();
byte[] privKeyBytes = privKey.toBytes();

assertEquals(algorithm, pubKey.getAlgorithm());
assertEquals(algorithm, privKey.getAlgorithm());
assertEquals(expectedPubKeyLength,rawPubKeyBytes.length);
assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length);

assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUB_KEY.CODE},rawPubKeyBytes), pubKeyBytes);
assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIV_KEY.CODE},rawPrivKeyBytes), privKeyBytes);

SignatureDigest signatureDigest = sf.sign(privKey,data);
byte[] rawDigest = signatureDigest.getRawDigest();

assertEquals(algorithm,signatureDigest.getAlgorithm());
assertEquals(expectedSignatureDigestLength,rawDigest.length);
byte[] signatureDigestBytes = signatureDigest.toBytes();
assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},rawDigest),signatureDigestBytes);

assertTrue(signatureDigest.equals(signatureDigest));
assertEquals(signatureDigest.hashCode(),signatureDigest.hashCode());

assertTrue(sf.verify(signatureDigest,pubKey,data));

assertTrue(sf.supportPubKey(pubKeyBytes));
assertTrue(sf.supportPrivKey(privKeyBytes));
assertTrue(sf.supportDigest(signatureDigestBytes));

assertEquals(pubKey,sf.resolvePubKey(pubKeyBytes));
assertEquals(privKey,sf.resolvePrivKey(privKeyBytes));
assertEquals(signatureDigest,sf.resolveDigest(signatureDigestBytes));

assertEquals(algorithm,sf.getAlgorithm());

} catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testVerify() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random randomData = new Random();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

// 测试256字节的消息进行签名
byte[] data = new byte[256];
randomData.nextBytes(data);
SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm);
CryptoKeyPair keyPair = sf.generateKeyPair();
byte[] pubKeyBytes = keyPair.getPubKey().toBytes();

byte[] signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null);

//签名数据末尾两个字节丢失情况下,抛出异常
byte[] truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class);

byte[] signatureDigestBytesWithWrongAlgCode = signatureDigestBytes;
signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class);

signatureDigestBytes = null;
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class);


//test SM2
algorithm = CryptoAlgorithm.SM2;

// 测试256字节的消息进行签名
data = new byte[256];
randomData.nextBytes(data);
sf = asymmetricCrypto.getSignatureFunction(algorithm);
keyPair = sf.generateKeyPair();
pubKeyBytes = keyPair.getPubKey().toBytes();

signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null);

//签名数据末尾两个字节丢失情况下,抛出异常
truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class);

signatureDigestBytesWithWrongAlgCode = signatureDigestBytes;
signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class);

signatureDigestBytes = null;
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class);

// //test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// // 测试256字节的消息进行签名
// data = new byte[256];
// randomData.nextBytes(data);
// sf = asymmetricCrypto.getSignatureFunction(algorithm);
// keyPair = sf.generateKeyPair();
// pubKeyBytes = keyPair.getPubKey().toBytes();
//
// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
// verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null);
//
// //签名数据末尾两个字节丢失情况下,抛出异常
// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class);
//
// signatureDigestBytesWithWrongAlgCode = signatureDigestBytes;
// signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class);
//
// signatureDigestBytes = null;
// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class);
}

private void verifyVerify(AsymmetricCryptography asymmetricCrypto,boolean expectedResult,byte[] data,
byte[] pubKeyBytes, byte[] signatureDigestBytes, Class<?> expectedException){

//初始化一个异常
Exception actualEx = null;
boolean pass = false;

try {

pass = asymmetricCrypto.verify(signatureDigestBytes,pubKeyBytes,data);

}
catch (Exception e){
actualEx = e;
}

assertEquals(expectedResult, pass);

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testGetAsymmetricEncryptionFunction() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random random = new Random();


//test SM2
CryptoAlgorithm algorithm = CryptoAlgorithm.SM2;

//Case 1: SM2Encryption with 16 bytes data
byte[] data = new byte[16];
random.nextBytes(data);
verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+16+32,data,null);

//Case 2: SM2Encryption with 256 bytes data
data = new byte[256];
random.nextBytes(data);
verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+256+32,data,null);

//Case 3: SM2Encryption with 1 bytes data
data = new byte[3];
random.nextBytes(data);
verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+3+32,data,null);

//Case 4: SM2Encryption with wrong algorithm
verifyGetAsymmetricEncryptionFunction(asymmetricCrypto,CryptoAlgorithm.AES,65,32,65+3+32,data,IllegalArgumentException.class);

//Case 5: SM2Encryption with null data
data = null;
verifyGetAsymmetricEncryptionFunction(asymmetricCrypto,algorithm,65,32,65+32,data,NullPointerException.class);
}

private void verifyGetAsymmetricEncryptionFunction(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
int expectedPubKeyLength, int expectedPrivKeyLength,
int expectedCiphertextLength, byte[] data, Class<?> expectedException){

//初始化一个异常
Exception actualEx = null;

try {
AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm);
//验证获取的算法实例非空
assertNotNull(aef);

CryptoKeyPair keyPair = aef.generateKeyPair();
PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes();
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
byte[] pubKeyBytes = pubKey.toBytes();
byte[] privKeyBytes = privKey.toBytes();

assertEquals(algorithm, pubKey.getAlgorithm());
assertEquals(algorithm, privKey.getAlgorithm());
assertEquals(expectedPubKeyLength,rawPubKeyBytes.length);
assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length);

assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUB_KEY.CODE},rawPubKeyBytes), pubKeyBytes);
assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIV_KEY.CODE},rawPrivKeyBytes), privKeyBytes);

Ciphertext ciphertext = aef.encrypt(pubKey,data);
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();

assertEquals(algorithm,ciphertext.getAlgorithm());
assertEquals(expectedCiphertextLength,rawCiphertextBytes.length);
byte[] ciphertextBytes = ciphertext.toBytes();
assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},rawCiphertextBytes),ciphertextBytes);

assertArrayEquals(data,aef.decrypt(privKey,ciphertext));

assertTrue(aef.supportPubKey(pubKeyBytes));
assertTrue(aef.supportPrivKey(privKeyBytes));
assertTrue(aef.supportCiphertext(ciphertextBytes));

assertEquals(pubKey,aef.resolvePubKey(pubKeyBytes));
assertEquals(privKey,aef.resolvePrivKey(privKeyBytes));
assertEquals(ciphertext,aef.resolveCiphertext(ciphertextBytes));

assertEquals(algorithm,aef.getAlgorithm());


}catch (Exception e){
actualEx = e;
}

if(expectedException == null){
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testDecrypt() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random random = new Random();

byte[] data = new byte[16];
random.nextBytes(data);

//test SM2
CryptoAlgorithm algorithm = CryptoAlgorithm.SM2;
AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm);
CryptoKeyPair keyPair = aef.generateKeyPair();
PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
Ciphertext ciphertext = aef.encrypt(pubKey,data);
byte[] ciphertextBytes = ciphertext.toBytes();

verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, ciphertextBytes, null);

//密钥的算法标识与密文的算法标识不一致情况
verifyDecrypt(asymmetricCrypto, CryptoAlgorithm.AES, rawPrivKeyBytes, data, ciphertextBytes, IllegalArgumentException.class);

//密文末尾两个字节丢失情况下,抛出异常
byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2];
System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length);
verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, truncatedCiphertextBytes, com.jd.blockchain.crypto.CryptoException.class);

byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes;
ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyDecrypt(asymmetricCrypto,algorithm,rawPrivKeyBytes,data,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class);

ciphertextBytes = null;
verifyDecrypt(asymmetricCrypto,algorithm,rawPrivKeyBytes,data,ciphertextBytes,NullPointerException.class);
}

private void verifyDecrypt(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
byte[] key, byte[] data, byte[] ciphertextBytes, Class<?> expectedException){
Exception actualEx = null;

try {
PrivKey privKey = new PrivKey(algorithm,key);

byte[] plaintext = asymmetricCrypto.decrypt(privKey.toBytes(), ciphertextBytes);

//解密后的明文与初始的明文一致
assertArrayEquals(data,plaintext);
}
catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testResolveCiphertext() {


AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random random = new Random();

byte[] data = new byte[16];
random.nextBytes(data);

//test SM2
CryptoAlgorithm algorithm = CryptoAlgorithm.SM2;
AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm);
CryptoKeyPair keyPair = aef.generateKeyPair();
PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
Ciphertext ciphertext = aef.encrypt(pubKey,data);
byte[] ciphertextBytes = ciphertext.toBytes();

verifyResolveCiphertext(asymmetricCrypto, algorithm, ciphertextBytes, null);


//密文末尾两个字节丢失情况下,抛出异常
byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2];
System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length);
verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, truncatedCiphertextBytes, CryptoException.class);

byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes;
ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyResolveCiphertext(asymmetricCrypto,algorithm,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class);

ciphertextBytes = null;
verifyResolveCiphertext(asymmetricCrypto,algorithm,ciphertextBytes,NullPointerException.class);
}

private void verifyResolveCiphertext(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, byte[] ciphertextBytes,
Class<?> expectedException){
Exception actualEx = null;

try {

Ciphertext ciphertext = asymmetricCrypto.resolveCiphertext(ciphertextBytes);

assertNotNull(ciphertext);

assertEquals(algorithm, ciphertext.getAlgorithm());

assertArrayEquals(ciphertextBytes, ciphertext.toBytes());
}
catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testTryResolveCiphertext() {
}

@Test
public void testResolveSignatureDigest() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();
Random randomData = new Random();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

// 测试256字节的消息进行签名
byte[] data = new byte[256];
randomData.nextBytes(data);
SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm);
CryptoKeyPair keyPair = sf.generateKeyPair();

byte[] signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null);

//签名数据末尾两个字节丢失情况下,抛出异常
byte[] truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class);

signatureDigestBytes = null;
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class);


//test SM2
algorithm = CryptoAlgorithm.SM2;

// 测试256字节的消息进行签名
data = new byte[256];
randomData.nextBytes(data);
sf = asymmetricCrypto.getSignatureFunction(algorithm);
keyPair = sf.generateKeyPair();

signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null);

//签名数据末尾两个字节丢失情况下,抛出异常
truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class);

signatureDigestBytes = null;
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class);

// //test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// // 测试256字节的消息进行签名
// data = new byte[256];
// randomData.nextBytes(data);
// sf = asymmetricCrypto.getSignatureFunction(algorithm);
// keyPair = sf.generateKeyPair();
//
// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes();
// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null);
//
// //签名数据末尾两个字节丢失情况下,抛出异常
// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2];
// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length);
// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class);
//
// signatureDigestBytes = null;
// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class);
}

private void verifyResolveSignatureDigest(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
int expectedSignatureDigestLength,
byte[] signatureDigestBytes, Class<?> expectedException){

//初始化一个异常
Exception actualEx = null;

try {

SignatureDigest signatureDigest = asymmetricCrypto.resolveSignatureDigest(signatureDigestBytes);

assertNotNull(signatureDigest);

assertEquals(algorithm,signatureDigest.getAlgorithm());

assertEquals(expectedSignatureDigestLength,signatureDigest.getRawDigest().length);

assertArrayEquals(signatureDigestBytes,signatureDigest.toBytes());

}
catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testTryResolveSignatureDigest() {
}

@Test
public void testRetrievePubKeyBytes() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm);

byte[] expectedPrivKeyBytes = keyPair.getPrivKey().toBytes();
byte[] expectedPubKeyBytes = keyPair.getPubKey().toBytes();

byte[] pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes);

assertArrayEquals(expectedPubKeyBytes,pubKeyBytes);


//test SM2
algorithm = CryptoAlgorithm.SM2;

keyPair = asymmetricCrypto.generateKeyPair(algorithm);

expectedPrivKeyBytes = keyPair.getPrivKey().toBytes();
expectedPubKeyBytes = keyPair.getPubKey().toBytes();

pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes);

assertArrayEquals(expectedPubKeyBytes,pubKeyBytes);


// //test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// keyPair = asymmetricCrypto.generateKeyPair(algorithm);
//
// expectedPrivKeyBytes = keyPair.getPrivKey().toBytes();
// expectedPubKeyBytes = keyPair.getPubKey().toBytes();
//
// pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes);
//
// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes);

}


@Test
public void testResolvePubKey() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm);

byte[] pubKeyBytes = keyPair.getPubKey().toBytes();
verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,null);

byte[] truncatedPubKeyBytes = new byte[pubKeyBytes.length-2];
System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length);
verifyResolvePubKey(asymmetricCrypto,algorithm,32,truncatedPubKeyBytes,IllegalArgumentException.class);

byte[] pubKeyBytesWithWrongAlgCode = pubKeyBytes;
pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);

byte[] pubKeyBytesWithWrongKeyType= pubKeyBytes;
pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);

pubKeyBytes = null;
verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,NullPointerException.class);


//test SM2
algorithm = CryptoAlgorithm.SM2;

keyPair = asymmetricCrypto.generateKeyPair(algorithm);

pubKeyBytes = keyPair.getPubKey().toBytes();
verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytes,null);

truncatedPubKeyBytes = new byte[pubKeyBytes.length-2];
System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length);
verifyResolvePubKey(asymmetricCrypto,algorithm,65,truncatedPubKeyBytes,IllegalArgumentException.class);

pubKeyBytesWithWrongAlgCode = pubKeyBytes;
pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);

pubKeyBytesWithWrongKeyType= pubKeyBytes;
pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);

pubKeyBytes = null;
verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytes,NullPointerException.class);

// //test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// keyPair = asymmetricCrypto.generateKeyPair(algorithm);
//
// pubKeyBytes = keyPair.getPubKey().toBytes();
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,null);
//
// truncatedPubKeyBytes = new byte[pubKeyBytes.length-2];
// System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length);
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,truncatedPubKeyBytes,IllegalArgumentException.class);
//
// pubKeyBytesWithWrongAlgCode = pubKeyBytes;
// pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
//
// pubKeyBytesWithWrongKeyType= pubKeyBytes;
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);
//
// pubKeyBytes = null;
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,NullPointerException.class);
}

private void verifyResolvePubKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
int expectedPubKeyLength, byte[] pubKeyBytes,Class<?> expectedException){

Exception actualEx = null;

try {
PubKey pubKey = asymmetricCrypto.resolvePubKey(pubKeyBytes);

assertNotNull(pubKey);

assertEquals(algorithm, pubKey.getAlgorithm());

assertEquals(expectedPubKeyLength, pubKey.getRawKeyBytes().length);

assertArrayEquals(pubKeyBytes, pubKey.toBytes());

}
catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testTryResolvePubKey() {
}

@Test
public void testResolvePrivKey() {

AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl();

//test ED25519
CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519;

CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm);

byte[] privKeyBytes = keyPair.getPrivKey().toBytes();
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,null);

byte[] truncatedPrivKeyBytes = new byte[privKeyBytes.length-2];
System.arraycopy(privKeyBytes,0,truncatedPrivKeyBytes,0,truncatedPrivKeyBytes.length);
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,truncatedPrivKeyBytes,IllegalArgumentException.class);

byte[] privKeyBytesWithWrongAlgCode = privKeyBytes;
privKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);

byte[] privKeyBytesWithWrongKeyType = privKeyBytes;
privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);

privKeyBytes = null;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class);


//test SM2
algorithm = CryptoAlgorithm.SM2;

keyPair = asymmetricCrypto.generateKeyPair(algorithm);

privKeyBytes = keyPair.getPrivKey().toBytes();
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,null);

truncatedPrivKeyBytes = new byte[privKeyBytes.length-2];
System.arraycopy(privKeyBytes,0,truncatedPrivKeyBytes,0,truncatedPrivKeyBytes.length);
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,truncatedPrivKeyBytes,IllegalArgumentException.class);

privKeyBytesWithWrongAlgCode = privKeyBytes;
privKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);

privKeyBytesWithWrongKeyType = privKeyBytes;
privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);

privKeyBytes = null;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class);

// //test JNIED25519
// algorithm = CryptoAlgorithm.JNIED25519;
//
// keyPair = asymmetricCrypto.generateKeyPair(algorithm);
//
// privKeyBytes = keyPair.getPrivKey().toBytes();
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,null);
//
// truncatedPrivKeyBytes = new byte[privKeyBytes.length-2];
// System.arraycopy(privKeyBytes,0,truncatedPrivKeyBytes,0,truncatedPrivKeyBytes.length);
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,truncatedPrivKeyBytes,IllegalArgumentException.class);
//
// privKeyBytesWithWrongAlgCode = privKeyBytes;
// privKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
//
// privKeyBytesWithWrongKeyType = privKeyBytes;
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);
//
// privKeyBytes = null;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class);
}

private void verifyResolvePrivKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
int expectedPrivKeyLength, byte[] privKeyBytes,Class<?> expectedException){

Exception actualEx = null;

try {
PrivKey privKey = asymmetricCrypto.resolvePrivKey(privKeyBytes);

assertNotNull(privKey);

assertEquals(algorithm, privKey.getAlgorithm());

assertEquals(expectedPrivKeyLength, privKey.getRawKeyBytes().length);

assertArrayEquals(privKeyBytes, privKey.toBytes());

}
catch (Exception e){
actualEx = e;
}

if (expectedException == null) {
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testTryResolvePrivKey() {
}
}

+ 0
- 333
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java View File

@@ -1,333 +0,0 @@
package test.com.jd.blockchain.crypto.hash;

import static org.junit.Assert.*;

import java.util.Random;

import com.jd.blockchain.crypto.smutils.hash.SM3Utils;
import com.jd.blockchain.utils.io.BytesUtils;
import com.jd.blockchain.utils.security.RipeMD160Utils;
import com.jd.blockchain.utils.security.ShaUtils;

import org.junit.Test;

import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.hash.HashCryptography;
import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.crypto.impl.HashCryptographyImpl;

public class HashCryptographyImplTest {

@Test
public void testGetFunction() {
HashCryptography hashCrypto = new HashCryptographyImpl();
Random rand = new Random();
// test SHA256
CryptoAlgorithm algorithm = CryptoAlgorithm.SHA256;
byte[] data = new byte[256];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = new byte[0];
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = new byte[1056];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = null;
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,NullPointerException.class);


// test RIPEMD160
algorithm = CryptoAlgorithm.RIPEMD160;
data=new byte[256];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null);

data = new byte[0];
verifyGetFunction(hashCrypto, algorithm, data, 160/ 8,null);

data = new byte[1056];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null);

data = null;
verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,NullPointerException.class);

// test SM3
algorithm = CryptoAlgorithm.SM3;
data = new byte[256];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = new byte[0];
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = new byte[1056];
rand.nextBytes(data);
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);

data = null;
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,NullPointerException.class);

// test AES
data = new byte[0];
algorithm = CryptoAlgorithm.AES;
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,IllegalArgumentException.class);

// // test JNISHA256
// algorithm = CryptoAlgorithm.JNISHA256;
// data = new byte[256];
// rand.nextBytes(data);
// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);
//
// data = new byte[0];
// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);
//
// data = new byte[1056];
// rand.nextBytes(data);
// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null);
//
// data = null;
// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,IllegalArgumentException.class);
//
// // test JNIRIPEMD160
// algorithm = CryptoAlgorithm.JNIRIPEMD160;
// data=new byte[256];
// rand.nextBytes(data);
// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null);
//
// data = new byte[0];
// verifyGetFunction(hashCrypto, algorithm, data, 160/ 8,null);
//
// data = new byte[1056];
// rand.nextBytes(data);
// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null);
//
// data = null;
// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,IllegalArgumentException.class);
}

private void verifyGetFunction(HashCryptography hashCrypto, CryptoAlgorithm algorithm, byte[] data,
int expectedRawBytes,Class<?> expectedException) {
Exception actualEx = null;
try {
HashFunction hf = hashCrypto.getFunction(algorithm);
assertNotNull(hf);

HashDigest hd = hf.hash(data);

assertEquals(algorithm, hd.getAlgorithm());

assertEquals(expectedRawBytes, hd.getRawDigest().length);

// verify encoding;
byte[] encodedHash = hd.toBytes();
assertEquals(expectedRawBytes + 1, encodedHash.length);


assertEquals(algorithm.CODE, encodedHash[0]);

//verify equals
assertEquals(true, hd.equals(hf.hash(data)));

//verify verify
assertTrue( hf.verify(hd, data));

} catch (Exception e) {
actualEx = e;
}

if(expectedException==null){
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testVerifyHashDigestByteArray() {
HashCryptography hashCrypto = new HashCryptographyImpl();
//test SHA256
byte[] data=new byte[256];
Random rand = new Random();
rand.nextBytes(data);
CryptoAlgorithm algorithm=CryptoAlgorithm.SHA256;
verifyHashDigestByteArray(hashCrypto,algorithm,data,null);
data=null;
verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class);

//test RIPEMD160
algorithm=CryptoAlgorithm.RIPEMD160;
data=new byte[896];
rand.nextBytes(data);
verifyHashDigestByteArray(hashCrypto,algorithm,data,null);
data=null;
verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class);

//test SM3
algorithm=CryptoAlgorithm.SM3;
data=new byte[896];
rand.nextBytes(data);
verifyHashDigestByteArray(hashCrypto,algorithm,data,null);
data=null;
verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class);


//test AES
algorithm=CryptoAlgorithm.AES;
data=new byte[277];
rand.nextBytes(data);
verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class);

// //test JNISHA256
// data=new byte[256];
// rand = new Random();
// rand.nextBytes(data);
// algorithm=CryptoAlgorithm.JNISHA256;
// verifyHashDigestByteArray(hashCrypto,algorithm,data,null);
// data=null;
// verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class);
//
// //test JNIRIPEMD160
// algorithm=CryptoAlgorithm.JNIRIPEMD160;
// data=new byte[896];
// rand.nextBytes(data);
// verifyHashDigestByteArray(hashCrypto,algorithm,data,null);
// data=null;
// verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class);
}

private void verifyHashDigestByteArray(HashCryptography hashCrypto,CryptoAlgorithm algorithm,byte[] data,Class<?> expectedException){
Exception actualEx=null;
try {
HashFunction hf = hashCrypto.getFunction(algorithm);
assertNotNull(hf);
HashDigest hd = hf.hash(data);
hashCrypto.verify(hd,data);
}catch (Exception e)
{
actualEx=e;
}
if (expectedException==null)
{
assertNull(actualEx);
}
else{
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

@Test
public void testResolveHashDigest() {
Random rand = new Random();
HashCryptography hashCrypto = new HashCryptographyImpl();

//test SHA256
CryptoAlgorithm algorithm = CryptoAlgorithm.SHA256;
byte[] data = new byte[256];
rand.nextBytes(data);
byte[] hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes();
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null);

byte[] truncatedHashDigestBytes = new byte[hashDigestBytes.length-2];
System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length);
verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class);

hashDigestBytes = null;
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class);


//test RIPEMD160
algorithm = CryptoAlgorithm.RIPEMD160;
data = new byte[256];
rand.nextBytes(data);
hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes();
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,null);

truncatedHashDigestBytes = new byte[hashDigestBytes.length-2];
System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length);
verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,20+1,IllegalArgumentException.class);

hashDigestBytes = null;
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,NullPointerException.class);


//test SM3
algorithm = CryptoAlgorithm.SM3;
data = new byte[256];
rand.nextBytes(data);
hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes();
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null);

truncatedHashDigestBytes = new byte[hashDigestBytes.length-2];
System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length);
verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class);

hashDigestBytes = null;
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class);


// //test JNISHA256
// algorithm = CryptoAlgorithm.JNISHA256;
// data = new byte[256];
// rand.nextBytes(data);
// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes();
// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null);
//
// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2];
// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length);
// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class);
//
// hashDigestBytes = null;
// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class);
//
// //test JNIRIPEMD160
// algorithm = CryptoAlgorithm.JNIRIPEMD160;
// data = new byte[256];
// rand.nextBytes(data);
// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes();
// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,null);
//
// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2];
// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length);
// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,20+1,IllegalArgumentException.class);
//
// hashDigestBytes = null;
// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,NullPointerException.class);
}

private void verifyResolveHashDigest(CryptoAlgorithm algorithm,HashCryptography
hashCrypto,byte[] hashDigestBytes,int expectedLength,Class<?>expectedException){

Exception actualEx=null;

try {

HashDigest hashDigest=hashCrypto.resolveHashDigest(hashDigestBytes);
assertNotNull(hashDigest);
assertEquals(algorithm,hashDigest.getAlgorithm());
byte[] algBytes = new byte[1];
algBytes[0] = algorithm.CODE;
assertArrayEquals(hashDigestBytes,BytesUtils.concat(algBytes,hashDigest.getRawDigest()));
assertEquals(expectedLength,hashDigestBytes.length);

}catch (Exception e)
{
actualEx = e;
}
if (expectedException==null)
{
assertNull(actualEx);
}
else {
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}
}

Loading…
Cancel
Save