Browse Source

Fix bugs in test case;

tags/1.0.0
huanghaiquan 5 years ago
parent
commit
682add6ed2
12 changed files with 794 additions and 791 deletions
  1. +7
    -7
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java
  2. +19
    -14
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/AsymmtricCryptographyImpl.java
  3. +12
    -14
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/HashCryptographyImpl.java
  4. +130
    -130
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/asymmetric/JNIED25519SignatureFunction.java
  5. +53
    -53
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNIRIPEMD160HashFunction.java
  6. +53
    -53
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/impl/jni/hash/JNISHA256HashFunction.java
  7. +130
    -130
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java
  8. +74
    -74
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java
  9. +123
    -123
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIED25519UtilsTest.java
  10. +111
    -111
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIMBSHA256UtilsTest.java
  11. +41
    -41
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIRIPEMD160UtilsTest.java
  12. +41
    -41
      source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNISHA256UtilsTest.java

+ 7
- 7
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java View File

@@ -15,9 +15,9 @@ public enum CryptoAlgorithm {


SM3(CryptoAlgorithmType.HASH, (byte) 0x03, false, false), SM3(CryptoAlgorithmType.HASH, (byte) 0x03, false, false),


JNISHA256(CryptoAlgorithmType.HASH, (byte) 0x04, false, false),
JNIRIPEMD160(CryptoAlgorithmType.HASH, (byte) 0x05, false, false),
// JNISHA256(CryptoAlgorithmType.HASH, (byte) 0x04, false, false),
//
// JNIRIPEMD160(CryptoAlgorithmType.HASH, (byte) 0x05, false, false),


// 非对称签名/加密算法; // 非对称签名/加密算法;


@@ -41,10 +41,10 @@ public enum CryptoAlgorithm {
*/ */
SM2(CryptoAlgorithmType.ASYMMETRIC, (byte) 0x04, true, true), SM2(CryptoAlgorithmType.ASYMMETRIC, (byte) 0x04, true, true),


/**
* JNIED25519 签名算法;只用于签名,没有加密特性;
*/
JNIED25519(CryptoAlgorithmType.ASYMMETRIC, (byte) 0x05, true, false),
// /**
// * JNIED25519 签名算法;只用于签名,没有加密特性;
// */
// JNIED25519(CryptoAlgorithmType.ASYMMETRIC, (byte) 0x05, true, false),


// 对称加密; // 对称加密;
/** /**


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

@@ -2,9 +2,14 @@ package com.jd.blockchain.crypto.impl;


import com.jd.blockchain.crypto.Ciphertext; import com.jd.blockchain.crypto.Ciphertext;
import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.asymmetric.*;
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.def.asymmetric.ED25519SignatureFunction;
import com.jd.blockchain.crypto.impl.jni.asymmetric.JNIED25519SignatureFunction;
import com.jd.blockchain.crypto.impl.sm.asymmetric.SM2CryptoFunction; import com.jd.blockchain.crypto.impl.sm.asymmetric.SM2CryptoFunction;


public class AsymmtricCryptographyImpl implements AsymmetricCryptography { public class AsymmtricCryptographyImpl implements AsymmetricCryptography {
@@ -13,7 +18,7 @@ public class AsymmtricCryptographyImpl implements AsymmetricCryptography {


private static final SignatureFunction SM2_SIGF = new SM2CryptoFunction(); private static final SignatureFunction SM2_SIGF = new SM2CryptoFunction();


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


private static final AsymmetricEncryptionFunction SM2_ENCF = new SM2CryptoFunction(); private static final AsymmetricEncryptionFunction SM2_ENCF = new SM2CryptoFunction();


@@ -41,8 +46,8 @@ public class AsymmtricCryptographyImpl implements AsymmetricCryptography {
return ED25519_SIGF; return ED25519_SIGF;
case SM2: case SM2:
return SM2_SIGF; return SM2_SIGF;
case JNIED25519:
return JNIED25519_SIGF;
// case JNIED25519:
// return JNIED25519_SIGF;
default: default:
break; break;
} }
@@ -128,9 +133,9 @@ public class AsymmtricCryptographyImpl implements AsymmetricCryptography {
if (SM2_SIGF.supportDigest(digestBytes)){ if (SM2_SIGF.supportDigest(digestBytes)){
return SM2_SIGF.resolveDigest(digestBytes); return SM2_SIGF.resolveDigest(digestBytes);
} }
if (JNIED25519_SIGF.supportDigest(digestBytes)){
return JNIED25519_SIGF.resolveDigest(digestBytes);
}
// if (JNIED25519_SIGF.supportDigest(digestBytes)){
// return JNIED25519_SIGF.resolveDigest(digestBytes);
// }
//否则返回null //否则返回null
return null; return null;
} }
@@ -153,9 +158,9 @@ public class AsymmtricCryptographyImpl implements AsymmetricCryptography {
if (SM2_SIGF.supportPubKey(pubKeyBytes)){ if (SM2_SIGF.supportPubKey(pubKeyBytes)){
return SM2_SIGF.resolvePubKey(pubKeyBytes); return SM2_SIGF.resolvePubKey(pubKeyBytes);
} }
if (JNIED25519_SIGF.supportPubKey(pubKeyBytes)){
return JNIED25519_SIGF.resolvePubKey(pubKeyBytes);
}
// if (JNIED25519_SIGF.supportPubKey(pubKeyBytes)){
// return JNIED25519_SIGF.resolvePubKey(pubKeyBytes);
// }
//遍历非对称加密算法,如果满足,则返回解析结果 //遍历非对称加密算法,如果满足,则返回解析结果
if (SM2_ENCF.supportPubKey(pubKeyBytes)){ if (SM2_ENCF.supportPubKey(pubKeyBytes)){
return SM2_ENCF.resolvePubKey(pubKeyBytes); return SM2_ENCF.resolvePubKey(pubKeyBytes);
@@ -181,9 +186,9 @@ public class AsymmtricCryptographyImpl implements AsymmetricCryptography {
if (SM2_SIGF.supportPrivKey(privKeyBytes)){ if (SM2_SIGF.supportPrivKey(privKeyBytes)){
return SM2_SIGF.resolvePrivKey(privKeyBytes); return SM2_SIGF.resolvePrivKey(privKeyBytes);
} }
if (JNIED25519_SIGF.supportPrivKey(privKeyBytes)){
return JNIED25519_SIGF.resolvePrivKey(privKeyBytes);
}
// if (JNIED25519_SIGF.supportPrivKey(privKeyBytes)){
// return JNIED25519_SIGF.resolvePrivKey(privKeyBytes);
// }
//遍历非对称加密算法,如果满足,则返回解析结果 //遍历非对称加密算法,如果满足,则返回解析结果
if (SM2_ENCF.supportPrivKey(privKeyBytes)){ if (SM2_ENCF.supportPrivKey(privKeyBytes)){
return SM2_ENCF.resolvePrivKey(privKeyBytes); return SM2_ENCF.resolvePrivKey(privKeyBytes);


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

@@ -6,8 +6,6 @@ import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction; import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.crypto.impl.def.hash.RIPEMD160HashFunction; import com.jd.blockchain.crypto.impl.def.hash.RIPEMD160HashFunction;
import com.jd.blockchain.crypto.impl.def.hash.SHA256HashFunction; import com.jd.blockchain.crypto.impl.def.hash.SHA256HashFunction;
import com.jd.blockchain.crypto.impl.jni.hash.JNIRIPEMD160HashFunction;
import com.jd.blockchain.crypto.impl.jni.hash.JNISHA256HashFunction;
import com.jd.blockchain.crypto.impl.sm.hash.SM3HashFunction; import com.jd.blockchain.crypto.impl.sm.hash.SM3HashFunction;


public class HashCryptographyImpl implements HashCryptography { public class HashCryptographyImpl implements HashCryptography {
@@ -16,8 +14,8 @@ public class HashCryptographyImpl implements HashCryptography {
private static final HashFunction RIPEMD160_FUNC = new RIPEMD160HashFunction(); private static final HashFunction RIPEMD160_FUNC = new RIPEMD160HashFunction();
private static final HashFunction SM3_FUNC = new SM3HashFunction(); private static final HashFunction SM3_FUNC = new SM3HashFunction();


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


@Override @Override
public HashFunction getFunction(CryptoAlgorithm algorithm) { public HashFunction getFunction(CryptoAlgorithm algorithm) {
@@ -30,10 +28,10 @@ public class HashCryptographyImpl implements HashCryptography {
return RIPEMD160_FUNC; return RIPEMD160_FUNC;
case SM3: case SM3:
return SM3_FUNC; return SM3_FUNC;
case JNISHA256:
return JNISHA256_FUNC;
case JNIRIPEMD160:
return JNIRIPEMD160_FUNC;
// case JNISHA256:
// return JNISHA256_FUNC;
// case JNIRIPEMD160:
// return JNIRIPEMD160_FUNC;
default: default:
break; break;
} }
@@ -72,12 +70,12 @@ public class HashCryptographyImpl implements HashCryptography {
if (SM3_FUNC.supportHashDigest(digestBytes)) { if (SM3_FUNC.supportHashDigest(digestBytes)) {
return SM3_FUNC.resolveHashDigest(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);
}
// if (JNISHA256_FUNC.supportHashDigest(digestBytes)) {
// return JNISHA256_FUNC.resolveHashDigest(digestBytes);
// }
// if (JNIRIPEMD160_FUNC.supportHashDigest(digestBytes)) {
// return JNIRIPEMD160_FUNC.resolveHashDigest(digestBytes);
// }
//否则返回null //否则返回null
return null; return null;
} }

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

@@ -1,130 +1,130 @@
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));
}
}
//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));
//
// }
//}

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

@@ -1,53 +1,53 @@
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);
}
}
//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);
// }
//}
//

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

@@ -1,53 +1,53 @@
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);
}
}
//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);
// }
//
//}

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

@@ -91,36 +91,36 @@ public class AsymmtricCryptographyImplTest {




//test JNIED25519 //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]);
// 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 @Test
@@ -160,19 +160,19 @@ public class AsymmtricCryptographyImplTest {
verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,65,32,64,NullPointerException.class); 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);
// //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, private void verifyGetSignatureFunction(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, byte[] data,
@@ -296,30 +296,30 @@ public class AsymmtricCryptographyImplTest {
signatureDigestBytes = null; signatureDigestBytes = null;
verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class); 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);
// //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, private void verifyVerify(AsymmetricCryptography asymmetricCrypto,boolean expectedResult,byte[] data,
@@ -620,25 +620,25 @@ public class AsymmtricCryptographyImplTest {
signatureDigestBytes = null; signatureDigestBytes = null;
verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class); 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);
// //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, private void verifyResolveSignatureDigest(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
@@ -730,28 +730,28 @@ public class AsymmtricCryptographyImplTest {
pubKeyBytes = null; pubKeyBytes = null;
verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytes,NullPointerException.class); 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);
// //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, private void verifyResolvePubKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,
@@ -840,28 +840,28 @@ public class AsymmtricCryptographyImplTest {
privKeyBytes = null; privKeyBytes = null;
verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class); 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);
// //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, private void verifyResolvePrivKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm,


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

@@ -77,37 +77,37 @@ public class HashCryptographyImplTest {
algorithm = CryptoAlgorithm.AES; algorithm = CryptoAlgorithm.AES;
verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,IllegalArgumentException.class); 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);
// // 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, private void verifyGetFunction(HashCryptography hashCrypto, CryptoAlgorithm algorithm, byte[] data,
@@ -184,22 +184,22 @@ public class HashCryptographyImplTest {
rand.nextBytes(data); rand.nextBytes(data);
verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class); 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);
// //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){ private void verifyHashDigestByteArray(HashCryptography hashCrypto,CryptoAlgorithm algorithm,byte[] data,Class<?> expectedException){
@@ -273,33 +273,33 @@ public class HashCryptographyImplTest {
verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class); 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);
// //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 private void verifyResolveHashDigest(CryptoAlgorithm algorithm,HashCryptography


+ 123
- 123
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIED25519UtilsTest.java View File

@@ -1,123 +1,123 @@
package test.com.jd.blockchain.crypto.jniutils;
import com.jd.blockchain.crypto.jniutils.asymmetric.JNIED25519Utils;
public class JNIED25519UtilsTest {
/* Program entry function */
public static void main(String args[]) {
byte[] msg = "abc".getBytes();
int i;
int j;
int count = 10000;
long startTS;
long elapsedTS;
byte[] privKey = new byte[32];
byte[] pubKey = new byte[32];
byte[] signature;
JNIED25519Utils ed25519 = new JNIED25519Utils();
System.out.println("=================== Key Generation test ===================");
ed25519.generateKeyPair(privKey,pubKey);
System.out.println("Private Key: ");
for(i = 0; i < privKey.length; i++) {
System.out.print(privKey[i] + " ");
if((i+1)%8 == 0)
System.out.println();
}
System.out.println();
System.out.println("Public Key: ");
for(i = 0; i < pubKey.length; i++) {
System.out.print(pubKey[i] + " ");
if((i+1)%8 == 0)
System.out.println();
}
System.out.println();
System.out.println("=================== Public Key Retrieval test ===================");
byte[] pk;
pk = ed25519.getPubKey(privKey);
System.out.println("Retrieved Public Key: ");
for(i = 0; i < pk.length; i++) {
System.out.print(pk[i] + " ");
if((i+1)%8 == 0)
System.out.println();
}
System.out.println();
System.out.println("=================== Signing test ===================");
signature = ed25519.sign(msg,privKey,pubKey);
System.out.println("Signature: ");
for(i = 0; i < signature.length; i++) {
System.out.print(signature[i] + " ");
if((i+1)%8 == 0)
System.out.println();
}
System.out.println();
System.out.println("=================== Verifying test ===================");
if (ed25519.verify(msg,pubKey,signature))
System.out.println("valid signature");
else System.out.println("invalid signature");
System.out.println("=================== Do ED25519 Key Pair Generation Test ===================");
for (j = 0; j < 5; j++) {
System.out.println("------------- round[" + j + "] --------------");
startTS = System.currentTimeMillis();
for (i = 0; i < count; i++) {
ed25519.generateKeyPair(privKey,pubKey);
}
elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("ED25519 Key Pair Generation: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
System.out.println();
System.out.println("=================== Do ED25519 Public Key Retrieval Test ===================");
for (j = 0; j < 5; j++) {
System.out.println("------------- round[" + j + "] --------------");
startTS = System.currentTimeMillis();
for (i = 0; i < count; i++) {
ed25519.getPubKey(privKey);
}
elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("ED25519 Public Key Retrieval: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
System.out.println();
System.out.println("=================== Do ED25519 Signing Test ===================");
for (j = 0; j < 5; j++) {
System.out.println("------------- round[" + j + "] --------------");
startTS = System.currentTimeMillis();
for (i = 0; i < count; i++) {
ed25519.sign(msg,privKey,pubKey);
}
elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("ED25519 Signing: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
System.out.println();
System.out.println("=================== Do ED25519 Verifying Test ===================");
for (j = 0; j < 5; j++) {
System.out.println("------------- round[" + j + "] --------------");
startTS = System.currentTimeMillis();
for (i = 0; i < count; i++) {
ed25519.verify(msg,pubKey,signature);
}
elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("ED25519 Verifying: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
System.out.println();
}
}
//package test.com.jd.blockchain.crypto.jniutils;
//
//import com.jd.blockchain.crypto.jniutils.asymmetric.JNIED25519Utils;
//
//
//public class JNIED25519UtilsTest {
//
// /* Program entry function */
// public static void main(String args[]) {
//
// byte[] msg = "abc".getBytes();
// int i;
// int j;
// int count = 10000;
//
// long startTS;
// long elapsedTS;
//
// byte[] privKey = new byte[32];
// byte[] pubKey = new byte[32];
// byte[] signature;
//
//
// JNIED25519Utils ed25519 = new JNIED25519Utils();
//
// System.out.println("=================== Key Generation test ===================");
// ed25519.generateKeyPair(privKey,pubKey);
// System.out.println("Private Key: ");
// for(i = 0; i < privKey.length; i++) {
// System.out.print(privKey[i] + " ");
// if((i+1)%8 == 0)
// System.out.println();
// }
// System.out.println();
// System.out.println("Public Key: ");
// for(i = 0; i < pubKey.length; i++) {
// System.out.print(pubKey[i] + " ");
// if((i+1)%8 == 0)
// System.out.println();
// }
// System.out.println();
//
// System.out.println("=================== Public Key Retrieval test ===================");
// byte[] pk;
// pk = ed25519.getPubKey(privKey);
// System.out.println("Retrieved Public Key: ");
// for(i = 0; i < pk.length; i++) {
// System.out.print(pk[i] + " ");
// if((i+1)%8 == 0)
// System.out.println();
// }
// System.out.println();
//
// System.out.println("=================== Signing test ===================");
// signature = ed25519.sign(msg,privKey,pubKey);
// System.out.println("Signature: ");
// for(i = 0; i < signature.length; i++) {
// System.out.print(signature[i] + " ");
// if((i+1)%8 == 0)
// System.out.println();
// }
// System.out.println();
//
// System.out.println("=================== Verifying test ===================");
// if (ed25519.verify(msg,pubKey,signature))
// System.out.println("valid signature");
// else System.out.println("invalid signature");
//
// System.out.println("=================== Do ED25519 Key Pair Generation Test ===================");
//
//
// for (j = 0; j < 5; j++) {
// System.out.println("------------- round[" + j + "] --------------");
// startTS = System.currentTimeMillis();
// for (i = 0; i < count; i++) {
// ed25519.generateKeyPair(privKey,pubKey);
// }
// elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("ED25519 Key Pair Generation: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// System.out.println();
//
// System.out.println("=================== Do ED25519 Public Key Retrieval Test ===================");
// for (j = 0; j < 5; j++) {
// System.out.println("------------- round[" + j + "] --------------");
// startTS = System.currentTimeMillis();
// for (i = 0; i < count; i++) {
// ed25519.getPubKey(privKey);
// }
// elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("ED25519 Public Key Retrieval: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// System.out.println();
//
// System.out.println("=================== Do ED25519 Signing Test ===================");
// for (j = 0; j < 5; j++) {
// System.out.println("------------- round[" + j + "] --------------");
// startTS = System.currentTimeMillis();
// for (i = 0; i < count; i++) {
// ed25519.sign(msg,privKey,pubKey);
// }
// elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("ED25519 Signing: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// System.out.println();
//
// System.out.println("=================== Do ED25519 Verifying Test ===================");
// for (j = 0; j < 5; j++) {
// System.out.println("------------- round[" + j + "] --------------");
// startTS = System.currentTimeMillis();
// for (i = 0; i < count; i++) {
// ed25519.verify(msg,pubKey,signature);
// }
// elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("ED25519 Verifying: Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// System.out.println();
// }
//}

+ 111
- 111
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIMBSHA256UtilsTest.java View File

@@ -1,111 +1,111 @@
package test.com.jd.blockchain.crypto.jniutils;
import com.jd.blockchain.crypto.jniutils.hash.JNIMBSHA256Utils;
public class JNIMBSHA256UtilsTest {
/* Program entry function */
public static void main(String args[]) {
String osName = System.getProperty("os.name").toLowerCase();
if (! osName.contains("linux")) {
return ;
}
byte[] array1 = "abc".getBytes();
byte[] array2 = "abcd".getBytes();
byte[] array3 = "abcde".getBytes();
byte[] array4 = "abcdef".getBytes();
byte[][] arrays = {array1,array2,array3,array4};
JNIMBSHA256Utils mbsha256 = new JNIMBSHA256Utils();
byte[][] results = mbsha256.multiBufferHash(arrays);
System.out.println("JAVA to C : ");
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
System.out.print(arrays[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("C to JAVA : ");
for (int i = 0; i < results.length; i++) {
for (int j = 0; j < results[i].length; j++) {
System.out.print(results[i][j] + " ");
}
System.out.println();
}
System.out.println();
String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
byte[] array = str.getBytes();
int count = 1000000;
byte[][] arraysx4 = {array,array,array,array};
byte[][] arraysx8 = {array,array,array,array,array,array,array,array};
byte[][] arraysx16 = {array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array};
byte[][] arraysx32 = {array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array};
System.out.println("=================== do MBSHA256 hash test in x4===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
mbsha256.multiBufferHash(arraysx4);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*4));
}
System.out.println();
System.out.println();
System.out.println("=================== do MBSHA256 hash test in x8===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
mbsha256.multiBufferHash(arraysx8);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*8));
}
System.out.println();
System.out.println();
System.out.println("=================== do MBSHA256 hash test in x16===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
mbsha256.multiBufferHash(arraysx16);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*16));
}
System.out.println();
System.out.println();
System.out.println("=================== do MBSHA256 hash test in x32===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
mbsha256.multiBufferHash(arraysx32);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*32));
}
}
}
//package test.com.jd.blockchain.crypto.jniutils;
//
//import com.jd.blockchain.crypto.jniutils.hash.JNIMBSHA256Utils;
//
//public class JNIMBSHA256UtilsTest {
// /* Program entry function */
// public static void main(String args[]) {
//
// String osName = System.getProperty("os.name").toLowerCase();
//
// if (! osName.contains("linux")) {
// return ;
// }
//
// byte[] array1 = "abc".getBytes();
// byte[] array2 = "abcd".getBytes();
// byte[] array3 = "abcde".getBytes();
// byte[] array4 = "abcdef".getBytes();
//
// byte[][] arrays = {array1,array2,array3,array4};
// JNIMBSHA256Utils mbsha256 = new JNIMBSHA256Utils();
// byte[][] results = mbsha256.multiBufferHash(arrays);
//
// System.out.println("JAVA to C : ");
// for (int i = 0; i < arrays.length; i++) {
// for (int j = 0; j < arrays[i].length; j++) {
// System.out.print(arrays[i][j] + " ");
// }
// System.out.println();
// }
//
// System.out.println();
//
// System.out.println("C to JAVA : ");
// for (int i = 0; i < results.length; i++) {
// for (int j = 0; j < results[i].length; j++) {
// System.out.print(results[i][j] + " ");
// }
// System.out.println();
// }
//
// System.out.println();
//
// String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
// byte[] array = str.getBytes();
//
//
// int count = 1000000;
//
//
// byte[][] arraysx4 = {array,array,array,array};
// byte[][] arraysx8 = {array,array,array,array,array,array,array,array};
// byte[][] arraysx16 = {array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array};
// byte[][] arraysx32 = {array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array,array};
//
//
// System.out.println("=================== do MBSHA256 hash test in x4===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// mbsha256.multiBufferHash(arraysx4);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*4));
// }
// System.out.println();
// System.out.println();
//
// System.out.println("=================== do MBSHA256 hash test in x8===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// mbsha256.multiBufferHash(arraysx8);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*8));
// }
// System.out.println();
// System.out.println();
//
// System.out.println("=================== do MBSHA256 hash test in x16===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// mbsha256.multiBufferHash(arraysx16);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*16));
// }
// System.out.println();
// System.out.println();
//
// System.out.println("=================== do MBSHA256 hash test in x32===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// mbsha256.multiBufferHash(arraysx32);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f; Total KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS, (count * 1000.00D) / elapsedTS*32));
// }
// }
//}

+ 41
- 41
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNIRIPEMD160UtilsTest.java View File

@@ -1,41 +1,41 @@
package test.com.jd.blockchain.crypto.jniutils;
import com.jd.blockchain.crypto.jniutils.hash.JNIRIPEMD160Utils;
public class JNIRIPEMD160UtilsTest {
/* Program entry function */
public static void main(String args[]) {
byte[] array1 = "abc".getBytes();
byte[] array2;
JNIRIPEMD160Utils ripemd160 = new JNIRIPEMD160Utils();
array2 = ripemd160.hash(array1);
System.out.print("JAVA to C : ");
for (byte anArray1 : array1) {
System.out.print(anArray1 + " ");
}
System.out.println();
System.out.print("C to JAVA : ");
for (byte anArray2 : array2) {
System.out.print(anArray2 + " ");
}
System.out.println();
String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
byte[] array = str.getBytes();
int count = 1000000;
System.out.println("=================== do RIPEMD160 hash test ===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
ripemd160.hash(array);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("RIPEMD160 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
}
}
//package test.com.jd.blockchain.crypto.jniutils;
//
//import com.jd.blockchain.crypto.jniutils.hash.JNIRIPEMD160Utils;
//
//public class JNIRIPEMD160UtilsTest {
//
// /* Program entry function */
// public static void main(String args[]) {
// byte[] array1 = "abc".getBytes();
// byte[] array2;
// JNIRIPEMD160Utils ripemd160 = new JNIRIPEMD160Utils();
// array2 = ripemd160.hash(array1);
//
// System.out.print("JAVA to C : ");
// for (byte anArray1 : array1) {
// System.out.print(anArray1 + " ");
// }
// System.out.println();
// System.out.print("C to JAVA : ");
// for (byte anArray2 : array2) {
// System.out.print(anArray2 + " ");
// }
// System.out.println();
//
// String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
// byte[] array = str.getBytes();
// int count = 1000000;
//
// System.out.println("=================== do RIPEMD160 hash test ===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// ripemd160.hash(array);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("RIPEMD160 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// }
//}

+ 41
- 41
source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/jniutils/JNISHA256UtilsTest.java View File

@@ -1,41 +1,41 @@
package test.com.jd.blockchain.crypto.jniutils;
import com.jd.blockchain.crypto.jniutils.hash.JNISHA256Utils;
public class JNISHA256UtilsTest {
/* Program entry function */
public static void main(String args[]) {
byte[] array1 = "abc".getBytes();
byte[] array2;
JNISHA256Utils sha256 = new JNISHA256Utils();
array2 = sha256.hash(array1);
System.out.print("JAVA to C : ");
for (byte anArray1 : array1) {
System.out.print(anArray1 + " ");
}
System.out.println();
System.out.print("C to JAVA : ");
for (byte anArray2 : array2) {
System.out.print(anArray2 + " ");
}
System.out.println();
String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
byte[] array = str.getBytes();
int count = 1000000;
System.out.println("=================== do SHA256 hash test ===================");
for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------");
long startTS = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
sha256.hash(array);
}
long elapsedTS = System.currentTimeMillis() - startTS;
System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS));
}
}
}
//package test.com.jd.blockchain.crypto.jniutils;
//
//import com.jd.blockchain.crypto.jniutils.hash.JNISHA256Utils;
//
//public class JNISHA256UtilsTest {
//
// /* Program entry function */
// public static void main(String args[]) {
// byte[] array1 = "abc".getBytes();
// byte[] array2;
// JNISHA256Utils sha256 = new JNISHA256Utils();
// array2 = sha256.hash(array1);
// System.out.print("JAVA to C : ");
// for (byte anArray1 : array1) {
// System.out.print(anArray1 + " ");
// }
// System.out.println();
// System.out.print("C to JAVA : ");
// for (byte anArray2 : array2) {
// System.out.print(anArray2 + " ");
// }
// System.out.println();
//
//
// String str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
// byte[] array = str.getBytes();
// int count = 1000000;
//
// System.out.println("=================== do SHA256 hash test ===================");
// for (int r = 0; r < 5; r++) {
// System.out.println("------------- round[" + r + "] --------------");
// long startTS = System.currentTimeMillis();
// for (int i = 0; i < count; i++) {
// sha256.hash(array);
// }
// long elapsedTS = System.currentTimeMillis() - startTS;
// System.out.println(String.format("SHA256 hashing Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS,
// (count * 1000.00D) / elapsedTS));
// }
// }
//}

Loading…
Cancel
Save