| @@ -1,11 +1,31 @@ | |||
| package com.jd.blockchain.crypto.utils.classic; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import org.bouncycastle.asn1.*; | |||
| import java.io.IOException; | |||
| import java.math.BigInteger; | |||
| import java.security.KeyFactory; | |||
| import java.security.NoSuchAlgorithmException; | |||
| import java.security.SecureRandom; | |||
| import java.security.interfaces.RSAPrivateCrtKey; | |||
| import java.security.interfaces.RSAPublicKey; | |||
| import java.security.spec.InvalidKeySpecException; | |||
| import java.security.spec.PKCS8EncodedKeySpec; | |||
| import java.security.spec.X509EncodedKeySpec; | |||
| import org.bouncycastle.asn1.ASN1EncodableVector; | |||
| import org.bouncycastle.asn1.ASN1Encoding; | |||
| import org.bouncycastle.asn1.ASN1Integer; | |||
| import org.bouncycastle.asn1.ASN1Sequence; | |||
| import org.bouncycastle.asn1.DERNull; | |||
| import org.bouncycastle.asn1.DERSequence; | |||
| 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.AsymmetricBlockCipher; | |||
| import org.bouncycastle.crypto.AsymmetricCipherKeyPair; | |||
| import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator; | |||
| import org.bouncycastle.crypto.CipherParameters; | |||
| import org.bouncycastle.crypto.CryptoException; | |||
| import org.bouncycastle.crypto.InvalidCipherTextException; | |||
| import org.bouncycastle.crypto.digests.SHA256Digest; | |||
| import org.bouncycastle.crypto.encodings.PKCS1Encoding; | |||
| import org.bouncycastle.crypto.engines.RSAEngine; | |||
| @@ -16,17 +36,8 @@ 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; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| /** | |||
| * @author zhanglin33 | |||
| @@ -442,9 +453,9 @@ public class RSAUtils { | |||
| throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | |||
| } | |||
| RSAPrivateCrtKeyImpl privateKey; | |||
| RSAPrivateCrtKey privateKey; | |||
| try { | |||
| privateKey = (RSAPrivateCrtKeyImpl) keyFactory.generatePrivate(keySpec); | |||
| privateKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec); | |||
| } catch (InvalidKeySpecException e) { | |||
| throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | |||
| } | |||
| @@ -69,7 +69,7 @@ public class AESEncryptionFunctionTest { | |||
| assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | |||
| assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] keyTypeBytes = new byte[] { SYMMETRIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | |||
| @@ -167,7 +167,7 @@ public class AESEncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -197,7 +197,7 @@ public class AESEncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -235,7 +235,7 @@ public class AESEncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||
| byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -275,7 +275,7 @@ public class AESEncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
| byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -66,7 +66,7 @@ public class ECDSASignatureFunctionTest { | |||
| assertEquals(2 + 1 + 65, pubKey.toBytes().length); | |||
| assertEquals(2 + 1 + 32, privKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | |||
| @@ -162,7 +162,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -194,7 +194,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -227,7 +227,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -259,7 +259,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -298,7 +298,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.toBytes(); | |||
| byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -335,7 +335,7 @@ public class ECDSASignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.getRawDigest(); | |||
| byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -93,7 +93,7 @@ public class ED25519SignatureFunctionTest { | |||
| assertEquals(2 + 1 + 32, pubKey.toBytes().length); | |||
| assertEquals(2 + 1 + 32, privKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | |||
| @@ -189,7 +189,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -221,7 +221,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -254,7 +254,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -286,7 +286,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -325,7 +325,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.toBytes(); | |||
| byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -362,7 +362,7 @@ public class ED25519SignatureFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.getRawDigest(); | |||
| byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -57,7 +57,7 @@ public class RIPEMD160HashFunctionTest { | |||
| HashDigest digest = hashFunction.hash(data); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] digestBytes = digest.toBytes(); | |||
| assertEquals(160 / 8 + 2, digestBytes.length); | |||
| @@ -111,7 +111,7 @@ public class RIPEMD160HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("aes"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | |||
| assertFalse(hashFunction.supportHashDigest(digestBytes)); | |||
| } | |||
| @@ -140,7 +140,7 @@ public class RIPEMD160HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("aes"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -156,7 +156,7 @@ public class RIPEMD160HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sha256"); | |||
| assertNotNull(algorithm); | |||
| algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| rawDigestBytes = digest.getRawDigest(); | |||
| byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -65,7 +65,7 @@ public class RSACryptoFunctionTest { | |||
| assertEquals(2 + 1 + 257, pubKey.toBytes().length); | |||
| assertEquals(2 + 1 + 1153, privKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | |||
| @@ -219,7 +219,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -251,7 +251,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -284,7 +284,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -316,7 +316,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -355,7 +355,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.toBytes(); | |||
| byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -392,7 +392,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.getRawDigest(); | |||
| byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -432,7 +432,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||
| byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -470,7 +470,7 @@ public class RSACryptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
| byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -57,7 +57,7 @@ public class SHA256HashFunctionTest { | |||
| HashDigest digest = hashFunction.hash(data); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] digestBytes = digest.toBytes(); | |||
| assertEquals(256 / 8 + 2, digestBytes.length); | |||
| @@ -111,7 +111,7 @@ public class SHA256HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("aes"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | |||
| assertFalse(hashFunction.supportHashDigest(digestBytes)); | |||
| } | |||
| @@ -140,7 +140,7 @@ public class SHA256HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("aes"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -156,7 +156,7 @@ public class SHA256HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("ripemd160"); | |||
| assertNotNull(algorithm); | |||
| algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| rawDigestBytes = digest.getRawDigest(); | |||
| byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -146,7 +146,7 @@ public class ECDSAUtilsTest { | |||
| assertTrue(ECDSAUtils.verify(pubKey,signature,hashedMsg)); | |||
| } | |||
| @Test | |||
| // @Test | |||
| public void performanceTest(){ | |||
| int count = 10000; | |||
| @@ -109,7 +109,7 @@ public class ED25519UtilsTest { | |||
| } | |||
| @Test | |||
| // @Test | |||
| public void performanceTest(){ | |||
| int count = 10000; | |||
| @@ -162,7 +162,7 @@ public class RSAUtilsTest { | |||
| } | |||
| @Test | |||
| // @Test | |||
| public void performanceTest(){ | |||
| int count = 10000; | |||
| @@ -228,7 +228,7 @@ public class RSAUtilsTest { | |||
| } | |||
| } | |||
| @Test | |||
| // @Test | |||
| public void encryptionConsistencyTest(){ | |||
| int count = 10000; | |||
| @@ -326,7 +326,7 @@ public class RSAUtilsTest { | |||
| assertArrayEquals(data,plaintext); | |||
| } | |||
| @Test | |||
| // @Test | |||
| public void signatureConsistencyTest() { | |||
| int count = 10000; | |||
| @@ -9,7 +9,7 @@ import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| @DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||
| //@DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||
| public interface CryptoAlgorithm { | |||
| /** | |||
| @@ -51,7 +51,7 @@ public interface CryptoAlgorithm { | |||
| static final int SYMMETRIC_KEY = 0x0200; | |||
| /** | |||
| * 算法编码的字节长度;等同于 {@link #toBytes(CryptoAlgorithm)} 返回的字节数组的长度; | |||
| * 算法编码的字节长度;等同于 {@link #getCodeBytes(CryptoAlgorithm)} 返回的字节数组的长度; | |||
| */ | |||
| static final int CODE_SIZE = 2; | |||
| @@ -63,7 +63,7 @@ public interface CryptoAlgorithm { | |||
| * {@link #EXT_ALGORITHM}) 5 种); 接下来4位标识密钥类型(包括:{@link #SYMMETRIC_KEY}, | |||
| * {@link #ASYMMETRIC_KEY}); 最后8位是算法唯一ID; | |||
| */ | |||
| @DataField(primitiveType = PrimitiveType.INT16, order = 0) | |||
| // @DataField(primitiveType = PrimitiveType.INT16, order = 0) | |||
| short code(); | |||
| /** | |||
| @@ -81,16 +81,16 @@ public interface CryptoAlgorithm { | |||
| * | |||
| * @return | |||
| */ | |||
| static byte[] toBytes(CryptoAlgorithm algorithm) { | |||
| static byte[] getCodeBytes(CryptoAlgorithm algorithm) { | |||
| return BytesUtils.toBytes(algorithm.code()); | |||
| } | |||
| static short resolveCode(byte[] algorithmBytes) { | |||
| return BytesUtils.toShort(algorithmBytes, 0); | |||
| static short resolveCode(byte[] codeBytes) { | |||
| return BytesUtils.toShort(codeBytes, 0); | |||
| } | |||
| static short resolveCode(byte[] algorithmBytes, int offset) { | |||
| return BytesUtils.toShort(algorithmBytes, offset); | |||
| static short resolveCode(byte[] codeBytes, int offset) { | |||
| return BytesUtils.toShort(codeBytes, offset); | |||
| } | |||
| static short resolveCode(InputStream in) { | |||
| @@ -10,7 +10,7 @@ public final class CryptoBytesEncoding { | |||
| } | |||
| static byte[] encodeBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
| return BytesUtils.concat(CryptoAlgorithm.toBytes(algorithm), rawCryptoBytes); | |||
| return BytesUtils.concat(CryptoAlgorithm.getCodeBytes(algorithm), rawCryptoBytes); | |||
| } | |||
| public static short decodeAlgorithm(byte[] cryptoBytes) { | |||
| @@ -65,7 +65,7 @@ public class SM2CyptoFunctionTest { | |||
| assertEquals(2 + 1 + 65, pubKey.toBytes().length); | |||
| assertEquals(2 + 1 + 32, privKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | |||
| @@ -219,7 +219,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -251,7 +251,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
| byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -284,7 +284,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -316,7 +316,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
| byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | |||
| byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | |||
| @@ -355,7 +355,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.toBytes(); | |||
| byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -392,7 +392,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = signatureDigest.getRawDigest(); | |||
| byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -432,7 +432,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||
| byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -470,7 +470,7 @@ public class SM2CyptoFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
| byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -55,7 +55,7 @@ public class SM3HashFunctionTest { | |||
| HashDigest digest = hashFunction.hash(data); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] digestBytes = digest.toBytes(); | |||
| assertEquals(256 / 8 + 2, digestBytes.length); | |||
| @@ -109,7 +109,7 @@ public class SM3HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm4"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | |||
| assertFalse(hashFunction.supportHashDigest(digestBytes)); | |||
| } | |||
| @@ -138,7 +138,7 @@ public class SM3HashFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm4"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawDigestBytes = digest.getRawDigest(); | |||
| byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | |||
| @@ -68,7 +68,7 @@ public class SM4EncryptionFunctionTest { | |||
| assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | |||
| assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] keyTypeBytes = new byte[] { SYMMETRIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | |||
| @@ -165,7 +165,7 @@ public class SM4EncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -194,7 +194,7 @@ public class SM4EncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
| byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | |||
| byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
| @@ -232,7 +232,7 @@ public class SM4EncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||
| byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -268,7 +268,7 @@ public class SM4EncryptionFunctionTest { | |||
| algorithm = Crypto.getAlgorithm("sm3"); | |||
| assertNotNull(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||
| byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
| byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
| byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
| @@ -119,50 +119,50 @@ public class SM2UtilsTest { | |||
| byte[] plaintext = SM2Utils.decrypt(ciphertext,privKeyBytes); | |||
| assertArrayEquals(expectedMessage.getBytes(),plaintext); | |||
| } | |||
| // | |||
| // @Test | |||
| public void encryptingPerformace(){ | |||
| byte[] data = new byte[1000]; | |||
| Random random = new Random(); | |||
| random.nextBytes(data); | |||
| int count = 10000; | |||
| byte[] ciphertext = null; | |||
| AsymmetricCipherKeyPair keyPair = SM2Utils.generateKeyPair(); | |||
| ECPublicKeyParameters ecPub = (ECPublicKeyParameters) keyPair.getPublic(); | |||
| ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters) keyPair.getPrivate(); | |||
| System.out.println("=================== do SM2 encrypt test ==================="); | |||
| for (int r = 0; r < 5; r++) { | |||
| System.out.println("------------- round[" + r + "] --------------"); | |||
| long startTS = System.currentTimeMillis(); | |||
| for (int i = 0; i < count; i++) { | |||
| ciphertext = SM2Utils.encrypt(data,ecPub); | |||
| } | |||
| long elapsedTS = System.currentTimeMillis() - startTS; | |||
| System.out.println(String.format("SM2 Encrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, | |||
| (count * 1000.00D) / elapsedTS)); | |||
| } | |||
| System.out.println("=================== do SM2 decrypt 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.decrypt(ciphertext,ecPriv); | |||
| } | |||
| long elapsedTS = System.currentTimeMillis() - startTS; | |||
| System.out.println(String.format("SM2 Decrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, | |||
| (count * 1000.00D) / elapsedTS)); | |||
| } | |||
| } | |||
| // @Test | |||
| // public void encryptingPerformace(){ | |||
| // | |||
| // byte[] data = new byte[1000]; | |||
| // Random random = new Random(); | |||
| // random.nextBytes(data); | |||
| // | |||
| // int count = 10000; | |||
| // | |||
| // byte[] ciphertext = null; | |||
| // | |||
| // AsymmetricCipherKeyPair keyPair = SM2Utils.generateKeyPair(); | |||
| // ECPublicKeyParameters ecPub = (ECPublicKeyParameters) keyPair.getPublic(); | |||
| // ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters) keyPair.getPrivate(); | |||
| // | |||
| // System.out.println("=================== do SM2 encrypt test ==================="); | |||
| // | |||
| // for (int r = 0; r < 5; r++) { | |||
| // System.out.println("------------- round[" + r + "] --------------"); | |||
| // long startTS = System.currentTimeMillis(); | |||
| // for (int i = 0; i < count; i++) { | |||
| // ciphertext = SM2Utils.encrypt(data,ecPub); | |||
| // } | |||
| // long elapsedTS = System.currentTimeMillis() - startTS; | |||
| // System.out.println(String.format("SM2 Encrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, | |||
| // (count * 1000.00D) / elapsedTS)); | |||
| // } | |||
| // | |||
| // System.out.println("=================== do SM2 decrypt 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.decrypt(ciphertext,ecPriv); | |||
| // } | |||
| // long elapsedTS = System.currentTimeMillis() - startTS; | |||
| // System.out.println(String.format("SM2 Decrypting Count=%s; Elapsed Times=%s; KBPS=%.2f", count, elapsedTS, | |||
| // (count * 1000.00D) / elapsedTS)); | |||
| // } | |||
| // } | |||
| // | |||
| // | |||
| @Test | |||
| public void signingPerformace(){ | |||
| byte[] data = new byte[1024]; | |||
| @@ -0,0 +1,12 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| public interface CryptoProviderInfo { | |||
| String getName(); | |||
| CryptoAlgorithm[] getAlgorithms(); | |||
| } | |||
| @@ -11,7 +11,7 @@ import com.jd.blockchain.utils.security.Ed25519Utils; | |||
| public class ED25519SignatureTest { | |||
| @Test | |||
| // @Test | |||
| public void perfomanceTest() { | |||
| Random rand = new Random(); | |||
| byte[] data = new byte[64]; | |||
| @@ -32,7 +32,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("3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu"); | |||
| PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9"); | |||
| assertEquals(pubKey0, part0.getPubKey()); | |||
| // assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); | |||
| // assertEquals(8900, part0.getConsensusAddress().getPort()); | |||
| @@ -43,7 +43,7 @@ public class LedgerInitSettingTest { | |||
| ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); | |||
| assertEquals(false, part1.getInitializerAddress().isSecure()); | |||
| PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7Ph1SYLQt9uqVEqiuvNXjxCdGvEdN6otJsg5rbr7Aze7kf"); | |||
| PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7PajLB35tEau1kmixc6ZrjLXgxwKbkv5bHhP7nT5dhD9eX"); | |||
| assertEquals(pubKey1, part1.getPubKey()); | |||
| ConsensusParticipantConfig part2 = setting.getConsensusParticipant(2); | |||
| @@ -51,7 +51,7 @@ cons_parti.2.name=bt.com | |||
| #第2个参与方的公钥文件路径; | |||
| cons_parti.2.pubkey-path=keys/bt-com.pub | |||
| #第2个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; | |||
| cons_parti.2.pubkey=3snPdw7i7PZi6TStiyc6mzjprnNhgs2atSGNS8wPYzhbKaUWGFJt7x | |||
| cons_parti.2.pubkey= | |||
| #第2个参与方的共识服务的主机地址; | |||
| cons_parti.2.consensus.host=127.0.0.1 | |||
| #第2个参与方的共识服务的端口; | |||