diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java deleted file mode 100644 index fcfaf36f..00000000 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java +++ /dev/null @@ -1,953 +0,0 @@ -//package test.com.jd.blockchain.crypto.asymmetric; -// -//import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE; -//import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC; -//import static org.junit.Assert.assertArrayEquals; -//import static org.junit.Assert.assertEquals; -//import static org.junit.Assert.assertNotNull; -//import static org.junit.Assert.assertNull; -//import static org.junit.Assert.assertTrue; -// -//import java.util.Random; -// -//import org.junit.Test; -// -//import com.jd.blockchain.crypto.Ciphertext; -//import com.jd.blockchain.crypto.CryptoAlgorithm; -//import com.jd.blockchain.crypto.CryptoException; -//import com.jd.blockchain.crypto.CryptoKeyType; -//import com.jd.blockchain.crypto.PrivKey; -//import com.jd.blockchain.crypto.PubKey; -//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.SignatureDigest; -//import com.jd.blockchain.crypto.asymmetric.SignatureFunction; -//import com.jd.blockchain.crypto.impl.AsymmtricCryptographyImpl; -//import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; -//import com.jd.blockchain.utils.io.BytesUtils; -// -//public class AsymmtricCryptographyImplTest { -// -// @Test -// public void testGenerateKeyPair() { -// -// //test ED25519 -// CryptoAlgorithm algorithm = ClassicCryptoService.ED25519.getAlgorithm(); -// CryptoKeyPair keyPair = ClassicCryptoService.ED25519.generateKeyPair(); -// -// PubKey pubKey = keyPair.getPubKey(); -// PrivKey privKey = keyPair.getPrivKey(); -// -// assertNotNull(pubKey); -// assertNotNull(privKey); -// -// assertEquals(ClassicCryptoService.ED25519.getAlgorithm().code(),pubKey.getAlgorithm().code()); -// assertEquals(ClassicCryptoService.ED25519.getAlgorithm().code(),privKey.getAlgorithm().code()); -// -// assertEquals(32,pubKey.getRawKeyBytes().length); -// assertEquals(32,privKey.getRawKeyBytes().length); -// -// byte[] pubKeyBytes = pubKey.toBytes(); -// byte[] privKeyBytes = privKey.toBytes(); -// -// assertEquals(32+1+1,pubKeyBytes.length); -// assertEquals(32+1+1,privKeyBytes.length); -// -// byte[] algorithmBytes = CryptoAlgorithm.toBytes(ClassicCryptoService.ED25519.getAlgorithm()); -// assertEquals(algorithmBytes[0],pubKeyBytes[0]); -// assertEquals(algorithmBytes[1],pubKeyBytes[1]); -// assertEquals(algorithmBytes[0],privKeyBytes[0]); -// assertEquals(algorithmBytes[1],privKeyBytes[1]); -// -// assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[CryptoAlgorithm.CODE_SIZE]); -// assertEquals(privKey.getKeyType().CODE,privKeyBytes[CryptoAlgorithm.CODE_SIZE]); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// assertNotNull(keyPair); -// -// pubKey = keyPair.getPubKey(); -// privKey = keyPair.getPrivKey(); -// -// assertNotNull(pubKey); -// assertNotNull(privKey); -// -// assertEquals(algorithm,pubKey.getAlgorithm()); -// assertEquals(algorithm,privKey.getAlgorithm()); -// -// assertEquals(65,pubKey.getRawKeyBytes().length); -// assertEquals(32,privKey.getRawKeyBytes().length); -// -// pubKeyBytes = pubKey.toBytes(); -// privKeyBytes = privKey.toBytes(); -// -// assertEquals(32+1+1,privKeyBytes.length); -// assertEquals(65+1+1,pubKeyBytes.length); -// -// assertEquals(CryptoAlgorithm.SM2.CODE,pubKeyBytes[0]); -// assertEquals(CryptoAlgorithm.SM2.CODE,privKeyBytes[0]); -// assertEquals(CryptoAlgorithm.SM2, CryptoAlgorithm.valueOf(pubKey.getAlgorithm().CODE)); -// assertEquals(CryptoAlgorithm.SM2, CryptoAlgorithm.valueOf(privKey.getAlgorithm().CODE)); -// -// assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[1]); -// assertEquals(privKey.getKeyType().CODE,privKeyBytes[1]); -// -// -// //test JNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// assertNotNull(keyPair); -// -// pubKey = keyPair.getPubKey(); -// privKey = keyPair.getPrivKey(); -// -// assertNotNull(pubKey); -// assertNotNull(privKey); -// -// assertEquals(algorithm,pubKey.getAlgorithm()); -// assertEquals(algorithm,privKey.getAlgorithm()); -// -// assertEquals(32,pubKey.getRawKeyBytes().length); -// assertEquals(32,privKey.getRawKeyBytes().length); -// -// pubKeyBytes = pubKey.toBytes(); -// privKeyBytes = privKey.toBytes(); -// -// assertEquals(32+1+1,pubKeyBytes.length); -// assertEquals(32+1+1,privKeyBytes.length); -// -// assertEquals(CryptoAlgorithm.JNIED25519.CODE,pubKeyBytes[0]); -// assertEquals(CryptoAlgorithm.JNIED25519.CODE,privKeyBytes[0]); -// assertEquals(CryptoAlgorithm.JNIED25519, CryptoAlgorithm.valueOf(pubKey.getAlgorithm().CODE)); -// assertEquals(CryptoAlgorithm.JNIED25519, CryptoAlgorithm.valueOf(privKey.getAlgorithm().CODE)); -// -// assertEquals(pubKey.getKeyType().CODE,pubKeyBytes[1]); -// assertEquals(privKey.getKeyType().CODE,privKeyBytes[1]); -// } -// -// @Test -// public void testGetSignatureFunction() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random random = new Random(); -// -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// // 测试256字节的消息进行签名 -// byte[] data = new byte[256]; -// random.nextBytes(data); -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,null); -// -// //错误的算法标识 -// verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,32,32,64,IllegalArgumentException.class); -// -// data = null; -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,NullPointerException.class); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// random.nextBytes(data); -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,65,32,64,null); -// -// //错误的算法标识 -// verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,65,32,64,IllegalArgumentException.class); -// -// data = null; -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,65,32,64,NullPointerException.class); -// -// -// //test JNNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// random.nextBytes(data); -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,null); -// -// //错误的算法标识 -// verifyGetSignatureFunction(asymmetricCrypto,CryptoAlgorithm.AES,data,32,32,64,IllegalArgumentException.class); -// -// data = null; -// verifyGetSignatureFunction(asymmetricCrypto,algorithm,data,32,32,64,IllegalArgumentException.class); -// } -// -// private void verifyGetSignatureFunction(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, byte[] data, -// int expectedPubKeyLength, int expectedPrivKeyLength, -// int expectedSignatureDigestLength, Class expectedException){ -// -// //初始化一个异常 -// Exception actualEx = null; -// -// try { -// SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm); -// -// assertNotNull(sf); -// -// CryptoKeyPair keyPair = sf.generateKeyPair(); -// PubKey pubKey = keyPair.getPubKey(); -// PrivKey privKey = keyPair.getPrivKey(); -// byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); -// byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); -// byte[] pubKeyBytes = pubKey.toBytes(); -// byte[] privKeyBytes = privKey.toBytes(); -// -// assertEquals(algorithm, pubKey.getAlgorithm()); -// assertEquals(algorithm, privKey.getAlgorithm()); -// assertEquals(expectedPubKeyLength,rawPubKeyBytes.length); -// assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length); -// -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUBLIC.CODE},rawPubKeyBytes), pubKeyBytes); -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIVATE.CODE},rawPrivKeyBytes), privKeyBytes); -// -// SignatureDigest signatureDigest = sf.sign(privKey,data); -// byte[] rawDigest = signatureDigest.getRawDigest(); -// -// assertEquals(algorithm,signatureDigest.getAlgorithm()); -// assertEquals(expectedSignatureDigestLength,rawDigest.length); -// byte[] signatureDigestBytes = signatureDigest.toBytes(); -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},rawDigest),signatureDigestBytes); -// -// assertTrue(signatureDigest.equals(signatureDigest)); -// assertEquals(signatureDigest.hashCode(),signatureDigest.hashCode()); -// -// assertTrue(sf.verify(signatureDigest,pubKey,data)); -// -// assertTrue(sf.supportPubKey(pubKeyBytes)); -// assertTrue(sf.supportPrivKey(privKeyBytes)); -// assertTrue(sf.supportDigest(signatureDigestBytes)); -// -// assertEquals(pubKey,sf.resolvePubKey(pubKeyBytes)); -// assertEquals(privKey,sf.resolvePrivKey(privKeyBytes)); -// assertEquals(signatureDigest,sf.resolveDigest(signatureDigestBytes)); -// -// assertEquals(algorithm,sf.getAlgorithm()); -// -// } catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testVerify() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random randomData = new Random(); -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// // 测试256字节的消息进行签名 -// byte[] data = new byte[256]; -// randomData.nextBytes(data); -// SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm); -// CryptoKeyPair keyPair = sf.generateKeyPair(); -// byte[] pubKeyBytes = keyPair.getPubKey().toBytes(); -// -// byte[] signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// byte[] truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// byte[] signatureDigestBytesWithWrongAlgCode = signatureDigestBytes; -// signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// randomData.nextBytes(data); -// sf = asymmetricCrypto.getSignatureFunction(algorithm); -// keyPair = sf.generateKeyPair(); -// pubKeyBytes = keyPair.getPubKey().toBytes(); -// -// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// signatureDigestBytesWithWrongAlgCode = signatureDigestBytes; -// signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class); -// -// //test JNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// randomData.nextBytes(data); -// sf = asymmetricCrypto.getSignatureFunction(algorithm); -// keyPair = sf.generateKeyPair(); -// pubKeyBytes = keyPair.getPubKey().toBytes(); -// -// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyVerify(asymmetricCrypto,true,data,pubKeyBytes,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// signatureDigestBytesWithWrongAlgCode = signatureDigestBytes; -// signatureDigestBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyVerify(asymmetricCrypto,false,data,pubKeyBytes,signatureDigestBytes,NullPointerException.class); -// } -// -// private void verifyVerify(AsymmetricCryptography asymmetricCrypto,boolean expectedResult,byte[] data, -// byte[] pubKeyBytes, byte[] signatureDigestBytes, Class expectedException){ -// -// //初始化一个异常 -// Exception actualEx = null; -// boolean pass = false; -// -// try { -// -// pass = asymmetricCrypto.verify(signatureDigestBytes,pubKeyBytes,data); -// -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// assertEquals(expectedResult, pass); -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testGetAsymmetricEncryptionFunction() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random random = new Random(); -// -// -// //test SM2 -// CryptoAlgorithm algorithm = CryptoAlgorithm.SM2; -// -// //Case 1: SM2Encryption with 16 bytes data -// byte[] data = new byte[16]; -// random.nextBytes(data); -// verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+16+32,data,null); -// -// //Case 2: SM2Encryption with 256 bytes data -// data = new byte[256]; -// random.nextBytes(data); -// verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+256+32,data,null); -// -// //Case 3: SM2Encryption with 1 bytes data -// data = new byte[3]; -// random.nextBytes(data); -// verifyGetAsymmetricEncryptionFunction(asymmetricCrypto, algorithm,65,32,65+3+32,data,null); -// -// //Case 4: SM2Encryption with wrong algorithm -// verifyGetAsymmetricEncryptionFunction(asymmetricCrypto,CryptoAlgorithm.AES,65,32,65+3+32,data,IllegalArgumentException.class); -// -// //Case 5: SM2Encryption with null data -// data = null; -// verifyGetAsymmetricEncryptionFunction(asymmetricCrypto,algorithm,65,32,65+32,data,NullPointerException.class); -// } -// -// private void verifyGetAsymmetricEncryptionFunction(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, -// int expectedPubKeyLength, int expectedPrivKeyLength, -// int expectedCiphertextLength, byte[] data, Class expectedException){ -// -// //初始化一个异常 -// Exception actualEx = null; -// -// try { -// AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm); -// //验证获取的算法实例非空 -// assertNotNull(aef); -// -// CryptoKeyPair keyPair = aef.generateKeyPair(); -// PubKey pubKey = keyPair.getPubKey(); -// PrivKey privKey = keyPair.getPrivKey(); -// byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); -// byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); -// byte[] pubKeyBytes = pubKey.toBytes(); -// byte[] privKeyBytes = privKey.toBytes(); -// -// assertEquals(algorithm, pubKey.getAlgorithm()); -// assertEquals(algorithm, privKey.getAlgorithm()); -// assertEquals(expectedPubKeyLength,rawPubKeyBytes.length); -// assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length); -// -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUBLIC.CODE},rawPubKeyBytes), pubKeyBytes); -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIVATE.CODE},rawPrivKeyBytes), privKeyBytes); -// -// Ciphertext ciphertext = aef.encrypt(pubKey,data); -// byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); -// -// assertEquals(algorithm,ciphertext.getAlgorithm()); -// assertEquals(expectedCiphertextLength,rawCiphertextBytes.length); -// byte[] ciphertextBytes = ciphertext.toBytes(); -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},rawCiphertextBytes),ciphertextBytes); -// -// assertArrayEquals(data,aef.decrypt(privKey,ciphertext)); -// -// assertTrue(aef.supportPubKey(pubKeyBytes)); -// assertTrue(aef.supportPrivKey(privKeyBytes)); -// assertTrue(aef.supportCiphertext(ciphertextBytes)); -// -// assertEquals(pubKey,aef.resolvePubKey(pubKeyBytes)); -// assertEquals(privKey,aef.resolvePrivKey(privKeyBytes)); -// assertEquals(ciphertext,aef.resolveCiphertext(ciphertextBytes)); -// -// assertEquals(algorithm,aef.getAlgorithm()); -// -// -// }catch (Exception e){ -// actualEx = e; -// } -// -// if(expectedException == null){ -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testDecrypt() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random random = new Random(); -// -// byte[] data = new byte[16]; -// random.nextBytes(data); -// -// //test SM2 -// CryptoAlgorithm algorithm = CryptoAlgorithm.SM2; -// AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm); -// CryptoKeyPair keyPair = aef.generateKeyPair(); -// PubKey pubKey = keyPair.getPubKey(); -// PrivKey privKey = keyPair.getPrivKey(); -// byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); -// Ciphertext ciphertext = aef.encrypt(pubKey,data); -// byte[] ciphertextBytes = ciphertext.toBytes(); -// -// verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, ciphertextBytes, null); -// -// //密钥的算法标识与密文的算法标识不一致情况 -// verifyDecrypt(asymmetricCrypto, CryptoAlgorithm.AES, rawPrivKeyBytes, data, ciphertextBytes, IllegalArgumentException.class); -// -// //密文末尾两个字节丢失情况下,抛出异常 -// byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, truncatedCiphertextBytes, com.jd.blockchain.crypto.CryptoException.class); -// -// byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyDecrypt(asymmetricCrypto,algorithm,rawPrivKeyBytes,data,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyDecrypt(asymmetricCrypto,algorithm,rawPrivKeyBytes,data,ciphertextBytes,NullPointerException.class); -// } -// -// private void verifyDecrypt(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, -// byte[] key, byte[] data, byte[] ciphertextBytes, Class expectedException){ -// Exception actualEx = null; -// -// try { -// PrivKey privKey = new PrivKey(algorithm,key); -// -// byte[] plaintext = asymmetricCrypto.decrypt(privKey.toBytes(), ciphertextBytes); -// -// //解密后的明文与初始的明文一致 -// assertArrayEquals(data,plaintext); -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testResolveCiphertext() { -// -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random random = new Random(); -// -// byte[] data = new byte[16]; -// random.nextBytes(data); -// -// //test SM2 -// CryptoAlgorithm algorithm = CryptoAlgorithm.SM2; -// AsymmetricEncryptionFunction aef = asymmetricCrypto.getAsymmetricEncryptionFunction(algorithm); -// CryptoKeyPair keyPair = aef.generateKeyPair(); -// PubKey pubKey = keyPair.getPubKey(); -// PrivKey privKey = keyPair.getPrivKey(); -// byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); -// Ciphertext ciphertext = aef.encrypt(pubKey,data); -// byte[] ciphertextBytes = ciphertext.toBytes(); -// -// verifyResolveCiphertext(asymmetricCrypto, algorithm, ciphertextBytes, null); -// -// -// //密文末尾两个字节丢失情况下,抛出异常 -// byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyDecrypt(asymmetricCrypto, algorithm, rawPrivKeyBytes, data, truncatedCiphertextBytes, CryptoException.class); -// -// byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolveCiphertext(asymmetricCrypto,algorithm,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyResolveCiphertext(asymmetricCrypto,algorithm,ciphertextBytes,NullPointerException.class); -// } -// -// private void verifyResolveCiphertext(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, byte[] ciphertextBytes, -// Class expectedException){ -// Exception actualEx = null; -// -// try { -// -// Ciphertext ciphertext = asymmetricCrypto.resolveCiphertext(ciphertextBytes); -// -// assertNotNull(ciphertext); -// -// assertEquals(algorithm, ciphertext.getAlgorithm()); -// -// assertArrayEquals(ciphertextBytes, ciphertext.toBytes()); -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolveCiphertext() { -// } -// -// @Test -// public void testResolveSignatureDigest() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// Random randomData = new Random(); -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// // 测试256字节的消息进行签名 -// byte[] data = new byte[256]; -// randomData.nextBytes(data); -// SignatureFunction sf = asymmetricCrypto.getSignatureFunction(algorithm); -// CryptoKeyPair keyPair = sf.generateKeyPair(); -// -// byte[] signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// byte[] truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// randomData.nextBytes(data); -// sf = asymmetricCrypto.getSignatureFunction(algorithm); -// keyPair = sf.generateKeyPair(); -// -// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class); -// -// //test JNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// -// // 测试256字节的消息进行签名 -// data = new byte[256]; -// randomData.nextBytes(data); -// sf = asymmetricCrypto.getSignatureFunction(algorithm); -// keyPair = sf.generateKeyPair(); -// -// signatureDigestBytes = sf.sign(keyPair.getPrivKey(),data).toBytes(); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,null); -// -// //签名数据末尾两个字节丢失情况下,抛出异常 -// truncatedSignatureDigestBytes = new byte[signatureDigestBytes.length-2]; -// System.arraycopy(signatureDigestBytes,0,truncatedSignatureDigestBytes,0,truncatedSignatureDigestBytes.length); -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,truncatedSignatureDigestBytes,IllegalArgumentException.class); -// -// signatureDigestBytes = null; -// verifyResolveSignatureDigest(asymmetricCrypto,algorithm,64,signatureDigestBytes,NullPointerException.class); -// } -// -// private void verifyResolveSignatureDigest(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, -// int expectedSignatureDigestLength, -// byte[] signatureDigestBytes, Class expectedException){ -// -// //初始化一个异常 -// Exception actualEx = null; -// -// try { -// -// SignatureDigest signatureDigest = asymmetricCrypto.resolveSignatureDigest(signatureDigestBytes); -// -// assertNotNull(signatureDigest); -// -// assertEquals(algorithm,signatureDigest.getAlgorithm()); -// -// assertEquals(expectedSignatureDigestLength,signatureDigest.getRawDigest().length); -// -// assertArrayEquals(signatureDigestBytes,signatureDigest.toBytes()); -// -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolveSignatureDigest() { -// } -// -// @Test -// public void testRetrievePubKeyBytes() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// byte[] expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); -// byte[] expectedPubKeyBytes = keyPair.getPubKey().toBytes(); -// -// byte[] pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); -// -// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); -// expectedPubKeyBytes = keyPair.getPubKey().toBytes(); -// -// pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); -// -// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); -// -// -// //test JNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); -// expectedPubKeyBytes = keyPair.getPubKey().toBytes(); -// -// pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); -// -// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); -// -// } -// -// -// @Test -// public void testResolvePubKey() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// byte[] pubKeyBytes = keyPair.getPubKey().toBytes(); -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,null); -// -// byte[] truncatedPubKeyBytes = new byte[pubKeyBytes.length-2]; -// System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length); -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,truncatedPubKeyBytes,IllegalArgumentException.class); -// -// byte[] pubKeyBytesWithWrongAlgCode = pubKeyBytes; -// pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// byte[] pubKeyBytesWithWrongKeyType= pubKeyBytes; -// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// pubKeyBytes = null; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,NullPointerException.class); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// pubKeyBytes = keyPair.getPubKey().toBytes(); -// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytes,null); -// -// truncatedPubKeyBytes = new byte[pubKeyBytes.length-2]; -// System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length); -// verifyResolvePubKey(asymmetricCrypto,algorithm,65,truncatedPubKeyBytes,IllegalArgumentException.class); -// -// pubKeyBytesWithWrongAlgCode = pubKeyBytes; -// pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// pubKeyBytesWithWrongKeyType= pubKeyBytes; -// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// pubKeyBytes = null; -// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytes,NullPointerException.class); -// -// //test JNIED25519 -// algorithm = CryptoAlgorithm.JNIED25519; -// -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// pubKeyBytes = keyPair.getPubKey().toBytes(); -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,null); -// -// truncatedPubKeyBytes = new byte[pubKeyBytes.length-2]; -// System.arraycopy(pubKeyBytes,0,truncatedPubKeyBytes,0,truncatedPubKeyBytes.length); -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,truncatedPubKeyBytes,IllegalArgumentException.class); -// -// pubKeyBytesWithWrongAlgCode = pubKeyBytes; -// pubKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// pubKeyBytesWithWrongKeyType= pubKeyBytes; -// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// pubKeyBytes = null; -// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytes,NullPointerException.class); -// } -// -// private void verifyResolvePubKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, -// int expectedPubKeyLength, byte[] pubKeyBytes,Class expectedException){ -// -// Exception actualEx = null; -// -// try { -// PubKey pubKey = asymmetricCrypto.resolvePubKey(pubKeyBytes); -// -// assertNotNull(pubKey); -// -// assertEquals(algorithm, pubKey.getAlgorithm()); -// -// assertEquals(expectedPubKeyLength, pubKey.getRawKeyBytes().length); -// -// assertArrayEquals(pubKeyBytes, pubKey.toBytes()); -// -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolvePubKey() { -// } -// -// @Test -// public void testResolvePrivKey() { -// -// AsymmetricCryptography asymmetricCrypto = new AsymmtricCryptographyImpl(); -// -// //test ED25519 -// CryptoAlgorithm algorithm = CryptoAlgorithm.ED25519; -// -// CryptoKeyPair keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// byte[] privKeyBytes = keyPair.getPrivKey().toBytes(); -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,null); -// -// byte[] truncatedPrivKeyBytes = new byte[privKeyBytes.length-2]; -// System.arraycopy(privKeyBytes,0,truncatedPrivKeyBytes,0,truncatedPrivKeyBytes.length); -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,truncatedPrivKeyBytes,IllegalArgumentException.class); -// -// byte[] privKeyBytesWithWrongAlgCode = privKeyBytes; -// privKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// byte[] privKeyBytesWithWrongKeyType = privKeyBytes; -// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// privKeyBytes = null; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class); -// -// -// //test SM2 -// algorithm = CryptoAlgorithm.SM2; -// -// keyPair = asymmetricCrypto.generateKeyPair(algorithm); -// -// privKeyBytes = keyPair.getPrivKey().toBytes(); -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,null); -// -// truncatedPrivKeyBytes = new byte[privKeyBytes.length-2]; -// System.arraycopy(privKeyBytes,0,truncatedPrivKeyBytes,0,truncatedPrivKeyBytes.length); -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,truncatedPrivKeyBytes,IllegalArgumentException.class); -// -// privKeyBytesWithWrongAlgCode = privKeyBytes; -// privKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// privKeyBytesWithWrongKeyType = privKeyBytes; -// privKeyBytesWithWrongKeyType[1] = PUBLIC.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] = PUBLIC.CODE; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// privKeyBytes = null; -// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytes,NullPointerException.class); -// } -// -// private void verifyResolvePrivKey(AsymmetricCryptography asymmetricCrypto, CryptoAlgorithm algorithm, -// int expectedPrivKeyLength, byte[] privKeyBytes,Class expectedException){ -// -// Exception actualEx = null; -// -// try { -// PrivKey privKey = asymmetricCrypto.resolvePrivKey(privKeyBytes); -// -// assertNotNull(privKey); -// -// assertEquals(algorithm, privKey.getAlgorithm()); -// -// assertEquals(expectedPrivKeyLength, privKey.getRawKeyBytes().length); -// -// assertArrayEquals(privKeyBytes, privKey.toBytes()); -// -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolvePrivKey() { -// } -//} \ No newline at end of file diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java deleted file mode 100644 index 062d0cd9..00000000 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/hash/HashCryptographyImplTest.java +++ /dev/null @@ -1,334 +0,0 @@ -//package test.com.jd.blockchain.crypto.hash; -// -//import static org.junit.Assert.*; -// -//import java.util.Random; -// -//import com.jd.blockchain.crypto.utils.hash.SM3Utils; -//import com.jd.blockchain.utils.io.BytesUtils; -//import com.jd.blockchain.utils.security.RipeMD160Utils; -//import com.jd.blockchain.utils.security.ShaUtils; -// -//import org.junit.Test; -// -//import com.jd.blockchain.crypto.CryptoAlgorithm; -//import com.jd.blockchain.crypto.CryptoUtils; -//import com.jd.blockchain.crypto.hash.HashCryptography; -//import com.jd.blockchain.crypto.hash.HashDigest; -//import com.jd.blockchain.crypto.hash.HashFunction; -//import com.jd.blockchain.crypto.impl.HashCryptographyImpl; -// -//public class HashCryptographyImplTest { -// -// @Test -// public void testGetFunction() { -// HashCryptography hashCrypto = CryptoUtils.hashCrypto(); -// Random rand = new Random(); -// // test SHA256 -// CryptoAlgorithm algorithm = CryptoAlgorithm.SHA256; -// byte[] data = new byte[256]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[0]; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[1056]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = null; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,NullPointerException.class); -// -// -// // test RIPEMD160 -// algorithm = CryptoAlgorithm.RIPEMD160; -// data=new byte[256]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null); -// -// data = new byte[0]; -// verifyGetFunction(hashCrypto, algorithm, data, 160/ 8,null); -// -// data = new byte[1056]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null); -// -// data = null; -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,NullPointerException.class); -// -// // test SM3 -// algorithm = CryptoAlgorithm.SM3; -// data = new byte[256]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[0]; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[1056]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = null; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,NullPointerException.class); -// -// // test AES -// data = new byte[0]; -// algorithm = CryptoAlgorithm.AES; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,IllegalArgumentException.class); -// -// // test JNISHA256 -// algorithm = CryptoAlgorithm.JNISHA256; -// data = new byte[256]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[0]; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = new byte[1056]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,null); -// -// data = null; -// verifyGetFunction(hashCrypto, algorithm, data, 256 / 8,IllegalArgumentException.class); -// -// // test JNIRIPEMD160 -// algorithm = CryptoAlgorithm.JNIRIPEMD160; -// data=new byte[256]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null); -// -// data = new byte[0]; -// verifyGetFunction(hashCrypto, algorithm, data, 160/ 8,null); -// -// data = new byte[1056]; -// rand.nextBytes(data); -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,null); -// -// data = null; -// verifyGetFunction(hashCrypto, algorithm, data, 160 / 8,IllegalArgumentException.class); -// } -// -// private void verifyGetFunction(HashCryptography hashCrypto, CryptoAlgorithm algorithm, byte[] data, -// int expectedRawBytes,Class expectedException) { -// Exception actualEx = null; -// try { -// HashFunction hf = hashCrypto.getFunction(algorithm); -// assertNotNull(hf); -// -// HashDigest hd = hf.hash(data); -// -// assertEquals(algorithm, hd.getAlgorithm()); -// -// assertEquals(expectedRawBytes, hd.getRawDigest().length); -// -// // verify encoding; -// byte[] encodedHash = hd.toBytes(); -// assertEquals(expectedRawBytes + 1, encodedHash.length); -// -// -// assertEquals(algorithm.CODE, encodedHash[0]); -// -// //verify equals -// assertEquals(true, hd.equals(hf.hash(data))); -// -// //verify verify -// assertTrue( hf.verify(hd, data)); -// -// } catch (Exception e) { -// actualEx = e; -// } -// -// if(expectedException==null){ -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testVerifyHashDigestByteArray() { -// HashCryptography hashCrypto = CryptoUtils.hashCrypto(); -// //test SHA256 -// byte[] data=new byte[256]; -// Random rand = new Random(); -// rand.nextBytes(data); -// CryptoAlgorithm algorithm=CryptoAlgorithm.SHA256; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,null); -// data=null; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class); -// -// //test RIPEMD160 -// algorithm=CryptoAlgorithm.RIPEMD160; -// data=new byte[896]; -// rand.nextBytes(data); -// verifyHashDigestByteArray(hashCrypto,algorithm,data,null); -// data=null; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class); -// -// //test SM3 -// algorithm=CryptoAlgorithm.SM3; -// data=new byte[896]; -// rand.nextBytes(data); -// verifyHashDigestByteArray(hashCrypto,algorithm,data,null); -// data=null; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,NullPointerException.class); -// -// -// //test AES -// algorithm=CryptoAlgorithm.AES; -// data=new byte[277]; -// rand.nextBytes(data); -// verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class); -// -// //test JNISHA256 -// data=new byte[256]; -// rand = new Random(); -// rand.nextBytes(data); -// algorithm=CryptoAlgorithm.JNISHA256; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,null); -// data=null; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class); -// -// //test JNIRIPEMD160 -// algorithm=CryptoAlgorithm.JNIRIPEMD160; -// data=new byte[896]; -// rand.nextBytes(data); -// verifyHashDigestByteArray(hashCrypto,algorithm,data,null); -// data=null; -// verifyHashDigestByteArray(hashCrypto,algorithm,data,IllegalArgumentException.class); -// } -// -// private void verifyHashDigestByteArray(HashCryptography hashCrypto,CryptoAlgorithm algorithm,byte[] data,Class expectedException){ -// Exception actualEx=null; -// try { -// HashFunction hf = hashCrypto.getFunction(algorithm); -// assertNotNull(hf); -// HashDigest hd = hf.hash(data); -// hashCrypto.verify(hd,data); -// }catch (Exception e) -// { -// actualEx=e; -// } -// if (expectedException==null) -// { -// assertNull(actualEx); -// } -// else{ -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testResolveHashDigest() { -// Random rand = new Random(); -// HashCryptography hashCrypto = CryptoUtils.hashCrypto(); -// -// //test SHA256 -// CryptoAlgorithm algorithm = CryptoAlgorithm.SHA256; -// byte[] data = new byte[256]; -// rand.nextBytes(data); -// byte[] hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes(); -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null); -// -// byte[] truncatedHashDigestBytes = new byte[hashDigestBytes.length-2]; -// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length); -// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class); -// -// hashDigestBytes = null; -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class); -// -// -// //test RIPEMD160 -// algorithm = CryptoAlgorithm.RIPEMD160; -// data = new byte[256]; -// rand.nextBytes(data); -// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes(); -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,null); -// -// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2]; -// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length); -// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,20+1,IllegalArgumentException.class); -// -// hashDigestBytes = null; -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,NullPointerException.class); -// -// -// //test SM3 -// algorithm = CryptoAlgorithm.SM3; -// data = new byte[256]; -// rand.nextBytes(data); -// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes(); -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null); -// -// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2]; -// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length); -// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class); -// -// hashDigestBytes = null; -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class); -// -// -// //test JNISHA256 -// algorithm = CryptoAlgorithm.JNISHA256; -// data = new byte[256]; -// rand.nextBytes(data); -// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes(); -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,null); -// -// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2]; -// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length); -// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,32+1,IllegalArgumentException.class); -// -// hashDigestBytes = null; -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,32+1,NullPointerException.class); -// -// //test JNIRIPEMD160 -// algorithm = CryptoAlgorithm.JNIRIPEMD160; -// data = new byte[256]; -// rand.nextBytes(data); -// hashDigestBytes = hashCrypto.getFunction(algorithm).hash(data).toBytes(); -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,null); -// -// truncatedHashDigestBytes = new byte[hashDigestBytes.length-2]; -// System.arraycopy(hashDigestBytes,0,truncatedHashDigestBytes,0,truncatedHashDigestBytes.length); -// verifyResolveHashDigest(algorithm, hashCrypto,truncatedHashDigestBytes,20+1,IllegalArgumentException.class); -// -// hashDigestBytes = null; -// verifyResolveHashDigest(algorithm, hashCrypto,hashDigestBytes,20+1,NullPointerException.class); -// } -// -// private void verifyResolveHashDigest(CryptoAlgorithm algorithm,HashCryptography -// hashCrypto,byte[] hashDigestBytes,int expectedLength,ClassexpectedException){ -// -// Exception actualEx=null; -// -// try { -// -// HashDigest hashDigest=hashCrypto.resolveHashDigest(hashDigestBytes); -// assertNotNull(hashDigest); -// assertEquals(algorithm,hashDigest.getAlgorithm()); -// byte[] algBytes = new byte[1]; -// algBytes[0] = algorithm.CODE; -// assertArrayEquals(hashDigestBytes,BytesUtils.concat(algBytes,hashDigest.getRawDigest())); -// assertEquals(expectedLength,hashDigestBytes.length); -// -// }catch (Exception e) -// { -// actualEx = e; -// } -// if (expectedException==null) -// { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// } diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java index 10a16e6b..ec7a1330 100644 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java +++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java @@ -34,6 +34,20 @@ import com.jd.blockchain.utils.io.BytesUtils; */ public class ED25519SignatureFunctionTest { + public static void main(String[] args) { + // Generate and output some public keys for test; + SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); + + AsymmetricKeypair kp1 = signFunc.generateKeypair(); + System.out.println("kp1.pubKey=[" + kp1.getPubKey().toBase58() + "]"); + System.out.println("kp1.privKey=[" + kp1.getPrivKey().toBase58() + "]"); + + AsymmetricKeypair kp2 = signFunc.generateKeypair(); + System.out.println("kp1.pubKey=[" + kp2.getPubKey().toBase58() + "]"); + System.out.println("kp1.privKey=[" + kp2.getPrivKey().toBase58() + "]"); + + } + @Test public void getAlgorithmTest() { diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/symmetric/SymmetricCryptographyImplTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/symmetric/SymmetricCryptographyImplTest.java deleted file mode 100644 index ff0a009e..00000000 --- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/symmetric/SymmetricCryptographyImplTest.java +++ /dev/null @@ -1,471 +0,0 @@ -//package test.com.jd.blockchain.crypto.symmetric; -// -//import com.jd.blockchain.crypto.Ciphertext; -//import com.jd.blockchain.crypto.CryptoAlgorithm; -//import com.jd.blockchain.crypto.SymmetricKey; -//import com.jd.blockchain.crypto.impl.SymmetricCryptographyImpl; -//import com.jd.blockchain.crypto.symmetric.SymmetricCryptography; -//import com.jd.blockchain.crypto.symmetric.SymmetricEncryptionFunction; -//import com.jd.blockchain.utils.io.BytesUtils; -// -//import org.junit.Test; -// -//import java.io.ByteArrayInputStream; -//import java.io.ByteArrayOutputStream; -//import java.io.InputStream; -//import java.io.OutputStream; -//import java.util.Random; -// -//import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE; -//import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC; -//import static org.junit.Assert.*; -// -//public class SymmetricCryptographyImplTest { -// -// @Test -// public void testGenerateKey() { -// -// SymmetricCryptography symmetricCrypto = new SymmetricCryptographyImpl(); -// -// //test AES -// CryptoAlgorithm algorithm = CryptoAlgorithm.AES; -// verifyGenerateKey(symmetricCrypto,algorithm); -// -// //test SM4 -// algorithm = CryptoAlgorithm.SM4; -// verifyGenerateKey(symmetricCrypto,algorithm); -// } -// -// private void verifyGenerateKey(SymmetricCryptography symmetricCrypto, CryptoAlgorithm algorithm){ -// -// SymmetricKey symmetricKey= symmetricCrypto.generateKey(algorithm); -// -// assertNotNull(symmetricKey); -// assertEquals(algorithm, symmetricKey.getAlgorithm()); -// assertEquals(128/8,symmetricKey.getRawKeyBytes().length); -// -// byte[] symmetricKeyBytes = symmetricKey.toBytes(); -// //判断密钥数据长度=算法标识长度+密钥掩码长度+原始密钥长度 -// assertEquals(1 + 1 + 128 / 8, symmetricKeyBytes.length); -// -// assertEquals(algorithm.CODE,symmetricKeyBytes[0]); -// assertEquals(algorithm,CryptoAlgorithm.valueOf(symmetricKeyBytes[0])); -// } -// -// @Test -// public void testGetSymmetricEncryptionFunction() { -// -// SymmetricCryptography symmetricCrypto = new SymmetricCryptographyImpl(); -// Random random = new Random(); -// -// -// //test AES -// CryptoAlgorithm algorithm = CryptoAlgorithm.AES; -// -// //Case 1: AES with 16 bytes data -// //刚好一个分组长度,随机生成明文数据 -// byte[] data = new byte[16]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 2*16, null); -// -// //Case 2: AES with 33 bytes data -// //明文长度大于两倍分组长度,生成的密文是三倍分组长度 -// data = new byte[33]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 3*16,null); -// -// //Case 3: AES with 3 bytes data -// //明文长度小于分组长度,生成的密文是一倍分组长度 -// data = new byte[3]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 16,null); -// -// //Case 4: AES with 0 bytes data -// //明文长度小于分组长度,生成的密文是一倍分组长度 -// data = new byte[0]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 16,null); -// -// //Case 5 AES with null -// //明文为空,可以捕获到异常异常 -// data = null; -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 16,IllegalArgumentException.class); -// -// -// //test ED25519 -// algorithm = CryptoAlgorithm.ED25519; -// data = new byte[16]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 16,IllegalArgumentException.class); -// -// -// //test SM4 -// algorithm = CryptoAlgorithm.SM4; -// -// //Case 1: SM4 with 16 bytes data -// data = new byte[16]; -// random.nextBytes(data); -// //密文长度 = IV长度 + 真实密文长度 -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 3*16, null); -// -// //Case 2: SM4 with 33 bytes data -// data = new byte[33]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 4*16,null); -// -// //Case 3: SM4 with 3 bytes data -// data = new byte[3]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 2*16,null); -// -// //Case 4: SM4 with 0 bytes data -// data = new byte[0]; -// random.nextBytes(data); -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 2*16,null); -// -// //Case 5 SM4 with null -// data = null; -// verifyGetSymmetricEncryptionFunction(symmetricCrypto, algorithm, data, 16,IllegalArgumentException.class); -// } -// -// //不同明文输入下,用来简化加解密过程的method -// private void verifyGetSymmetricEncryptionFunction(SymmetricCryptography symmetricCrypto, CryptoAlgorithm algorithm, -// byte[] data, int expectedCiphertextLength, Class expectedException){ -// -// //初始化一个异常 -// Exception actualEx = null; -// -// try { -// SymmetricEncryptionFunction sef = symmetricCrypto.getSymmetricEncryptionFunction(algorithm); -// //验证获取的算法实例非空 -// assertNotNull(sef); -// -// SymmetricKey symmetricKey = (SymmetricKey) sef.generateSymmetricKey(); -// -// //验证SymmetricKey的getAlgorithm方法 -// assertEquals(algorithm, symmetricKey.getAlgorithm()); -// //验证SymmetricKey的getRawKeyBytes方法 -// assertEquals(16, symmetricKey.getRawKeyBytes().length); -// //验证SymmetricKey的toBytes方法 -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},symmetricKey.getRawKeyBytes()), symmetricKey.toBytes()); -// -// -// Ciphertext ciphertext = sef.encrypt(symmetricKey,data); -// -// //Ciphertext中算法标识与入参算法一致 -// assertEquals(algorithm, ciphertext.getAlgorithm()); -// //验证原始密文长度与预期长度一致 -// assertEquals(expectedCiphertextLength, ciphertext.getRawCiphertext().length); -// //验证密文数据长度=算法标识长度+预期长度 -// byte[] ciphertextBytes = ciphertext.toBytes(); -// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},ciphertext.getRawCiphertext()), ciphertextBytes); -// -// -// //验证equal -// assertTrue(ciphertext.equals(ciphertext)); -// assertEquals(ciphertext.hashCode(),ciphertext.hashCode()); -// -// //验证SymmetricEncryptionFunction的decrypt -// assertArrayEquals(data, sef.decrypt(symmetricKey,ciphertext)); -// -// //测试SymmetricEncryptionFunction的输入输出流的加解密方法 -// InputStream inPlaintext = new ByteArrayInputStream(data); -// //16字节的明文输入,将会产生32字节的密文 -// OutputStream outCiphertext = new ByteArrayOutputStream(ciphertext.toBytes().length); -// InputStream inCiphertext = new ByteArrayInputStream(ciphertext.toBytes()); -// OutputStream outPlaintext = new ByteArrayOutputStream(data.length); -// sef.encrypt(symmetricKey, inPlaintext, outCiphertext); -// sef.decrypt(symmetricKey, inCiphertext, outPlaintext); -// -// //验证SymmetricEncryptionFunction的supportCiphertext方法 -// assertTrue(sef.supportCiphertext(ciphertextBytes)); -// -// //验证SymmetricEncryptionFunction的resolveCiphertext方法 -// assertEquals(ciphertext, sef.resolveCiphertext(ciphertextBytes)); -// -// //验证SymmetricEncryptionFunction的supportSymmetricKey方法 -// assertTrue(sef.supportSymmetricKey(symmetricKey.toBytes())); -// -// //验证SymmetricEncryptionFunction的resolveSymmetricKey方法 -// assertEquals(symmetricKey, sef.resolveSymmetricKey(symmetricKey.toBytes())); -// -// //验证SymmetricEncryptionFunction的getAlgorithm -// assertEquals(algorithm, sef.getAlgorithm()); -// -// } catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testDecrypt() { -// -// SymmetricCryptography symmetricCrypto = new SymmetricCryptographyImpl(); -// Random randomData = new Random(); -// Random randomKey = new Random(); -// -// -// //test AES -// CryptoAlgorithm algorithm = CryptoAlgorithm.AES; -// SymmetricEncryptionFunction sef = symmetricCrypto.getSymmetricEncryptionFunction(algorithm); -// -// byte[] data = new byte[16]; -// randomData.nextBytes(data); -// byte[] key = new byte[16]; -// randomKey.nextBytes(key); -// -// SymmetricKey symmetricKey = new SymmetricKey(algorithm, key); -// byte[] ciphertextBytes = sef.encrypt(symmetricKey,data).toBytes(); -// -// verifyDecrypt(symmetricCrypto, algorithm, key, data, ciphertextBytes, null); -// -// //密钥的算法标识与密文的算法标识不一致情况 -// verifyDecrypt(symmetricCrypto, CryptoAlgorithm.SM4, key, data, ciphertextBytes, IllegalArgumentException.class); -// -// //密文末尾两个字节丢失情况下,抛出异常 -// byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyDecrypt(symmetricCrypto, algorithm, key, data, truncatedCiphertextBytes, IllegalArgumentException.class); -// -// byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyDecrypt(symmetricCrypto,algorithm,key,data,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyDecrypt(symmetricCrypto,algorithm,key,data,ciphertextBytes,NullPointerException.class); -// -// -// //test SM4 -// algorithm = CryptoAlgorithm.SM4; -// sef = symmetricCrypto.getSymmetricEncryptionFunction(algorithm); -// symmetricKey = new SymmetricKey(algorithm, key); -// ciphertextBytes = sef.encrypt(symmetricKey,data).toBytes(); -// -// verifyDecrypt(symmetricCrypto, algorithm, key, data, ciphertextBytes, null); -// -// //密钥的算法标识与密文的算法标识不一致情况 -// verifyDecrypt(symmetricCrypto, CryptoAlgorithm.AES, key, data, ciphertextBytes, IllegalArgumentException.class); -// -// //密文末尾两个字节丢失情况下,抛出异常 -// truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyDecrypt(symmetricCrypto, algorithm, key, data, truncatedCiphertextBytes, IllegalArgumentException.class); -// -// ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyDecrypt(symmetricCrypto,algorithm,key,data,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyDecrypt(symmetricCrypto,algorithm,key,data,ciphertextBytes,NullPointerException.class); -// } -// -// private void verifyDecrypt(SymmetricCryptography symmetricCrypto, CryptoAlgorithm algorithm, -// byte[] key, byte[] data, byte[] ciphertextBytes, Class expectedException) { -// -// Exception actualEx = null; -// -// try { -// SymmetricKey symmetricKey = new SymmetricKey(algorithm,key); -// -// byte[] plaintext = symmetricCrypto.decrypt(symmetricKey.toBytes(), ciphertextBytes); -// -// //解密后的明文与初始的明文一致 -// assertArrayEquals(data,plaintext); -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testResolveCiphertext() { -// -// SymmetricCryptography symmetricCrypto = new SymmetricCryptographyImpl(); -// Random randomData = new Random(); -// Random randomKey = new Random(); -// -// //test AES -// CryptoAlgorithm algorithm = CryptoAlgorithm.AES; -// SymmetricEncryptionFunction sef = symmetricCrypto.getSymmetricEncryptionFunction(algorithm); -// -// byte[] data = new byte[16]; -// randomData.nextBytes(data); -// byte[] key = new byte[16]; -// randomKey.nextBytes(key); -// -// SymmetricKey symmetricKey = new SymmetricKey(algorithm, key); -// byte[] ciphertextBytes = sef.encrypt(symmetricKey,data).toBytes(); -// verifyResolveCiphertext(symmetricCrypto, algorithm, ciphertextBytes, null); -// -// byte[] truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyResolveCiphertext(symmetricCrypto,algorithm,truncatedCiphertextBytes,IllegalArgumentException.class); -// -// byte[] ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolveCiphertext(symmetricCrypto,algorithm,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyResolveCiphertext(symmetricCrypto,algorithm,ciphertextBytes,NullPointerException.class); -// -// -// //test SM4 -// algorithm = CryptoAlgorithm.SM4; -// sef = symmetricCrypto.getSymmetricEncryptionFunction(algorithm); -// -// symmetricKey = new SymmetricKey(algorithm, key); -// ciphertextBytes = sef.encrypt(symmetricKey,data).toBytes(); -// -// verifyResolveCiphertext(symmetricCrypto, algorithm, ciphertextBytes, null); -// -// truncatedCiphertextBytes = new byte[ciphertextBytes.length-2]; -// System.arraycopy(ciphertextBytes,0,truncatedCiphertextBytes,0,truncatedCiphertextBytes.length); -// verifyResolveCiphertext(symmetricCrypto,algorithm,truncatedCiphertextBytes,IllegalArgumentException.class); -// -// ciphertextBytesWithWrongAlgCode = ciphertextBytes; -// ciphertextBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolveCiphertext(symmetricCrypto,algorithm,ciphertextBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// ciphertextBytes = null; -// verifyResolveCiphertext(symmetricCrypto,algorithm,ciphertextBytes,NullPointerException.class); -// } -// -// private void verifyResolveCiphertext(SymmetricCryptography symmetricCrypto, CryptoAlgorithm algorithm, byte[] ciphertextBytes, -// Class expectedException) { -// -// Exception actualEx = null; -// -// try { -// Ciphertext ciphertext = symmetricCrypto.resolveCiphertext(ciphertextBytes); -// -// assertNotNull(ciphertext); -// -// assertEquals(algorithm, ciphertext.getAlgorithm()); -// -// assertEquals(0, ciphertext.getRawCiphertext().length % 16); -// -// assertArrayEquals(ciphertextBytes, ciphertext.toBytes()); -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolveCiphertext() { -// } -// -// -// -// @Test -// public void testResolveSymmetricKey() { -// -// SymmetricCryptography symmetricCrypto = new SymmetricCryptographyImpl(); -// -// //test AES -// CryptoAlgorithm algorithm = CryptoAlgorithm.AES; -// -// Random randomKey = new Random(); -// byte[] key = new byte[16]; -// randomKey.nextBytes(key); -// -// byte[] symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},key); -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null); -// -// byte[] truncatedSymmetricKeyBytes = new byte[symmetricKeyBytes.length-2]; -// System.arraycopy(symmetricKeyBytes,0,truncatedSymmetricKeyBytes,0,truncatedSymmetricKeyBytes.length); -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,truncatedSymmetricKeyBytes,IllegalArgumentException.class); -// -// byte[] symmetricKeyBytesWithWrongAlgCode = symmetricKeyBytes; -// symmetricKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// byte[] symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; -// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); -// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// symmetricKeyBytes = null; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,NullPointerException.class); -// -// -// //test SM4 -// algorithm = CryptoAlgorithm.SM4; -// symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},key); -// -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null); -// -// truncatedSymmetricKeyBytes = new byte[symmetricKeyBytes.length-2]; -// System.arraycopy(symmetricKeyBytes,0,truncatedSymmetricKeyBytes,0,truncatedSymmetricKeyBytes.length); -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,truncatedSymmetricKeyBytes,IllegalArgumentException.class); -// -// symmetricKeyBytesWithWrongAlgCode = symmetricKeyBytes; -// symmetricKeyBytesWithWrongAlgCode[0] = CryptoAlgorithm.SHA256.CODE; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongAlgCode,IllegalArgumentException.class); -// -// symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; -// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); -// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); -// -// symmetricKeyBytes = null; -// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,NullPointerException.class); -// } -// -// private void verifyResolveSymmetricKey(SymmetricCryptography symmetricCrypto, CryptoAlgorithm algorithm, byte[] symmetricKeyBytes, -// Class expectedException) { -// -// Exception actualEx = null; -// -// try { -// SymmetricKey symmetricKey = symmetricCrypto.resolveSymmetricKey(symmetricKeyBytes); -// -// assertNotNull(symmetricKey); -// -// assertEquals(algorithm, symmetricKey.getAlgorithm()); -// -// assertEquals(16, symmetricKey.getRawKeyBytes().length); -// -// assertArrayEquals(symmetricKeyBytes, symmetricKey.toBytes()); -// } -// catch (Exception e){ -// actualEx = e; -// } -// -// if (expectedException == null) { -// assertNull(actualEx); -// } -// else { -// assertNotNull(actualEx); -// assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); -// } -// } -// -// @Test -// public void testTryResolveSymmetricKey() { -// } -//} diff --git a/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java b/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java new file mode 100644 index 00000000..9093ecf4 --- /dev/null +++ b/source/crypto/crypto-framework/src/test/java/test/com/jd/blockchain/crypto/CryptoKeySerializationTest.java @@ -0,0 +1,74 @@ +package test.com.jd.blockchain.crypto; + +import static org.junit.Assert.*; + +import java.util.UUID; + +import org.junit.Test; + +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoAlgorithmDefinition; +import com.jd.blockchain.crypto.CryptoKeyType; +import com.jd.blockchain.crypto.PrivKey; +import com.jd.blockchain.crypto.PubKey; +import com.jd.blockchain.utils.io.BytesUtils; + +public class CryptoKeySerializationTest { + + /** + * Test the equivalence of serialization and deserialization of PubKey; + */ + @Test + public void testPubKey() { + CryptoAlgorithm algorithm = CryptoAlgorithmDefinition.defineSignature("TEST", false, (byte) 123); + + // Simulate a public key with a random number; + byte[] rawBytes = UUID.randomUUID().toString().getBytes(); + + PubKey pubKey = new PubKey(algorithm, rawBytes); + + assertEquals(algorithm.code(), pubKey.getAlgorithm()); + assertEquals(CryptoKeyType.PUBLIC, pubKey.getKeyType()); + + // serialize; + byte[] keyBytes = pubKey.toBytes(); + + // deserialize; + PubKey desPubKey = new PubKey(keyBytes); + + assertEquals(algorithm.code(), desPubKey.getAlgorithm()); + assertEquals(CryptoKeyType.PUBLIC, desPubKey.getKeyType()); + byte[] desRawBytes = desPubKey.getRawKeyBytes(); + assertTrue(BytesUtils.equals(rawBytes, desRawBytes)); + + } + + /** + * Test the equivalence of serialization and deserialization of PrivKey; + */ + @Test + public void testPrivKey() { + CryptoAlgorithm algorithm = CryptoAlgorithmDefinition.defineSignature("TEST", false, (byte) 123); + + // Simulate a public key with a random number; + byte[] rawBytes = UUID.randomUUID().toString().getBytes(); + + PrivKey privKey = new PrivKey(algorithm, rawBytes); + + assertEquals(algorithm.code(), privKey.getAlgorithm()); + assertEquals(CryptoKeyType.PRIVATE, privKey.getKeyType()); + + // serialize; + byte[] keyBytes = privKey.toBytes(); + + // deserialize; + PrivKey desPrivKey = new PrivKey(keyBytes); + + assertEquals(algorithm.code(), desPrivKey.getAlgorithm()); + assertEquals(CryptoKeyType.PRIVATE, desPrivKey.getKeyType()); + byte[] desRawBytes = desPrivKey.getRawKeyBytes(); + assertTrue(BytesUtils.equals(rawBytes, desRawBytes)); + + } + +} diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java index 243be4bc..cc44ed93 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java @@ -30,7 +30,7 @@ public class LedgerInitSettingTest { ConsensusParticipantConfig part0 = setting.getConsensusParticipant(0); assertEquals("jd.com", part0.getName()); assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); - PubKey pubKey0 = KeyGenCommand.decodePubKey("endPsK36koyFr1D245Sa9j83vt6pZUdFBJoJRB3xAsWM6cwhRbna"); + PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu"); assertEquals(pubKey0, part0.getPubKey()); // assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); // assertEquals(8900, part0.getConsensusAddress().getPort()); @@ -41,7 +41,7 @@ public class LedgerInitSettingTest { ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); assertEquals(false, part1.getInitializerAddress().isSecure()); - PubKey pubKey1 = KeyGenCommand.decodePubKey("endPsK36sC5JdPCDPDAXUwZtS3sxEmqEhFcC4whayAsTTh8Z6eoZ"); + PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7Ph1SYLQt9uqVEqiuvNXjxCdGvEdN6otJsg5rbr7Aze7kf"); assertEquals(pubKey1, part1.getPubKey()); ConsensusParticipantConfig part2 = setting.getConsensusParticipant(2); diff --git a/source/test/test-integration/src/test/resources/ledger.init b/source/test/test-integration/src/test/resources/ledger.init index 5bf66ced..a0b6f736 100644 --- a/source/test/test-integration/src/test/resources/ledger.init +++ b/source/test/test-integration/src/test/resources/ledger.init @@ -13,7 +13,7 @@ cons_parti.0.name=jd.com #第0个参与方的公钥文件路径; cons_parti.0.pubkey-path=keys/jd-com.pub #第0个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; -cons_parti.0.pubkey=endPsK36koyFr1D245Sa9j83vt6pZUdFBJoJRB3xAsWM6cwhRbna +cons_parti.0.pubkey=3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu #第0个参与方的共识服务的主机地址; cons_parti.0.consensus.host=127.0.0.1 #第0个参与方的共识服务的端口; @@ -32,7 +32,7 @@ cons_parti.1.name=at.com #第1个参与方的公钥文件路径; cons_parti.1.pubkey-path=keys/at-com.pub #第1个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; -cons_parti.1.pubkey=endPsK36sC5JdPCDPDAXUwZtS3sxEmqEhFcC4whayAsTTh8Z6eoZ +cons_parti.1.pubkey=3snPdw7i7Ph1SYLQt9uqVEqiuvNXjxCdGvEdN6otJsg5rbr7Aze7kf #第1个参与方的共识服务的主机地址; cons_parti.1.consensus.host=127.0.0.1 #第1个参与方的共识服务的端口; @@ -70,7 +70,7 @@ cons_parti.3.name=xt.com #第3个参与方的公钥文件路径; cons_parti.3.pubkey-path=keys/xt-com.pub #第3个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; -cons_parti.3.pubkey=endPsK36nse1dck4uF19zPvAMijCV336Y3zWdgb4rQG8QoRj5ktR +cons_parti.3.pubkey=3snPdw7i7PerZYfRzEB61SAN9tFK4yHm9wUSRtkLSSGXHkQRbB5PkS #第3个参与方的共识服务的主机地址; cons_parti.3.consensus.host=127.0.0.1 #第3个参与方的共识服务的端口; diff --git a/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerBindingConfigTest.java b/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerBindingConfigTest.java index 0deb80c7..3138abf1 100644 --- a/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerBindingConfigTest.java +++ b/source/tools/tools-initializer/src/test/java/test/com/jd/blockchain/tools/initializer/LedgerBindingConfigTest.java @@ -7,34 +7,16 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.UUID; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; import org.junit.Test; import org.springframework.core.io.ClassPathResource; -import com.jd.blockchain.crypto.CryptoServiceProviders; import com.jd.blockchain.crypto.HashDigest; -import com.jd.blockchain.crypto.HashFunction; -import com.jd.blockchain.crypto.RandomFunction; -import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; import com.jd.blockchain.tools.initializer.LedgerBindingConfig; import com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig; -import com.jd.blockchain.utils.codec.Base58Utils; -import com.jd.blockchain.utils.io.BytesUtils; public class LedgerBindingConfigTest { - public static void main(String[] args) { - //生成测试 - HashFunction hashFunc = CryptoServiceProviders.getHashFunction(ClassicAlgorithm.SHA256); - HashDigest hash1 = hashFunc.hash(UUID.randomUUID().toString().getBytes()); - HashDigest hash2 = hashFunc.hash(UUID.randomUUID().toString().getBytes()); - System.out.println("Hash1=[" + hash1.toBase58() + "]"); - System.out.println("Hash1=[" + hash2.toBase58() + "]"); - } - @Test public void testResolveAndStore() throws IOException { ClassPathResource ledgerBindingConfigFile = new ClassPathResource("ledger-binding.conf"); diff --git a/source/tools/tools-keygen-booter/pom.xml b/source/tools/tools-keygen-booter/pom.xml index ce9ef4e2..3b377b11 100644 --- a/source/tools/tools-keygen-booter/pom.xml +++ b/source/tools/tools-keygen-booter/pom.xml @@ -15,6 +15,16 @@ tools-keygen ${project.version} + + com.jd.blockchain + crypto-classic + ${project.version} + + + com.jd.blockchain + crypto-sm + ${project.version} + diff --git a/source/tools/tools-keygen/src/main/java/com/jd/blockchain/tools/keygen/KeyGenCommand.java b/source/tools/tools-keygen/src/main/java/com/jd/blockchain/tools/keygen/KeyGenCommand.java index e9b37976..845d9b6b 100644 --- a/source/tools/tools-keygen/src/main/java/com/jd/blockchain/tools/keygen/KeyGenCommand.java +++ b/source/tools/tools-keygen/src/main/java/com/jd/blockchain/tools/keygen/KeyGenCommand.java @@ -53,15 +53,15 @@ public class KeyGenCommand { * @param args */ public static void main(String[] args) { - Setting setting = ArgumentSet.setting().prefix(READ_ARG, NAME_ARG, OUT_DIR_ARG, LOCAL_CONF_ARG).option(OPT_DECRYPTING, - OPT_DEBUG); + Setting setting = ArgumentSet.setting().prefix(READ_ARG, NAME_ARG, OUT_DIR_ARG, LOCAL_CONF_ARG) + .option(OPT_DECRYPTING, OPT_DEBUG); ArgumentSet argSet = ArgumentSet.resolve(args, setting); try { ArgEntry[] argEntries = argSet.getArgs(); if (argEntries.length == 0) { ConsoleUtils.info("Miss argument!\r\n" - + "-r : in reading mode if set this option, or in generating mode if not set.\r\n" - + "-d : decrypt priv key in reading mode. This is optional.\r\n" + "-n : name of key.\r\n" + + "-r : Run in reading mode if set this option, or in generating mode if not set.\r\n" + + "-d : Decrypt priv key in reading mode, optional.\r\n" + "-n : name of key.\r\n" + "-o : output dir of key under generating mode.\r\n"); return; } @@ -74,18 +74,28 @@ public class KeyGenCommand { ConsoleUtils.info("Miss name of key!"); return; } + String outputDir = null; ArgEntry dirArg = argSet.getArg(OUT_DIR_ARG); if (dirArg == null || dirArg.getValue() == null) { - ConsoleUtils.info("Miss storage dir of keys!"); - return; + // 在当前目录生成; + outputDir = "." + File.separatorChar; + + // ConsoleUtils.info("Miss storage dir of keys!"); + // return; + } else { + outputDir = dirArg.getValue(); } - if (!FileUtils.existDirectory(dirArg.getValue())) { - ConsoleUtils.info("The storage dir doesn't exist!"); - return; + if (!FileUtils.existDirectory(outputDir)) { + // 创建目录; + ConsoleUtils.info( + "The storage dir[" + outputDir + "] doesn't exist, it will be created automatically!"); + FileUtils.makeDirectory(outputDir); +// return; } ArgEntry localConfArg = argSet.getArg(LOCAL_CONF_ARG); String localConfPath = localConfArg == null ? null : localConfArg.getValue(); - generateKeyPair(name.getValue(), dirArg.getValue(), localConfPath); + + generateKeyPair(name.getValue(), outputDir, localConfPath); } } catch (Exception e) { @@ -117,7 +127,8 @@ public class KeyGenCommand { FileUtils.writeText(base58PrivKey, privKeyFile); String base58PwdKey = null; - String savePwdStr = ConsoleUtils.confirm("Do you want to save encode password to file? Please input y or n ..."); + String savePwdStr = ConsoleUtils + .confirm("Do you want to save encode password to file? Please input y or n ..."); if (savePwdStr.equalsIgnoreCase("y") || savePwdStr.equalsIgnoreCase("yes")) { base58PwdKey = Base58Utils.encode(pwdBytes); File pwdFile = new File(outputDir, String.format("%s.pwd", name)); @@ -135,9 +146,9 @@ public class KeyGenCommand { } } if (localConfPath != null) { - File localConf = new File(localConfPath); - if (localConf.exists()) { - try { + File localConf = new File(localConfPath); + if (localConf.exists()) { + try { List configs = org.apache.commons.io.FileUtils.readLines(localConf); List modifyConfigs = new ArrayList<>(); if (configs != null && !configs.isEmpty()) { @@ -157,8 +168,8 @@ public class KeyGenCommand { } catch (Exception e) { System.err.println("Error!!! --[" + e.getClass().getName() + "] " + e.getMessage()); } - } - } + } + } } public static String encodePubKey(PubKey pubKey) { @@ -203,15 +214,12 @@ public class KeyGenCommand { // Try reading pubKey; PubKey pubKey = doDecodePubKeyBytes(keyBytes); ConsoleUtils.info( - "======================== pub key ========================\r\n" - + "[%s]\r\n" + "======================== pub key ========================\r\n" + "[%s]\r\n" + "Raw:[%s][%s]\r\n", - base58KeyString, pubKey.getAlgorithm(), Base58Utils.encode(pubKey.toBytes())); - }else { - ConsoleUtils.info( - "======================== pub key ========================\r\n" - + "[%s]\r\n", - base58KeyString); + base58KeyString, pubKey.getAlgorithm(), Base58Utils.encode(pubKey.toBytes())); + } else { + ConsoleUtils.info("======================== pub key ========================\r\n" + "[%s]\r\n", + base58KeyString); } return; } else if (BytesUtils.startsWith(keyBytes, PRIV_KEY_FILE_MAGICNUM)) { @@ -220,9 +228,9 @@ public class KeyGenCommand { if (decrypting) { byte[] pwdBytes = readPassword(); PrivKey privKey = decryptedPrivKeyBytes(keyBytes, pwdBytes); - ConsoleUtils.info("======================== priv key ========================\r\n" - + "[%s]\r\n" - + "Raw:[%s][%s]\r\n", + ConsoleUtils.info( + "======================== priv key ========================\r\n" + "[%s]\r\n" + + "Raw:[%s][%s]\r\n", base58KeyString, privKey.getAlgorithm(), Base58Utils.encode(privKey.toBytes())); } else { ConsoleUtils.info("======================== priv key ========================\r\n[%s]\r\n", @@ -268,7 +276,7 @@ public class KeyGenCommand { return doDecodePubKeyBytes(encodedPubKeyBytes); } - throw new IllegalArgumentException("The specified file is not a pub key file!"); + throw new IllegalArgumentException("The specified bytes is not valid PubKey generated by the KeyGen tool!"); } /** @@ -314,7 +322,7 @@ public class KeyGenCommand { public static PrivKey readPrivKey(String keyFile, String base58Pwd) { return readPrivKey(keyFile, Base58Utils.decode(base58Pwd)); } - + /** * 从文件读取私钥; * @@ -330,12 +338,12 @@ public class KeyGenCommand { } return decryptedPrivKeyBytes(keyBytes, pwdBytes); } - + public static PrivKey decodePrivKey(String base58Key, String base58Pwd) { byte[] decryptedKey = Base58Utils.decode(base58Pwd); return decodePrivKey(base58Key, decryptedKey); } - + public static PrivKey decodePrivKey(String base58Key, byte[] pwdBytes) { byte[] keyBytes = Base58Utils.decode(base58Key); if (!BytesUtils.startsWith(keyBytes, PRIV_KEY_FILE_MAGICNUM)) { @@ -343,7 +351,7 @@ public class KeyGenCommand { } return decryptedPrivKeyBytes(keyBytes, pwdBytes); } - + public static PrivKey decodePrivKeyWithRawPassword(String base58Key, String rawPassword) { byte[] pwdBytes = encodePassword(rawPassword); byte[] keyBytes = Base58Utils.decode(base58Key);