@@ -1,11 +1,31 @@ | |||||
package com.jd.blockchain.crypto.utils.classic; | 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.PKCSObjectIdentifiers; | ||||
import org.bouncycastle.asn1.pkcs.RSAPrivateKey; | import org.bouncycastle.asn1.pkcs.RSAPrivateKey; | ||||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; | 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.digests.SHA256Digest; | ||||
import org.bouncycastle.crypto.encodings.PKCS1Encoding; | import org.bouncycastle.crypto.encodings.PKCS1Encoding; | ||||
import org.bouncycastle.crypto.engines.RSAEngine; | 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.params.RSAPrivateCrtKeyParameters; | ||||
import org.bouncycastle.crypto.signers.RSADigestSigner; | import org.bouncycastle.crypto.signers.RSADigestSigner; | ||||
import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; | 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 | * @author zhanglin33 | ||||
@@ -442,9 +453,9 @@ public class RSAUtils { | |||||
throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | ||||
} | } | ||||
RSAPrivateCrtKeyImpl privateKey; | |||||
RSAPrivateCrtKey privateKey; | |||||
try { | try { | ||||
privateKey = (RSAPrivateCrtKeyImpl) keyFactory.generatePrivate(keySpec); | |||||
privateKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec); | |||||
} catch (InvalidKeySpecException e) { | } catch (InvalidKeySpecException e) { | ||||
throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | throw new com.jd.blockchain.crypto.CryptoException(e.getMessage(), e); | ||||
} | } | ||||
@@ -69,7 +69,7 @@ public class AESEncryptionFunctionTest { | |||||
assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | ||||
assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | 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[] keyTypeBytes = new byte[] { SYMMETRIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | ||||
@@ -167,7 +167,7 @@ public class AESEncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -197,7 +197,7 @@ public class AESEncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -235,7 +235,7 @@ public class AESEncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | byte[] rawCiphertextBytes = ciphertext.toBytes(); | ||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -275,7 +275,7 @@ public class AESEncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -66,7 +66,7 @@ public class ECDSASignatureFunctionTest { | |||||
assertEquals(2 + 1 + 65, pubKey.toBytes().length); | assertEquals(2 + 1 + 65, pubKey.toBytes().length); | ||||
assertEquals(2 + 1 + 32, privKey.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[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | ||||
@@ -162,7 +162,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -194,7 +194,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -227,7 +227,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -259,7 +259,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -298,7 +298,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | byte[] rawDigestBytes = signatureDigest.toBytes(); | ||||
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -335,7 +335,7 @@ public class ECDSASignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.getRawDigest(); | byte[] rawDigestBytes = signatureDigest.getRawDigest(); | ||||
byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -93,7 +93,7 @@ public class ED25519SignatureFunctionTest { | |||||
assertEquals(2 + 1 + 32, pubKey.toBytes().length); | assertEquals(2 + 1 + 32, pubKey.toBytes().length); | ||||
assertEquals(2 + 1 + 32, privKey.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[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | ||||
@@ -189,7 +189,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -221,7 +221,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -254,7 +254,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -286,7 +286,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -325,7 +325,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | byte[] rawDigestBytes = signatureDigest.toBytes(); | ||||
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -362,7 +362,7 @@ public class ED25519SignatureFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.getRawDigest(); | byte[] rawDigestBytes = signatureDigest.getRawDigest(); | ||||
byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -57,7 +57,7 @@ public class RIPEMD160HashFunctionTest { | |||||
HashDigest digest = hashFunction.hash(data); | HashDigest digest = hashFunction.hash(data); | ||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] digestBytes = digest.toBytes(); | byte[] digestBytes = digest.toBytes(); | ||||
assertEquals(160 / 8 + 2, digestBytes.length); | assertEquals(160 / 8 + 2, digestBytes.length); | ||||
@@ -111,7 +111,7 @@ public class RIPEMD160HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("aes"); | algorithm = Crypto.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | ||||
assertFalse(hashFunction.supportHashDigest(digestBytes)); | assertFalse(hashFunction.supportHashDigest(digestBytes)); | ||||
} | } | ||||
@@ -140,7 +140,7 @@ public class RIPEMD160HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("aes"); | algorithm = Crypto.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -156,7 +156,7 @@ public class RIPEMD160HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sha256"); | algorithm = Crypto.getAlgorithm("sha256"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
rawDigestBytes = digest.getRawDigest(); | rawDigestBytes = digest.getRawDigest(); | ||||
byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -65,7 +65,7 @@ public class RSACryptoFunctionTest { | |||||
assertEquals(2 + 1 + 257, pubKey.toBytes().length); | assertEquals(2 + 1 + 257, pubKey.toBytes().length); | ||||
assertEquals(2 + 1 + 1153, privKey.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[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | ||||
@@ -219,7 +219,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -251,7 +251,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -284,7 +284,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -316,7 +316,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -355,7 +355,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | byte[] rawDigestBytes = signatureDigest.toBytes(); | ||||
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -392,7 +392,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.getRawDigest(); | byte[] rawDigestBytes = signatureDigest.getRawDigest(); | ||||
byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -432,7 +432,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | byte[] rawCiphertextBytes = ciphertext.toBytes(); | ||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -470,7 +470,7 @@ public class RSACryptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -57,7 +57,7 @@ public class SHA256HashFunctionTest { | |||||
HashDigest digest = hashFunction.hash(data); | HashDigest digest = hashFunction.hash(data); | ||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] digestBytes = digest.toBytes(); | byte[] digestBytes = digest.toBytes(); | ||||
assertEquals(256 / 8 + 2, digestBytes.length); | assertEquals(256 / 8 + 2, digestBytes.length); | ||||
@@ -111,7 +111,7 @@ public class SHA256HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("aes"); | algorithm = Crypto.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | ||||
assertFalse(hashFunction.supportHashDigest(digestBytes)); | assertFalse(hashFunction.supportHashDigest(digestBytes)); | ||||
} | } | ||||
@@ -140,7 +140,7 @@ public class SHA256HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("aes"); | algorithm = Crypto.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -156,7 +156,7 @@ public class SHA256HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("ripemd160"); | algorithm = Crypto.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
rawDigestBytes = digest.getRawDigest(); | rawDigestBytes = digest.getRawDigest(); | ||||
byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -146,7 +146,7 @@ public class ECDSAUtilsTest { | |||||
assertTrue(ECDSAUtils.verify(pubKey,signature,hashedMsg)); | assertTrue(ECDSAUtils.verify(pubKey,signature,hashedMsg)); | ||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void performanceTest(){ | public void performanceTest(){ | ||||
int count = 10000; | int count = 10000; | ||||
@@ -109,7 +109,7 @@ public class ED25519UtilsTest { | |||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void performanceTest(){ | public void performanceTest(){ | ||||
int count = 10000; | int count = 10000; | ||||
@@ -162,7 +162,7 @@ public class RSAUtilsTest { | |||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void performanceTest(){ | public void performanceTest(){ | ||||
int count = 10000; | int count = 10000; | ||||
@@ -228,7 +228,7 @@ public class RSAUtilsTest { | |||||
} | } | ||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void encryptionConsistencyTest(){ | public void encryptionConsistencyTest(){ | ||||
int count = 10000; | int count = 10000; | ||||
@@ -326,7 +326,7 @@ public class RSAUtilsTest { | |||||
assertArrayEquals(data,plaintext); | assertArrayEquals(data,plaintext); | ||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void signatureConsistencyTest() { | public void signatureConsistencyTest() { | ||||
int count = 10000; | int count = 10000; | ||||
@@ -9,7 +9,7 @@ import com.jd.blockchain.binaryproto.PrimitiveType; | |||||
import com.jd.blockchain.consts.DataCodes; | import com.jd.blockchain.consts.DataCodes; | ||||
import com.jd.blockchain.utils.io.BytesUtils; | import com.jd.blockchain.utils.io.BytesUtils; | ||||
@DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||||
//@DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||||
public interface CryptoAlgorithm { | public interface CryptoAlgorithm { | ||||
/** | /** | ||||
@@ -51,7 +51,7 @@ public interface CryptoAlgorithm { | |||||
static final int SYMMETRIC_KEY = 0x0200; | static final int SYMMETRIC_KEY = 0x0200; | ||||
/** | /** | ||||
* 算法编码的字节长度;等同于 {@link #toBytes(CryptoAlgorithm)} 返回的字节数组的长度; | |||||
* 算法编码的字节长度;等同于 {@link #getCodeBytes(CryptoAlgorithm)} 返回的字节数组的长度; | |||||
*/ | */ | ||||
static final int CODE_SIZE = 2; | static final int CODE_SIZE = 2; | ||||
@@ -63,7 +63,7 @@ public interface CryptoAlgorithm { | |||||
* {@link #EXT_ALGORITHM}) 5 种); 接下来4位标识密钥类型(包括:{@link #SYMMETRIC_KEY}, | * {@link #EXT_ALGORITHM}) 5 种); 接下来4位标识密钥类型(包括:{@link #SYMMETRIC_KEY}, | ||||
* {@link #ASYMMETRIC_KEY}); 最后8位是算法唯一ID; | * {@link #ASYMMETRIC_KEY}); 最后8位是算法唯一ID; | ||||
*/ | */ | ||||
@DataField(primitiveType = PrimitiveType.INT16, order = 0) | |||||
// @DataField(primitiveType = PrimitiveType.INT16, order = 0) | |||||
short code(); | short code(); | ||||
/** | /** | ||||
@@ -81,16 +81,16 @@ public interface CryptoAlgorithm { | |||||
* | * | ||||
* @return | * @return | ||||
*/ | */ | ||||
static byte[] toBytes(CryptoAlgorithm algorithm) { | |||||
static byte[] getCodeBytes(CryptoAlgorithm algorithm) { | |||||
return BytesUtils.toBytes(algorithm.code()); | 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) { | static short resolveCode(InputStream in) { | ||||
@@ -10,7 +10,7 @@ public final class CryptoBytesEncoding { | |||||
} | } | ||||
static byte[] encodeBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | 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) { | public static short decodeAlgorithm(byte[] cryptoBytes) { | ||||
@@ -65,7 +65,7 @@ public class SM2CyptoFunctionTest { | |||||
assertEquals(2 + 1 + 65, pubKey.toBytes().length); | assertEquals(2 + 1 + 65, pubKey.toBytes().length); | ||||
assertEquals(2 + 1 + 32, privKey.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[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawPubKeyBytes = pubKey.getRawKeyBytes(); | ||||
@@ -219,7 +219,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -251,7 +251,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | byte[] rawKeyBytes = privKey.getRawKeyBytes(); | ||||
byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -284,7 +284,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -316,7 +316,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | ||||
byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | byte[] rawKeyBytes = pubKey.getRawKeyBytes(); | ||||
byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes); | ||||
@@ -355,7 +355,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | byte[] rawDigestBytes = signatureDigest.toBytes(); | ||||
byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -392,7 +392,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.getRawDigest(); | byte[] rawDigestBytes = signatureDigest.getRawDigest(); | ||||
byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -432,7 +432,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | byte[] rawCiphertextBytes = ciphertext.toBytes(); | ||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -470,7 +470,7 @@ public class SM2CyptoFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -55,7 +55,7 @@ public class SM3HashFunctionTest { | |||||
HashDigest digest = hashFunction.hash(data); | HashDigest digest = hashFunction.hash(data); | ||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] digestBytes = digest.toBytes(); | byte[] digestBytes = digest.toBytes(); | ||||
assertEquals(256 / 8 + 2, digestBytes.length); | assertEquals(256 / 8 + 2, digestBytes.length); | ||||
@@ -109,7 +109,7 @@ public class SM3HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm4"); | algorithm = Crypto.getAlgorithm("sm4"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length); | ||||
assertFalse(hashFunction.supportHashDigest(digestBytes)); | assertFalse(hashFunction.supportHashDigest(digestBytes)); | ||||
} | } | ||||
@@ -138,7 +138,7 @@ public class SM3HashFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm4"); | algorithm = Crypto.getAlgorithm("sm4"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawDigestBytes = digest.getRawDigest(); | byte[] rawDigestBytes = digest.getRawDigest(); | ||||
byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes); | ||||
@@ -68,7 +68,7 @@ public class SM4EncryptionFunctionTest { | |||||
assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | assertEquals(algorithm.code(), symmetricKey.getAlgorithm()); | ||||
assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | 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[] keyTypeBytes = new byte[] { SYMMETRIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes()); | ||||
@@ -165,7 +165,7 @@ public class SM4EncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -194,7 +194,7 @@ public class SM4EncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | ||||
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | byte[] rawKeyBytes = symmetricKey.getRawKeyBytes(); | ||||
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | ||||
@@ -232,7 +232,7 @@ public class SM4EncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | byte[] rawCiphertextBytes = ciphertext.toBytes(); | ||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -268,7 +268,7 @@ public class SM4EncryptionFunctionTest { | |||||
algorithm = Crypto.getAlgorithm("sm3"); | algorithm = Crypto.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | ||||
@@ -119,50 +119,50 @@ public class SM2UtilsTest { | |||||
byte[] plaintext = SM2Utils.decrypt(ciphertext,privKeyBytes); | byte[] plaintext = SM2Utils.decrypt(ciphertext,privKeyBytes); | ||||
assertArrayEquals(expectedMessage.getBytes(),plaintext); | 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 | // @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(){ | public void signingPerformace(){ | ||||
byte[] data = new byte[1024]; | byte[] data = new byte[1024]; | ||||
@@ -2,6 +2,13 @@ package com.jd.blockchain.gateway.web; | |||||
import java.util.List; | import java.util.List; | ||||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||||
import com.jd.blockchain.consensus.ClientIdentification; | |||||
import com.jd.blockchain.consensus.ClientIdentifications; | |||||
import com.jd.blockchain.consensus.action.ActionRequest; | |||||
import com.jd.blockchain.consensus.action.ActionResponse; | |||||
import com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings; | |||||
import com.jd.blockchain.ledger.*; | |||||
import com.jd.blockchain.web.serializes.ByteArrayObjectUtil; | import com.jd.blockchain.web.serializes.ByteArrayObjectUtil; | ||||
import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||
import org.springframework.format.FormatterRegistry; | import org.springframework.format.FormatterRegistry; | ||||
@@ -27,6 +34,7 @@ public class GatewayWebServerConfigurer implements WebMvcConfigurer { | |||||
static { | static { | ||||
JSONSerializeUtils.disableCircularReferenceDetect(); | JSONSerializeUtils.disableCircularReferenceDetect(); | ||||
JSONSerializeUtils.configStringSerializer(ByteArray.class); | JSONSerializeUtils.configStringSerializer(ByteArray.class); | ||||
DataContractRegistry.register(BftsmartNodeSettings.class); | |||||
} | } | ||||
@@ -21,10 +21,10 @@ peer.providers=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider | |||||
data.retrieval.url=http://127.0.0.1:10001 | data.retrieval.url=http://127.0.0.1:10001 | ||||
#默认公钥的内容(Base58编码数据); | #默认公钥的内容(Base58编码数据); | ||||
keys.default.pubkey=3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu | |||||
keys.default.pubkey=3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9 | |||||
#默认私钥的路径;在 pk-path 和 pk 之间必须设置其一; | #默认私钥的路径;在 pk-path 和 pk 之间必须设置其一; | ||||
keys.default.privkey-path= | keys.default.privkey-path= | ||||
#默认私钥的内容(加密的Base58编码数据);在 pk-path 和 pk 之间必须设置其一; | #默认私钥的内容(加密的Base58编码数据);在 pk-path 和 pk 之间必须设置其一; | ||||
keys.default.privkey=177gjyoEUhdD1NkQSxBVvfSyovMd1ha5H46zsb9kyErLNBuQkLRAf2ea6CNjStjCFJQN8S1 | |||||
keys.default.privkey=177gjzHTznYdPgWqZrH43W3yp37onm74wYXT4v9FukpCHBrhRysBBZh7Pzdo5AMRyQGJD7x | |||||
#默认私钥的解码密码; | #默认私钥的解码密码; | ||||
keys.default.privkey-password=DYu3G8aGTMBW1WrTw76zxQJQU4DHLw9MLyy7peG4LKkY | keys.default.privkey-password=DYu3G8aGTMBW1WrTw76zxQJQU4DHLw9MLyy7peG4LKkY |
@@ -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 { | public class ED25519SignatureTest { | ||||
@Test | |||||
// @Test | |||||
public void perfomanceTest() { | public void perfomanceTest() { | ||||
Random rand = new Random(); | Random rand = new Random(); | ||||
byte[] data = new byte[64]; | byte[] data = new byte[64]; | ||||
@@ -32,7 +32,7 @@ public class LedgerInitSettingTest { | |||||
ConsensusParticipantConfig part0 = setting.getConsensusParticipant(0); | ConsensusParticipantConfig part0 = setting.getConsensusParticipant(0); | ||||
assertEquals("jd.com", part0.getName()); | assertEquals("jd.com", part0.getName()); | ||||
assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); | assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); | ||||
PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu"); | |||||
PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9"); | |||||
assertEquals(pubKey0, part0.getPubKey()); | assertEquals(pubKey0, part0.getPubKey()); | ||||
// assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); | // assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); | ||||
// assertEquals(8900, part0.getConsensusAddress().getPort()); | // assertEquals(8900, part0.getConsensusAddress().getPort()); | ||||
@@ -43,7 +43,7 @@ public class LedgerInitSettingTest { | |||||
ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); | ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); | ||||
assertEquals(false, part1.getInitializerAddress().isSecure()); | assertEquals(false, part1.getInitializerAddress().isSecure()); | ||||
PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7Ph1SYLQt9uqVEqiuvNXjxCdGvEdN6otJsg5rbr7Aze7kf"); | |||||
PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7PajLB35tEau1kmixc6ZrjLXgxwKbkv5bHhP7nT5dhD9eX"); | |||||
assertEquals(pubKey1, part1.getPubKey()); | assertEquals(pubKey1, part1.getPubKey()); | ||||
ConsensusParticipantConfig part2 = setting.getConsensusParticipant(2); | ConsensusParticipantConfig part2 = setting.getConsensusParticipant(2); | ||||
@@ -51,7 +51,7 @@ cons_parti.2.name=bt.com | |||||
#第2个参与方的公钥文件路径; | #第2个参与方的公钥文件路径; | ||||
cons_parti.2.pubkey-path=keys/bt-com.pub | cons_parti.2.pubkey-path=keys/bt-com.pub | ||||
#第2个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; | #第2个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; | ||||
cons_parti.2.pubkey=3snPdw7i7PZi6TStiyc6mzjprnNhgs2atSGNS8wPYzhbKaUWGFJt7x | |||||
cons_parti.2.pubkey= | |||||
#第2个参与方的共识服务的主机地址; | #第2个参与方的共识服务的主机地址; | ||||
cons_parti.2.consensus.host=127.0.0.1 | cons_parti.2.consensus.host=127.0.0.1 | ||||
#第2个参与方的共识服务的端口; | #第2个参与方的共识服务的端口; | ||||