@@ -89,10 +89,10 @@ public class ECDSAUtils { | |||
return sign(data,params); | |||
} | |||
public static byte[] sign(byte[] data, byte[] privateKey, SecureRandom random, String ID){ | |||
public static byte[] sign(byte[] data, byte[] privateKey, SecureRandom random){ | |||
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(new BigInteger(1,privateKey), DOMAIN_PARAMS); | |||
CipherParameters params = new ParametersWithID(new ParametersWithRandom(privKey,random),ID.getBytes()); | |||
CipherParameters params = new ParametersWithRandom(privKey,random); | |||
return sign(data,params); | |||
} | |||
@@ -100,16 +100,14 @@ public class ECDSAUtils { | |||
public static byte[] sign(byte[] data, CipherParameters params){ | |||
byte[] hashedMsg = SHA256Utils.hash(data); | |||
return sign(params,hashedMsg); | |||
} | |||
public static byte[] sign(CipherParameters params, byte[] hashedMsg){ | |||
ECDSASigner signer = new ECDSASigner(); | |||
signer.init(true, params); | |||
BigInteger[] signature = signer.generateSignature(hashedMsg); | |||
// // To decode the signature | |||
// ASN1Sequence sig = ASN1Sequence.getInstance(encodedSignature); | |||
// byte[] rBytes = BigIntegerTo32Bytes(ASN1Integer.getInstance(sig.getObjectAt(0)).getValue()); | |||
// byte[] sBytes = BigIntegerTo32Bytes(ASN1Integer.getInstance(sig.getObjectAt(1)).getValue()); | |||
byte[] rBytes = BigIntegerTo32Bytes(signature[0]); | |||
byte[] sBytes = BigIntegerTo32Bytes(signature[1]); | |||
@@ -120,6 +118,7 @@ public class ECDSAUtils { | |||
return result; | |||
} | |||
/** | |||
* verification | |||
* | |||
@@ -139,6 +138,10 @@ public class ECDSAUtils { | |||
public static boolean verify(byte[] data, CipherParameters params, byte[] signature){ | |||
byte[] hashedMsg = SHA256Utils.hash(data); | |||
return verify(params,signature,hashedMsg); | |||
} | |||
public static boolean verify(CipherParameters params, byte[] signature, byte[] hashedMsg){ | |||
byte[] rBytes = new byte[R_SIZE]; | |||
byte[] sBytes = new byte[S_SIZE]; | |||
@@ -153,7 +156,6 @@ public class ECDSAUtils { | |||
return verifier.verifySignature(hashedMsg,r,s); | |||
} | |||
// To convert BigInteger to byte[] whose length is 32 | |||
private static byte[] BigIntegerTo32Bytes(BigInteger b){ | |||
byte[] tmp = b.toByteArray(); | |||
@@ -0,0 +1,25 @@ | |||
package com.jd.blockchain.crypto.utils.classic; | |||
import org.bouncycastle.crypto.digests.RIPEMD160Digest; | |||
/** | |||
* @author zhanglin33 | |||
* @title: RIPEMD160Utils | |||
* @description: RIPEMD160 hash algorithm | |||
* @date 2019-04-10, 16:51 | |||
*/ | |||
public class RIPEMD160Utils { | |||
// The length of RIPEMD160 output is 20 bytes | |||
private static final int RIPEMD160DIGEST_LENGTH = 160 / 8; | |||
public static byte[] hash(byte[] data){ | |||
byte[] result = new byte[RIPEMD160DIGEST_LENGTH]; | |||
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest(); | |||
ripemd160Digest.update(data,0,data.length); | |||
ripemd160Digest.doFinal(result,0); | |||
return result; | |||
} | |||
} |
@@ -1,10 +1,492 @@ | |||
package com.jd.blockchain.crypto.utils.classic; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
import org.bouncycastle.asn1.*; | |||
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; | |||
import org.bouncycastle.asn1.pkcs.RSAPrivateKey; | |||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; | |||
import org.bouncycastle.crypto.*; | |||
import org.bouncycastle.crypto.digests.SHA256Digest; | |||
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator; | |||
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters; | |||
import org.bouncycastle.crypto.params.RSAKeyParameters; | |||
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters; | |||
import org.bouncycastle.crypto.signers.RSADigestSigner; | |||
import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; | |||
import sun.security.rsa.RSAPrivateCrtKeyImpl; | |||
import java.io.IOException; | |||
import java.math.BigInteger; | |||
import java.security.KeyFactory; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.security.SecureRandom; | |||
import java.security.interfaces.RSAPublicKey; | |||
import java.security.spec.InvalidKeySpecException; | |||
import java.security.spec.PKCS8EncodedKeySpec; | |||
import java.security.spec.X509EncodedKeySpec; | |||
/** | |||
* @author zhanglin33 | |||
* @title: RSAUtils | |||
* @description: RSA encryption and signature algorithms | |||
* @description: RSA2048 encryption and signature algorithms with SHA256, | |||
* and keys are output in both PKCS1v2 format and PKCS8 | |||
* @date 2019-03-25, 17:20 | |||
*/ | |||
public class RSAUtils { | |||
public class RSAUtils { | |||
private static final int KEYSIZEBITS = 2048; | |||
private static final int CERTAINTY = 100; | |||
private static final int MODULUS_LENGTH = 2048 / 8; | |||
private static final int PRIVEXP_LENGTH = 2048 / 8; | |||
private static final int P_LENGTH = 1024 / 8; | |||
private static final int Q_LENGTH = 1024 / 8; | |||
private static final int DP_LENGTH = 1024 / 8; | |||
private static final int DQ_LENGTH = 1024 / 8; | |||
private static final int QINV_LENGTH = 1024 / 8; | |||
private static final BigInteger VERSION_2PRIMES = BigInteger.valueOf(0); | |||
private static final AlgorithmIdentifier RSA_ALGORITHM_IDENTIFIER = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE); | |||
//-----------------Key Generation Algorithm----------------- | |||
/** | |||
* key pair generation | |||
* | |||
* @return key pair | |||
*/ | |||
public static AsymmetricCipherKeyPair generateKeyPair(){ | |||
return generateKeyPair(new SecureRandom()); | |||
} | |||
public static AsymmetricCipherKeyPair generateKeyPair(SecureRandom random){ | |||
AsymmetricCipherKeyPairGenerator kpGen = new RSAKeyPairGenerator(); | |||
BigInteger exponent = BigInteger.valueOf(0x11); | |||
kpGen.init(new RSAKeyGenerationParameters(exponent, random, KEYSIZEBITS, CERTAINTY)); | |||
return kpGen.generateKeyPair(); | |||
} | |||
// Retrieve public key in raw keys form | |||
public static byte[] retrievePublicKey(byte[] privateKey) { | |||
RSAPrivateCrtKeyParameters privKey = bytes2PrivKey_RawKey(privateKey); | |||
BigInteger modulus = privKey.getModulus(); | |||
BigInteger exponent = privKey.getPublicExponent(); | |||
RSAKeyParameters pubKey = new RSAKeyParameters(false, modulus, exponent); | |||
return pubKey2Bytes_RawKey(pubKey); | |||
} | |||
//-----------------Digital Signature Algorithm----------------- | |||
/** | |||
* signature generation | |||
* | |||
* @param data data to be signed | |||
* @param privateKey private key | |||
* @return signature | |||
*/ | |||
public static byte[] sign(byte[] data, byte[] privateKey){ | |||
RSAPrivateCrtKeyParameters privKey = bytes2PrivKey_RawKey(privateKey); | |||
return sign(data,privKey); | |||
} | |||
public static byte[] sign(byte[] data, CipherParameters params){ | |||
SHA256Digest digest = new SHA256Digest(); | |||
RSADigestSigner signer = new RSADigestSigner(digest); | |||
signer.init(true, params); | |||
signer.update(data, 0, data.length); | |||
try { | |||
return signer.generateSignature(); | |||
} catch (CryptoException e) { | |||
e.printStackTrace(); | |||
} | |||
return null; | |||
} | |||
/** | |||
* verification | |||
* | |||
* @param data data to be signed | |||
* @param publicKey public key | |||
* @param signature signature to be verified | |||
* @return true or false | |||
*/ | |||
public static boolean verify(byte[] data, byte[] publicKey, byte[] signature){ | |||
RSAKeyParameters pubKey = bytes2PubKey_RawKey(publicKey); | |||
return verify(data,pubKey,signature); | |||
} | |||
public static boolean verify(byte[] data, CipherParameters params, byte[] signature){ | |||
SHA256Digest digest = new SHA256Digest(); | |||
RSADigestSigner signer = new RSADigestSigner(digest); | |||
signer.init(false, params); | |||
signer.update(data, 0, data.length); | |||
return signer.verifySignature(signature); | |||
} | |||
/** | |||
* This outputs the key in PKCS1v2 format. | |||
* RSAPublicKey ::= SEQUENCE { | |||
* modulus INTEGER, -- n | |||
* publicExponent INTEGER, -- e | |||
* } | |||
*/ | |||
public static byte[] pubKey2Bytes_PKCS1(RSAKeyParameters pubKey) | |||
{ | |||
ASN1EncodableVector v = new ASN1EncodableVector(); | |||
v.add(new ASN1Integer(pubKey.getModulus())); | |||
v.add(new ASN1Integer(pubKey.getExponent())); | |||
DERSequence pubKeySequence = new DERSequence(v); | |||
byte[] result; | |||
try { | |||
result = pubKeySequence.getEncoded(ASN1Encoding.DER); | |||
} catch (IOException e) { | |||
throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | |||
} | |||
return result; | |||
} | |||
public static byte[] pubKey2Bytes_PKCS8(RSAKeyParameters pubKey){ | |||
BigInteger modulus = pubKey.getModulus(); | |||
BigInteger exponent = pubKey.getExponent(); | |||
return KeyUtil.getEncodedSubjectPublicKeyInfo(RSA_ALGORITHM_IDENTIFIER, | |||
new org.bouncycastle.asn1.pkcs.RSAPublicKey(modulus, exponent)); | |||
} | |||
public static byte[] pubKey2Bytes_RawKey(RSAKeyParameters pubKey){ | |||
BigInteger modulus = pubKey.getModulus(); | |||
BigInteger exponent = pubKey.getExponent(); | |||
byte[] modulusBytes = new byte[MODULUS_LENGTH]; | |||
byte[] exponentBytes = exponent.toByteArray(); | |||
byte[] encodedModulusBytes = modulus.toByteArray(); | |||
int encodedModulusLength = encodedModulusBytes.length; | |||
if (encodedModulusLength > MODULUS_LENGTH) { | |||
System.arraycopy(encodedModulusBytes,encodedModulusLength - MODULUS_LENGTH, | |||
modulusBytes,0, MODULUS_LENGTH); | |||
} else { | |||
System.arraycopy(encodedModulusBytes,0, | |||
modulusBytes,MODULUS_LENGTH - encodedModulusLength, MODULUS_LENGTH); | |||
} | |||
return BytesUtils.concat(modulusBytes,exponentBytes); | |||
} | |||
public static RSAKeyParameters bytes2PubKey_PKCS1(byte[] pubKeyBytes) { | |||
ASN1Sequence pubKeySequence = ASN1Sequence.getInstance(pubKeyBytes); | |||
BigInteger modulus = ASN1Integer.getInstance(pubKeySequence.getObjectAt(0)).getValue(); | |||
BigInteger exponent = ASN1Integer.getInstance(pubKeySequence.getObjectAt(1)).getValue(); | |||
return new RSAKeyParameters(false, modulus, exponent); | |||
} | |||
public static RSAKeyParameters bytes2PubKey_PKCS8(byte[] pubKeyBytes) { | |||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubKeyBytes); | |||
KeyFactory keyFactory = null; | |||
try { | |||
keyFactory = KeyFactory.getInstance("RSA"); | |||
} catch (NoSuchAlgorithmException e) { | |||
e.printStackTrace(); | |||
} | |||
RSAPublicKey publicKey = null; | |||
try { | |||
assert keyFactory != null; | |||
publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); | |||
} catch (InvalidKeySpecException e) { | |||
e.printStackTrace(); | |||
} | |||
assert publicKey != null; | |||
BigInteger exponent = publicKey.getPublicExponent(); | |||
BigInteger modulus = publicKey.getModulus(); | |||
return new RSAKeyParameters(false,modulus,exponent); | |||
} | |||
public static RSAKeyParameters bytes2PubKey_RawKey(byte[] pubKeyBytes) { | |||
byte[] modulusBytes = new byte[MODULUS_LENGTH]; | |||
byte[] exponentBytes = new byte[pubKeyBytes.length - MODULUS_LENGTH]; | |||
System.arraycopy(pubKeyBytes,0, modulusBytes,0, MODULUS_LENGTH); | |||
System.arraycopy(pubKeyBytes,MODULUS_LENGTH, exponentBytes,0,exponentBytes.length); | |||
BigInteger modulus = new BigInteger(1, modulusBytes); | |||
BigInteger exponent = new BigInteger(1, exponentBytes); | |||
return new RSAKeyParameters(false,modulus,exponent); | |||
} | |||
/** | |||
* This outputs the key in PKCS1v2 format. | |||
* RSAPrivateKey ::= SEQUENCE { | |||
* VERSION_2PRIMES Version, | |||
* modulus INTEGER, -- n | |||
* publicExponent INTEGER, -- e | |||
* privateExponent INTEGER, -- d | |||
* prime1 INTEGER, -- p | |||
* prime2 INTEGER, -- q | |||
* exponent1 INTEGER, -- d mod (p-1) | |||
* exponent2 INTEGER, -- d mod (q-1) | |||
* coefficient INTEGER, -- (inverse of q) mod p | |||
* otherPrimeInfos OtherPrimeInfos OPTIONAL | |||
* } | |||
* | |||
* Version ::= INTEGER { two-prime(0), multi(1) } | |||
* (CONSTRAINED BY {-- version must be multi if otherPrimeInfos present --}) | |||
* | |||
* This routine is written to output PKCS1 version 2.1, private keys. | |||
*/ | |||
public static byte[] privKey2Bytes_PKCS1(RSAPrivateCrtKeyParameters privKey) | |||
{ | |||
ASN1EncodableVector v = new ASN1EncodableVector(); | |||
v.add(new ASN1Integer(VERSION_2PRIMES)); // version | |||
v.add(new ASN1Integer(privKey.getModulus())); | |||
v.add(new ASN1Integer(privKey.getPublicExponent())); | |||
v.add(new ASN1Integer(privKey.getExponent())); | |||
v.add(new ASN1Integer(privKey.getP())); | |||
v.add(new ASN1Integer(privKey.getQ())); | |||
v.add(new ASN1Integer(privKey.getDP())); | |||
v.add(new ASN1Integer(privKey.getDQ())); | |||
v.add(new ASN1Integer(privKey.getQInv())); | |||
DERSequence privKeySequence = new DERSequence(v); | |||
byte[] result; | |||
try { | |||
result = privKeySequence.getEncoded(ASN1Encoding.DER); | |||
} catch (IOException e) { | |||
throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | |||
} | |||
return result; | |||
} | |||
public static byte[] privKey2Bytes_PKCS8(RSAPrivateCrtKeyParameters privKey){ | |||
BigInteger modulus = privKey.getModulus(); | |||
BigInteger pubExp = privKey.getPublicExponent(); | |||
BigInteger privExp = privKey.getExponent(); | |||
BigInteger p = privKey.getP(); | |||
BigInteger q = privKey.getQ(); | |||
BigInteger dP = privKey.getDP(); | |||
BigInteger dQ = privKey.getDQ(); | |||
BigInteger qInv = privKey.getQInv(); | |||
return KeyUtil.getEncodedPrivateKeyInfo(RSA_ALGORITHM_IDENTIFIER, new RSAPrivateKey(modulus, pubExp, privExp, p, q, dP, dQ, qInv)); | |||
} | |||
public static byte[] privKey2Bytes_RawKey(RSAPrivateCrtKeyParameters privKey){ | |||
BigInteger modulus = privKey.getModulus(); | |||
BigInteger pubExp = privKey.getPublicExponent(); | |||
BigInteger privExp = privKey.getExponent(); | |||
BigInteger p = privKey.getP(); | |||
BigInteger q = privKey.getQ(); | |||
BigInteger dP = privKey.getDP(); | |||
BigInteger dQ = privKey.getDQ(); | |||
BigInteger qInv = privKey.getQInv(); | |||
byte[] modulusBytes = new byte[MODULUS_LENGTH]; | |||
byte[] pubExpBytes = pubExp.toByteArray(); | |||
byte[] privExpBytes = new byte[PRIVEXP_LENGTH]; | |||
byte[] pBytes = new byte[P_LENGTH]; | |||
byte[] qBytes = new byte[Q_LENGTH]; | |||
byte[] dPBytes = new byte[DP_LENGTH]; | |||
byte[] dQBytes = new byte[DQ_LENGTH]; | |||
byte[] qInvBytes = new byte[QINV_LENGTH]; | |||
byte[] encodedModulusBytes = modulus.toByteArray(); | |||
byte[] encodedPrivExpBytes = privExp.toByteArray(); | |||
byte[] encodedPBytes = p.toByteArray(); | |||
byte[] encodedQBytes = q.toByteArray(); | |||
byte[] encodedDPBytes = dP.toByteArray(); | |||
byte[] encodedDQBytes = dQ.toByteArray(); | |||
byte[] encodedQInvBytes = qInv.toByteArray(); | |||
int encodedModulusLength = encodedModulusBytes.length; | |||
int encodedPrivExpBytesLength = encodedPrivExpBytes.length; | |||
int encodedPBytesLength = encodedPBytes.length; | |||
int encodedQBytesLength = encodedQBytes.length; | |||
int encodedDPBytesLength = encodedDPBytes.length; | |||
int encodedDQBytesLength = encodedDQBytes.length; | |||
int encodedQInvBytesLength = encodedQInvBytes.length; | |||
if (encodedModulusLength > MODULUS_LENGTH) { | |||
System.arraycopy(encodedModulusBytes,encodedModulusLength - MODULUS_LENGTH, | |||
modulusBytes,0, MODULUS_LENGTH); | |||
} else { | |||
System.arraycopy(encodedModulusBytes,0, | |||
modulusBytes,MODULUS_LENGTH - encodedModulusLength, MODULUS_LENGTH); | |||
} | |||
if (encodedPrivExpBytesLength > PRIVEXP_LENGTH) { | |||
System.arraycopy(encodedPrivExpBytes,encodedPrivExpBytesLength - PRIVEXP_LENGTH, | |||
privExpBytes,0, PRIVEXP_LENGTH); | |||
} else { | |||
System.arraycopy(encodedPrivExpBytes,0, | |||
privExpBytes,PRIVEXP_LENGTH - encodedPrivExpBytesLength, PRIVEXP_LENGTH); | |||
} | |||
if (encodedPBytesLength > P_LENGTH) { | |||
System.arraycopy(encodedPBytes,encodedPBytesLength - P_LENGTH, | |||
pBytes,0, P_LENGTH); | |||
} else { | |||
System.arraycopy(encodedPBytes,0, | |||
pBytes,P_LENGTH - encodedPBytesLength, P_LENGTH); | |||
} | |||
if (encodedQBytesLength > Q_LENGTH) { | |||
System.arraycopy(encodedQBytes,encodedQBytesLength - Q_LENGTH, | |||
qBytes,0, Q_LENGTH); | |||
} else { | |||
System.arraycopy(encodedQBytes,0, | |||
qBytes,Q_LENGTH - encodedQBytesLength, Q_LENGTH); | |||
} | |||
if (encodedDPBytesLength > DP_LENGTH) { | |||
System.arraycopy(encodedDPBytes,encodedDPBytesLength - DP_LENGTH, | |||
dPBytes,0, DP_LENGTH); | |||
} else { | |||
System.arraycopy(encodedDPBytes,0, | |||
dPBytes,DP_LENGTH - encodedDPBytesLength, DP_LENGTH); | |||
} | |||
if (encodedDQBytesLength > DQ_LENGTH) { | |||
System.arraycopy(encodedDQBytes,encodedDQBytesLength - DQ_LENGTH, | |||
dQBytes,0, DQ_LENGTH); | |||
} else { | |||
System.arraycopy(encodedDQBytes,0, | |||
dQBytes,DQ_LENGTH - encodedDQBytesLength, DQ_LENGTH); | |||
} | |||
if (encodedQInvBytesLength > QINV_LENGTH) { | |||
System.arraycopy(encodedQInvBytes,encodedQInvBytesLength - QINV_LENGTH, | |||
qInvBytes,0, QINV_LENGTH); | |||
} else { | |||
System.arraycopy(encodedQInvBytes,0, | |||
qInvBytes,QINV_LENGTH - encodedQInvBytesLength, QINV_LENGTH); | |||
} | |||
return BytesUtils.concat(modulusBytes,pubExpBytes,privExpBytes,pBytes,qBytes,dPBytes,dQBytes,qInvBytes); | |||
} | |||
public static RSAPrivateCrtKeyParameters bytes2PrivKey_PKCS1(byte[] privKeyBytes){ | |||
ASN1Sequence priKeySequence = ASN1Sequence.getInstance(privKeyBytes); | |||
BigInteger modulus = ASN1Integer.getInstance(priKeySequence.getObjectAt(1)).getValue(); | |||
BigInteger pubExp = ASN1Integer.getInstance(priKeySequence.getObjectAt(2)).getValue(); | |||
BigInteger privExp = ASN1Integer.getInstance(priKeySequence.getObjectAt(3)).getValue(); | |||
BigInteger p = ASN1Integer.getInstance(priKeySequence.getObjectAt(4)).getValue(); | |||
BigInteger q = ASN1Integer.getInstance(priKeySequence.getObjectAt(5)).getValue(); | |||
BigInteger dP = ASN1Integer.getInstance(priKeySequence.getObjectAt(6)).getValue(); | |||
BigInteger dQ = ASN1Integer.getInstance(priKeySequence.getObjectAt(7)).getValue(); | |||
BigInteger qInv = ASN1Integer.getInstance(priKeySequence.getObjectAt(8)).getValue(); | |||
return new RSAPrivateCrtKeyParameters(modulus, pubExp, privExp, p, q, dP, dQ, qInv); | |||
} | |||
public static RSAPrivateCrtKeyParameters bytes2PrivKey_PKCS8(byte[] privKeyBytes){ | |||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKeyBytes); | |||
KeyFactory keyFactory = null; | |||
try { | |||
keyFactory = KeyFactory.getInstance("RSA"); | |||
} catch (NoSuchAlgorithmException e) { | |||
e.printStackTrace(); | |||
} | |||
RSAPrivateCrtKeyImpl privateKey = null; | |||
try { | |||
assert keyFactory != null; | |||
privateKey = (RSAPrivateCrtKeyImpl) keyFactory.generatePrivate(keySpec); | |||
} catch (InvalidKeySpecException e) { | |||
e.printStackTrace(); | |||
} | |||
assert privateKey != null; | |||
BigInteger modulus = privateKey.getModulus(); | |||
BigInteger pubExp = privateKey.getPublicExponent(); | |||
BigInteger privExp = privateKey.getPrivateExponent(); | |||
BigInteger p = privateKey.getPrimeP(); | |||
BigInteger q = privateKey.getPrimeQ(); | |||
BigInteger dP = privateKey.getPrimeExponentP(); | |||
BigInteger dQ = privateKey.getPrimeExponentQ(); | |||
BigInteger qInv = privateKey.getCrtCoefficient(); | |||
return new RSAPrivateCrtKeyParameters(modulus, pubExp, privExp, p, q, dP, dQ, qInv); | |||
} | |||
public static RSAPrivateCrtKeyParameters bytes2PrivKey_RawKey(byte[] privKeyBytes){ | |||
byte[] modulusBytes = new byte[MODULUS_LENGTH]; | |||
byte[] pubExpBytes = new byte[privKeyBytes.length - MODULUS_LENGTH - PRIVEXP_LENGTH - P_LENGTH - Q_LENGTH | |||
- DP_LENGTH - DQ_LENGTH - QINV_LENGTH]; | |||
byte[] privExpBytes = new byte[PRIVEXP_LENGTH]; | |||
byte[] pBytes = new byte[P_LENGTH]; | |||
byte[] qBytes = new byte[Q_LENGTH]; | |||
byte[] dPBytes = new byte[DP_LENGTH]; | |||
byte[] dQBytes = new byte[DQ_LENGTH]; | |||
byte[] qInvBytes = new byte[QINV_LENGTH]; | |||
System.arraycopy(privKeyBytes,0, modulusBytes,0, MODULUS_LENGTH); | |||
System.arraycopy(privKeyBytes, MODULUS_LENGTH, pubExpBytes,0,pubExpBytes.length); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length, | |||
privExpBytes,0,PRIVEXP_LENGTH); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length + PRIVEXP_LENGTH, | |||
pBytes,0,P_LENGTH); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length + PRIVEXP_LENGTH + P_LENGTH, | |||
qBytes,0,Q_LENGTH); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length + PRIVEXP_LENGTH + P_LENGTH + | |||
Q_LENGTH, dPBytes,0,DP_LENGTH); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length + PRIVEXP_LENGTH + P_LENGTH + | |||
Q_LENGTH + DP_LENGTH, dQBytes,0,DQ_LENGTH); | |||
System.arraycopy(privKeyBytes,MODULUS_LENGTH + pubExpBytes.length + PRIVEXP_LENGTH + P_LENGTH + | |||
Q_LENGTH + DP_LENGTH + DQ_LENGTH, qInvBytes,0,QINV_LENGTH); | |||
BigInteger modulus = new BigInteger(1, modulusBytes); | |||
BigInteger pubExp = new BigInteger(1, pubExpBytes); | |||
BigInteger privExp = new BigInteger(1, privExpBytes); | |||
BigInteger p = new BigInteger(1, pBytes); | |||
BigInteger q = new BigInteger(1, qBytes); | |||
BigInteger dP = new BigInteger(1, dPBytes); | |||
BigInteger dQ = new BigInteger(1, dQBytes); | |||
BigInteger qInv = new BigInteger(1, qInvBytes); | |||
return new RSAPrivateCrtKeyParameters(modulus, pubExp, privExp, p, q, dP, dQ, qInv); | |||
} | |||
} |
@@ -2,16 +2,20 @@ package test.com.jd.blockchain.crypto.utils.classic; | |||
import com.jd.blockchain.crypto.utils.classic.ECDSAUtils; | |||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; | |||
import org.bouncycastle.crypto.CipherParameters; | |||
import org.bouncycastle.crypto.params.ECDomainParameters; | |||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters; | |||
import org.bouncycastle.crypto.params.ECPublicKeyParameters; | |||
import org.bouncycastle.crypto.params.ParametersWithRandom; | |||
import org.bouncycastle.math.ec.ECMultiplier; | |||
import org.bouncycastle.math.ec.ECPoint; | |||
import org.bouncycastle.math.ec.FixedPointCombMultiplier; | |||
import org.bouncycastle.util.encoders.Hex; | |||
import org.bouncycastle.util.test.FixedSecureRandom; | |||
import org.junit.Test; | |||
import java.math.BigInteger; | |||
import java.security.SecureRandom; | |||
import java.util.Random; | |||
import static org.junit.Assert.*; | |||
@@ -110,6 +114,80 @@ public class ECDSAUtilsTest { | |||
assertEquals("04" + xCoord + yCoord,Hex.toHexString(result).toUpperCase()); | |||
} | |||
@Test | |||
public void checkDeterministicValues(){ | |||
// https://crypto.stackexchange.com/questions/41316/complete-set-of-test-vectors-for-ecdsa-secp256k1 | |||
ECDomainParameters domainParams = ECDSAUtils.getDomainParams(); | |||
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters( | |||
new BigInteger("ebb2c082fd7727890a28ac82f6bdf97bad8de9f5d7c9028692de1a255cad3e0f", 16), | |||
domainParams); | |||
ECPublicKeyParameters pubKey = new ECPublicKeyParameters( | |||
domainParams.getCurve().decodePoint(Hex.decode("04" + | |||
"779dd197a5df977ed2cf6cb31d82d43328b790dc6b3b7d4437a427bd5847dfcd" + | |||
"e94b724a555b6d017bb7607c3e3281daf5b1699d6ef4124975c9237b917d426f")), | |||
domainParams); | |||
byte[] privKeyBytes = BigIntegerTo32Bytes(privKey.getD()); | |||
byte[] pubKeyBytes = ECDSAUtils.retrievePublicKey(privKeyBytes); | |||
assertArrayEquals(pubKeyBytes,pubKey.getQ().getEncoded(false)); | |||
SecureRandom k = new FixedSecureRandom(Hex.decode("49a0d7b786ec9cde0d0721d72804befd06571c974b191efb42ecf322ba9ddd9a")); | |||
CipherParameters params = new ParametersWithRandom(privKey,k); | |||
byte[] hashedMsg = Hex.decode("4b688df40bcedbe641ddb16ff0a1842d9c67ea1c3bf63f3e0471baa664531d1a"); | |||
byte[] signature = ECDSAUtils.sign(params,hashedMsg); | |||
String r = "241097efbf8b63bf145c8961dbdf10c310efbb3b2676bbc0f8b08505c9e2f795"; | |||
String s = "021006b7838609339e8b415a7f9acb1b661828131aef1ecbc7955dfb01f3ca0e"; | |||
assertEquals(Hex.toHexString(signature),r + s); | |||
assertTrue(ECDSAUtils.verify(pubKey,signature,hashedMsg)); | |||
} | |||
@Test | |||
public void performanceTest(){ | |||
int count = 10000; | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = ECDSAUtils.generateKeyPair(); | |||
ECPrivateKeyParameters privKeyParams = (ECPrivateKeyParameters) keyPair.getPrivate(); | |||
ECPublicKeyParameters pubKeyParams = (ECPublicKeyParameters) keyPair.getPublic(); | |||
byte[] signatureDigest = ECDSAUtils.sign(data,privKeyParams); | |||
assertTrue(ECDSAUtils.verify(data,pubKeyParams,signatureDigest)); | |||
System.out.println("=================== do ECDSA sign test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
ECDSAUtils.sign(data,privKeyParams); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("ECDSA Signing Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
System.out.println("=================== do ECDSA verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
ECDSAUtils.verify(data,pubKeyParams,signatureDigest); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("ECDSA Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
} | |||
// To convert BigInteger to byte[] whose length is 32 | |||
private static byte[] BigIntegerTo32Bytes(BigInteger b){ | |||
byte[] tmp = b.toByteArray(); | |||
@@ -122,6 +200,4 @@ public class ECDSAUtilsTest { | |||
} | |||
return result; | |||
} | |||
} |
@@ -108,4 +108,76 @@ public class ED25519UtilsTest { | |||
assertTrue(Ed25519Utils.verify(data,pubKeyBytes,signatureDigest)); | |||
} | |||
@Test | |||
public void performanceTest(){ | |||
int count = 10000; | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = ED25519Utils.generateKeyPair(); | |||
Ed25519PrivateKeyParameters privKeyParams = (Ed25519PrivateKeyParameters) keyPair.getPrivate(); | |||
Ed25519PublicKeyParameters pubKeyParams = (Ed25519PublicKeyParameters) keyPair.getPublic(); | |||
byte[] pubKeyBytes = pubKeyParams.getEncoded(); | |||
byte[] privKeyBytes = privKeyParams.getEncoded(); | |||
byte[] signatureDigest = ED25519Utils.sign(data,privKeyParams); | |||
assertTrue(ED25519Utils.verify(data,pubKeyParams,signatureDigest)); | |||
System.out.println("=================== do ED25519 sign test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
ED25519Utils.sign(data,privKeyParams); | |||
} | |||
long 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("=================== do ED25519 verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
ED25519Utils.verify(data,pubKeyParams,signatureDigest); | |||
} | |||
long 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("=================== do ED25519 sign test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
Ed25519Utils.sign_512(data,privKeyBytes); | |||
} | |||
long 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("=================== do ED25519 verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
Ed25519Utils.verify(data,pubKeyBytes,signatureDigest); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("ED25519 Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
} | |||
} |
@@ -0,0 +1,32 @@ | |||
package test.com.jd.blockchain.crypto.utils.classic; | |||
import com.jd.blockchain.crypto.utils.classic.RIPEMD160Utils; | |||
import org.bouncycastle.util.encoders.Hex; | |||
import org.junit.Test; | |||
import static org.junit.Assert.assertEquals; | |||
/** | |||
* @author zhanglin33 | |||
* @title: RIPEMD160UtilsTest | |||
* @description: Tests for the hash method in RIPEMD160 | |||
* @date 2019-04-10, 16:54 | |||
*/ | |||
public class RIPEMD160UtilsTest { | |||
@Test | |||
public void hashTest() { | |||
byte[] data1 = "a".getBytes(); | |||
byte[] data2 = "abc".getBytes(); | |||
byte[] result1 = RIPEMD160Utils.hash(data1); | |||
byte[] result2 = RIPEMD160Utils.hash(data2); | |||
String respectedResult1 = "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"; | |||
String respectedResult2 = "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"; | |||
assertEquals(respectedResult1, Hex.toHexString(result1)); | |||
assertEquals(respectedResult2, Hex.toHexString(result2)); | |||
} | |||
} |
@@ -0,0 +1,153 @@ | |||
package test.com.jd.blockchain.crypto.utils.classic; | |||
import com.jd.blockchain.crypto.utils.classic.RSAUtils; | |||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; | |||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter; | |||
import org.bouncycastle.crypto.params.RSAKeyParameters; | |||
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters; | |||
import org.junit.Test; | |||
import java.util.Random; | |||
import static org.junit.Assert.*; | |||
/** | |||
* @author zhanglin33 | |||
* @title: RSAUtilsTest | |||
* @description: TODO | |||
* @date 2019-04-11, 17:10 | |||
*/ | |||
public class RSAUtilsTest { | |||
@Test | |||
public void generateKeyPairTest(){ | |||
AsymmetricCipherKeyPair kp = RSAUtils.generateKeyPair(); | |||
RSAKeyParameters pubKey = (RSAKeyParameters) kp.getPublic(); | |||
RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) kp.getPrivate(); | |||
byte[] pubKeyBytes_RawKey = RSAUtils.pubKey2Bytes_RawKey(pubKey); | |||
byte[] pubKeyBytesConverted_RawKey = | |||
RSAUtils.pubKey2Bytes_RawKey(RSAUtils.bytes2PubKey_RawKey(pubKeyBytes_RawKey)); | |||
assertArrayEquals(pubKeyBytes_RawKey,pubKeyBytesConverted_RawKey); | |||
byte[] privKeyBytes_RawKey = RSAUtils.privKey2Bytes_RawKey(privKey); | |||
byte[] privKeyBytesConverted_RawKey = | |||
RSAUtils.privKey2Bytes_RawKey(RSAUtils.bytes2PrivKey_RawKey(privKeyBytes_RawKey)); | |||
assertArrayEquals(privKeyBytes_RawKey,privKeyBytesConverted_RawKey); | |||
byte[] pubKeyBytes_PKCS1 = RSAUtils.pubKey2Bytes_PKCS1(pubKey); | |||
byte[] pubKeyBytesConverted_PKCS1 = | |||
RSAUtils.pubKey2Bytes_PKCS1(RSAUtils.bytes2PubKey_PKCS1(pubKeyBytes_PKCS1)); | |||
assertArrayEquals(pubKeyBytes_PKCS1,pubKeyBytesConverted_PKCS1); | |||
byte[] privKeyBytes_PKCS1 = RSAUtils.privKey2Bytes_PKCS1(privKey); | |||
byte[] privKeyBytesConverted_PKCS1 = | |||
RSAUtils.privKey2Bytes_PKCS1(RSAUtils.bytes2PrivKey_PKCS1(privKeyBytes_PKCS1)); | |||
assertArrayEquals(privKeyBytes_PKCS1,privKeyBytesConverted_PKCS1); | |||
byte[] pubKeyBytes_PKCS8 = RSAUtils.pubKey2Bytes_PKCS8(pubKey); | |||
byte[] pubKeyBytesConverted_PKCS8 = | |||
RSAUtils.pubKey2Bytes_PKCS8(RSAUtils.bytes2PubKey_PKCS8(pubKeyBytes_PKCS8)); | |||
assertArrayEquals(pubKeyBytes_PKCS8,pubKeyBytesConverted_PKCS8); | |||
byte[] privKeyBytes_PKCS8 = RSAUtils.privKey2Bytes_PKCS8(privKey); | |||
byte[] privKeyBytesConverted_PKCS8 = | |||
RSAUtils.privKey2Bytes_PKCS8(RSAUtils.bytes2PrivKey_PKCS8(privKeyBytes_PKCS8)); | |||
assertArrayEquals(privKeyBytes_PKCS8,privKeyBytesConverted_PKCS8); | |||
} | |||
@Test | |||
public void retrievePublicKeyTest(){ | |||
AsymmetricCipherKeyPair kp = RSAUtils.generateKeyPair(); | |||
RSAKeyParameters pubKey = (RSAKeyParameters) kp.getPublic(); | |||
RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) kp.getPrivate(); | |||
byte[] privKeyBytes = RSAUtils.privKey2Bytes_RawKey(privKey); | |||
byte[] pubKeyBytes = RSAUtils.pubKey2Bytes_RawKey(pubKey); | |||
byte[] retrievedPubKeyBytes = RSAUtils.retrievePublicKey(privKeyBytes); | |||
assertArrayEquals(pubKeyBytes,retrievedPubKeyBytes); | |||
} | |||
@Test | |||
public void signTest(){ | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
byte[] privKeyBytes = RSAUtils.privKey2Bytes_RawKey((RSAPrivateCrtKeyParameters) privKey); | |||
byte[] signatureFromPrivKey = RSAUtils.sign(data, privKey); | |||
byte[] signatureFromPrivKeyBytes = RSAUtils.sign(data, privKeyBytes); | |||
assertNotNull(signatureFromPrivKey); | |||
assertEquals(2048 / 8, signatureFromPrivKey.length); | |||
assertArrayEquals(signatureFromPrivKeyBytes,signatureFromPrivKey); | |||
} | |||
@Test | |||
public void verifyTest(){ | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
AsymmetricKeyParameter pubKey = keyPair.getPublic(); | |||
byte[] pubKeyBytes = RSAUtils.pubKey2Bytes_RawKey((RSAKeyParameters) pubKey); | |||
byte[] signature = RSAUtils.sign(data,privKey); | |||
boolean isValidFromPubKey = RSAUtils.verify(data, pubKey, signature); | |||
boolean isValidFromPubKeyBytes = RSAUtils.verify(data, pubKeyBytes, signature); | |||
assertTrue(isValidFromPubKey); | |||
assertTrue(isValidFromPubKeyBytes); | |||
} | |||
@Test | |||
public void performanceTest(){ | |||
int count = 10000; | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
AsymmetricKeyParameter pubKey = keyPair.getPublic(); | |||
byte[] signature = RSAUtils.sign(data,privKey); | |||
System.out.println("=================== do RSA sign test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
RSAUtils.sign(data,privKey); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("RSA Signing Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
System.out.println("=================== do RSA verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
RSAUtils.verify(data,pubKey,signature); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("RSA Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
} | |||
} |
@@ -9,7 +9,7 @@ import static org.junit.Assert.assertEquals; | |||
/** | |||
* @author zhanglin33 | |||
* @title: SHA256UtilsTest | |||
* @description: Tests for the hash method in ECDSAUtils | |||
* @description: Tests for the hash method in SHA256Utils | |||
* @date 2019-04-09, 16:18 | |||
*/ | |||
public class SHA256UtilsTest { | |||
@@ -13,6 +13,8 @@ import org.junit.Test; | |||
import com.jd.blockchain.crypto.utils.sm.SM2Utils; | |||
import java.util.Random; | |||
public class SM2UtilsTest { | |||
@Test | |||
@@ -160,74 +162,44 @@ public class SM2UtilsTest { | |||
// } | |||
// | |||
// | |||
// @Test | |||
// public void signingPerformace(){ | |||
// | |||
// byte[] data = new byte[1000]; | |||
// Random random = new Random(); | |||
// random.nextBytes(data); | |||
// | |||
// int count = 10000; | |||
// | |||
// byte[] sm2Digest = null; | |||
// byte[] ed25519Digest = null; | |||
// | |||
// AsymmetricCipherKeyPair keyPair = SM2Utils.generateKeyPair(); | |||
// ECPublicKeyParameters ecPub = (ECPublicKeyParameters) keyPair.getPublic(); | |||
// ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters) keyPair.getPrivate(); | |||
// | |||
// System.out.println("=================== do SM2 sign test ==================="); | |||
// | |||
// for (int r = 0; r < 5; r++) { | |||
// System.out.println("------------- round[" + r + "] --------------"); | |||
// long startTS = System.currentTimeMillis(); | |||
// for (int i = 0; i < count; i++) { | |||
// sm2Digest = SM2Utils.sign(data,ecPriv); | |||
// } | |||
// long elapsedTS = System.currentTimeMillis() - startTS; | |||
// System.out.println(String.format("SM2 Signing Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
// (count * 1000.00D) / elapsedTS)); | |||
// } | |||
// | |||
// System.out.println("=================== do SM2 verify test ==================="); | |||
// for (int r = 0; r < 5; r++) { | |||
// System.out.println("------------- round[" + r + "] --------------"); | |||
// long startTS = System.currentTimeMillis(); | |||
// for (int i = 0; i < count; i++) { | |||
// SM2Utils.verify(data,ecPub,sm2Digest); | |||
// } | |||
// long elapsedTS = System.currentTimeMillis() - startTS; | |||
// System.out.println(String.format("SM2 Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
// (count * 1000.00D) / elapsedTS)); | |||
// } | |||
// | |||
// KeyPairGenerator keyPairGenerator = new KeyPairGenerator(); | |||
// KeyPair ed25519KeyPair = keyPairGenerator.generateKeyPair(); | |||
// EdDSAPrivateKey privKey = (EdDSAPrivateKey) ed25519KeyPair.getPrivate(); | |||
// EdDSAPublicKey pubKey = (EdDSAPublicKey) ed25519KeyPair.getPublic(); | |||
// | |||
// System.out.println("=================== do ED25519 sign test ==================="); | |||
// for (int r = 0; r < 5; r++) { | |||
// System.out.println("------------- round[" + r + "] --------------"); | |||
// long startTS = System.currentTimeMillis(); | |||
// for (int i = 0; i < count; i++) { | |||
// ed25519Digest = Ed25519Utils.sign_512(data,privKey.getSeed()); | |||
// } | |||
// long 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("=================== do ED25519 verify test ==================="); | |||
// for (int r = 0; r < 5; r++) { | |||
// System.out.println("------------- round[" + r + "] --------------"); | |||
// long startTS = System.currentTimeMillis(); | |||
// for (int i = 0; i < count; i++) { | |||
// Ed25519Utils.verify(data,pubKey.getAbyte(),ed25519Digest); | |||
// } | |||
// long elapsedTS = System.currentTimeMillis() - startTS; | |||
// System.out.println(String.format("ED25519 Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
// (count * 1000.00D) / elapsedTS)); | |||
// } | |||
// } | |||
@Test | |||
public void signingPerformace(){ | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
int count = 10000; | |||
byte[] sm2Digest = null; | |||
AsymmetricCipherKeyPair keyPair = SM2Utils.generateKeyPair(); | |||
ECPublicKeyParameters ecPub = (ECPublicKeyParameters) keyPair.getPublic(); | |||
ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters) keyPair.getPrivate(); | |||
System.out.println("=================== do SM2 sign test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
sm2Digest = SM2Utils.sign(data,ecPriv); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("SM2 Signing Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
System.out.println("=================== do SM2 verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
long startTS = System.currentTimeMillis(); | |||
for (int i = 0; i < count; i++) { | |||
SM2Utils.verify(data,ecPub,sm2Digest); | |||
} | |||
long elapsedTS = System.currentTimeMillis() - startTS; | |||
System.out.println(String.format("SM2 Verifying Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
} | |||
} |
@@ -1,10 +1,19 @@ | |||
package test.my.utils.security; | |||
import static com.jd.blockchain.utils.security.RSAUtils.ALG_RSA; | |||
import static org.junit.Assert.*; | |||
import java.io.UnsupportedEncodingException; | |||
import java.security.KeyPair; | |||
import java.security.KeyPairGenerator; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.security.SecureRandom; | |||
import java.security.interfaces.RSAPrivateKey; | |||
import java.security.interfaces.RSAPublicKey; | |||
import java.util.UUID; | |||
import org.bouncycastle.util.encoders.Base64; | |||
import org.bouncycastle.util.encoders.Hex; | |||
import org.junit.Test; | |||
import com.jd.blockchain.utils.codec.HexUtils; | |||
@@ -57,4 +66,23 @@ public class RSAUtilsTest { | |||
} | |||
@Test | |||
public void generateKeyPairTest() throws NoSuchAlgorithmException { | |||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); | |||
keyPairGen.initialize(2048, new SecureRandom()); | |||
KeyPair keyPair = keyPairGen.generateKeyPair(); | |||
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); | |||
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); | |||
byte[] pubKey = publicKey.getEncoded(); | |||
byte[] privKey = privateKey.getEncoded(); | |||
System.out.println(Base64.toBase64String(pubKey)); | |||
System.out.println(Base64.toBase64String(privKey)); | |||
System.out.println(Hex.toHexString(pubKey)); | |||
System.out.println(Hex.toHexString(privKey)); | |||
} | |||
} |