diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java
index 20a2d796..d6e42764 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java
@@ -8,12 +8,18 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import com.jd.blockchain.crypto.*;
+import com.jd.blockchain.crypto.Ciphertext;
+import com.jd.blockchain.crypto.CryptoAlgorithm;
+import com.jd.blockchain.crypto.CryptoException;
+import com.jd.blockchain.crypto.CryptoKey;
+import com.jd.blockchain.crypto.SymmetricCiphertext;
+import com.jd.blockchain.crypto.SymmetricEncryptionFunction;
+import com.jd.blockchain.crypto.SymmetricKey;
import com.jd.blockchain.utils.security.AESUtils;
public class AESEncryptionFunction implements SymmetricEncryptionFunction {
- public static final CryptoAlgorithm AES = ClassicCryptoService.AES_ALGORITHM;
+ public static final CryptoAlgorithm AES = ClassicAlgorithm.AES;
private static final int KEY_SIZE = 128 / 8;
private static final int BLOCK_SIZE = 128 / 8;
@@ -38,7 +44,7 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密钥数据的算法标识对应AES算法
- if (key.getAlgorithm().code() != AES.code()) {
+ if (key.getAlgorithm() != AES.code()) {
throw new CryptoException("The is not AES symmetric key!");
}
@@ -103,7 +109,7 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密钥数据的算法标识对应AES算法
- if (key.getAlgorithm().code() != AES.code()) {
+ if (key.getAlgorithm() != AES.code()) {
throw new CryptoException("The is not AES symmetric key!");
}
@@ -113,7 +119,7 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密文数据算法标识对应AES算法
- if (ciphertext.getAlgorithm().code() != AES.code()) {
+ if (ciphertext.getAlgorithm() != AES.code()) {
throw new CryptoException("This is not AES ciphertext!");
}
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicAlgorithm.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicAlgorithm.java
new file mode 100644
index 00000000..fdd040fb
--- /dev/null
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicAlgorithm.java
@@ -0,0 +1,27 @@
+package com.jd.blockchain.crypto.service.classic;
+
+import com.jd.blockchain.crypto.CryptoAlgorithm;
+import com.jd.blockchain.crypto.CryptoAlgorithmDefinition;
+
+public final class ClassicAlgorithm {
+
+ public static final CryptoAlgorithm ED25519 = CryptoAlgorithmDefinition.defineSignature("ED25519", false,
+ (byte) 21);
+ public static final CryptoAlgorithm ECDSA = CryptoAlgorithmDefinition.defineSignature("ECDSA", false, (byte) 22);
+
+ public static final CryptoAlgorithm RSA = CryptoAlgorithmDefinition.defineSignature("RSA", true, (byte) 23);
+
+ public static final CryptoAlgorithm SHA256 = CryptoAlgorithmDefinition.defineHash("SHA256", (byte) 24);
+
+ public static final CryptoAlgorithm RIPEMD160 = CryptoAlgorithmDefinition.defineHash("RIPEMD160", (byte) 25);
+
+ public static final CryptoAlgorithm AES = CryptoAlgorithmDefinition.defineSymmetricEncryption("AES", (byte) 26);
+
+ public static final CryptoAlgorithm JVM_SECURE_RANDOM = CryptoAlgorithmDefinition.defineRandom("JVM-SECURE-RANDOM",
+ (byte) 27);
+
+ private ClassicAlgorithm() {
+
+ }
+
+}
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicCryptoService.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicCryptoService.java
index d807fd7c..164f966c 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicCryptoService.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ClassicCryptoService.java
@@ -5,8 +5,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import com.jd.blockchain.crypto.CryptoAlgorithm;
-import com.jd.blockchain.crypto.CryptoAlgorithmDefinition;
import com.jd.blockchain.crypto.CryptoFunction;
import com.jd.blockchain.crypto.CryptoService;
import com.jd.blockchain.provider.NamedProvider;
@@ -14,25 +12,6 @@ import com.jd.blockchain.provider.NamedProvider;
@NamedProvider("CLASSIC")
public class ClassicCryptoService implements CryptoService {
- public static final CryptoAlgorithm ED25519_ALGORITHM = CryptoAlgorithmDefinition.defineSignature("ED25519",
- false, (byte) 21);
- public static final CryptoAlgorithm ECDSA_ALGORITHM = CryptoAlgorithmDefinition.defineSignature("ECDSA",
- false, (byte) 22);
-
- public static final CryptoAlgorithm RSA_ALGORITHM = CryptoAlgorithmDefinition.defineSignature("RSA",
- true, (byte) 23);
-
- public static final CryptoAlgorithm SHA256_ALGORITHM = CryptoAlgorithmDefinition.defineHash("SHA256",
- (byte) 24);
-
- public static final CryptoAlgorithm RIPEMD160_ALGORITHM = CryptoAlgorithmDefinition.defineHash("RIPEMD160",
- (byte) 25);
-
- public static final CryptoAlgorithm AES_ALGORITHM = CryptoAlgorithmDefinition.defineSymmetricEncryption("AES",
- (byte) 26);
-
- public static final CryptoAlgorithm JVM_SECURE_RANDOM_ALGORITHM = CryptoAlgorithmDefinition
- .defineRandom("JVM-SECURE-RANDOM", (byte) 27);
public static final AESEncryptionFunction AES = new AESEncryptionFunction();
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ECDSASignatureFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ECDSASignatureFunction.java
index 145e687c..779d77a6 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ECDSASignatureFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ECDSASignatureFunction.java
@@ -59,7 +59,7 @@ public class ECDSASignatureFunction implements SignatureFunction {
@Override
public CryptoAlgorithm getAlgorithm() {
- return ClassicCryptoService.ECDSA_ALGORITHM;
+ return ClassicAlgorithm.ECDSA;
}
@Override
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java
index 202070f8..ff1d5dfb 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java
@@ -20,7 +20,7 @@ import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
public class ED25519SignatureFunction implements SignatureFunction {
- private static final CryptoAlgorithm ED25519 = ClassicCryptoService.ED25519_ALGORITHM;
+ private static final CryptoAlgorithm ED25519 = ClassicAlgorithm.ED25519;
private static final int PUBKEY_SIZE = 32;
private static final int PRIVKEY_SIZE = 32;
@@ -44,7 +44,7 @@ public class ED25519SignatureFunction implements SignatureFunction {
}
// 验证密钥数据的算法标识对应ED25519签名算法
- if (privKey.getAlgorithm().code() != ED25519.code()) {
+ if (privKey.getAlgorithm() != ED25519.code()) {
throw new CryptoException("This key is not ED25519 private key!");
}
@@ -66,12 +66,12 @@ public class ED25519SignatureFunction implements SignatureFunction {
}
// 验证密钥数据的算法标识对应ED25519签名算法
- if (pubKey.getAlgorithm().code() != ED25519.code()) {
+ if (pubKey.getAlgorithm() != ED25519.code()) {
throw new CryptoException("This key is not ED25519 public key!");
}
// 验证密文数据的算法标识对应ED25519签名算法,并且原始摘要长度为64字节
- if (digest.getAlgorithm().code() != ED25519.code() || rawDigestBytes.length != SIGNATUREDIGEST_SIZE) {
+ if (digest.getAlgorithm() != ED25519.code() || rawDigestBytes.length != SIGNATUREDIGEST_SIZE) {
throw new CryptoException("This is not ED25519 signature digest!");
}
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/JVMSecureRandomFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/JVMSecureRandomFunction.java
index e32b4813..fe120951 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/JVMSecureRandomFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/JVMSecureRandomFunction.java
@@ -8,7 +8,7 @@ import com.jd.blockchain.crypto.RandomGenerator;
public class JVMSecureRandomFunction implements RandomFunction {
- private static final CryptoAlgorithm JVM_SECURE_RANDOM = ClassicCryptoService.JVM_SECURE_RANDOM_ALGORITHM;
+ private static final CryptoAlgorithm JVM_SECURE_RANDOM = ClassicAlgorithm.JVM_SECURE_RANDOM;
JVMSecureRandomFunction() {
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java
index 2f50e147..f6b2771f 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunction.java
@@ -12,7 +12,7 @@ import com.jd.blockchain.utils.security.RipeMD160Utils;
public class RIPEMD160HashFunction implements HashFunction {
- private static final CryptoAlgorithm RIPEMD160 = ClassicCryptoService.RIPEMD160_ALGORITHM;
+ private static final CryptoAlgorithm RIPEMD160 = ClassicAlgorithm.RIPEMD160;
private static final int DIGEST_BYTES = 160 / 8;
diff --git a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java
index 3c66704c..1ee2511b 100644
--- a/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java
+++ b/source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/SHA256HashFunction.java
@@ -12,7 +12,7 @@ import com.jd.blockchain.utils.security.ShaUtils;
public class SHA256HashFunction implements HashFunction {
- private static final CryptoAlgorithm SHA256 = ClassicCryptoService.SHA256_ALGORITHM;
+ private static final CryptoAlgorithm SHA256 = ClassicAlgorithm.SHA256;
private static final int DIGEST_BYTES = 256 / 8;
diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
index 83548751..02edc9f1 100644
--- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
+++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
@@ -1,20 +1,28 @@
package test.com.jd.blockchain.crypto.service.classic;
-import com.jd.blockchain.crypto.*;
-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.CryptoAlgorithm.ENCRYPTION_ALGORITHM;
import static com.jd.blockchain.crypto.CryptoAlgorithm.SYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+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.CryptoServiceProviders;
+import com.jd.blockchain.crypto.SymmetricEncryptionFunction;
+import com.jd.blockchain.crypto.SymmetricKey;
+import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm;
+import com.jd.blockchain.utils.io.BytesUtils;
/**
* @author zhanglin33
@@ -24,268 +32,261 @@ import static org.junit.Assert.*;
*/
public class AESEncryptionFunctionTest {
- @Test
- public void getAlgorithmTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
-
- algorithm = CryptoServiceProviders.getAlgorithm("AES");
- assertNotNull(algorithm);
-
- assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
-
- algorithm = CryptoServiceProviders.getAlgorithm("aess");
- assertNull(algorithm);
- }
-
- @Test
- public void generateSymmetricKeyTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- assertEquals(SYMMETRIC.CODE,symmetricKey.getKeyType().CODE);
- assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);
-
- assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
- assertEquals(algorithm.code(),symmetricKey.getAlgorithm().code());
-
- assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] keyTypeBytes = new byte[] {SYMMETRIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- assertArrayEquals(BytesUtils.concat(algoBytes,keyTypeBytes,rawKeyBytes),symmetricKey.toBytes());
- }
-
-
-
- @Test
- public void encryptTest(){
-
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ @Test
+ public void getAlgorithmTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
- byte[] ciphertextBytes = ciphertext.toBytes();
- assertEquals(2 + 16 + 1024 , ciphertextBytes.length);
- CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
- assertEquals("AES",ciphertextAlgo.name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),ciphertextAlgo.code());
+ algorithm = CryptoServiceProviders.getAlgorithm("AES");
+ assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- assertArrayEquals(BytesUtils.concat(algoBytes,rawCiphertextBytes),ciphertextBytes);
- }
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
+ algorithm = CryptoServiceProviders.getAlgorithm("aess");
+ assertNull(algorithm);
+ }
- @Test
- public void decryptTest(){
+ @Test
+ public void generateSymmetricKeyTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ assertEquals(SYMMETRIC.CODE, symmetricKey.getKeyType().CODE);
+ assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ assertEquals(algorithm.code(), symmetricKey.getAlgorithm());
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
+ assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] keyTypeBytes = new byte[] { SYMMETRIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes());
+ }
- byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey,ciphertext);
+ @Test
+ public void encryptTest() {
- assertArrayEquals(data,decryptedPlaintext);
- }
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
-// @Test
-// public void streamEncryptTest(){
-//
-// byte[] data = new byte[1024];
-// Random random = new Random();
-// random.nextBytes(data);
-//
-//
-// InputStream inputStream = new ByteArrayInputStream(data);
-// OutputStream outputStream = new ByteArrayOutputStream();
-//
-// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
-// assertNotNull(algorithm);
-//
-// SymmetricEncryptionFunction symmetricEncryptionFunction =
-// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-//
-// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-//
-// symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
-//
-// assertNotNull(outputStream);
-//
-//
-// }
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- @Test
- public void supportSymmetricKeyTest(){
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
+ byte[] ciphertextBytes = ciphertext.toBytes();
+ assertEquals(2 + 16 + 1024, ciphertextBytes.length);
+ assertEquals(ClassicAlgorithm.AES.code(), ciphertext.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)), ciphertext.getAlgorithm());
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ byte[] algoBytes = BytesUtils.toBytes(ciphertext.getAlgorithm());
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ assertArrayEquals(BytesUtils.concat(algoBytes, rawCiphertextBytes), ciphertextBytes);
+ }
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- byte[] symmetricKeyBytes = symmetricKey.toBytes();
+ @Test
+ public void decryptTest() {
- assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
- }
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
+
+ byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey, ciphertext);
- @Test
- public void resolveSymmetricKeyTest(){
+ assertArrayEquals(data, decryptedPlaintext);
+ }
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
+ // @Test
+ // public void streamEncryptTest(){
+ //
+ // byte[] data = new byte[1024];
+ // Random random = new Random();
+ // random.nextBytes(data);
+ //
+ //
+ // InputStream inputStream = new ByteArrayInputStream(data);
+ // OutputStream outputStream = new ByteArrayOutputStream();
+ //
+ // CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ // assertNotNull(algorithm);
+ //
+ // SymmetricEncryptionFunction symmetricEncryptionFunction =
+ // CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ //
+ // SymmetricKey symmetricKey = (SymmetricKey)
+ // symmetricEncryptionFunction.generateSymmetricKey();
+ //
+ // symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
+ //
+ // assertNotNull(outputStream);
+ //
+ //
+ // }
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ @Test
+ public void supportSymmetricKeyTest() {
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- byte[] symmetricKeyBytes = symmetricKey.toBytes();
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
- SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE);
- assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);
- assertEquals("AES",resolvedKey.getAlgorithm().name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),resolvedKey.getAlgorithm().code());
- assertArrayEquals(symmetricKeyBytes,resolvedKey.toBytes());
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ byte[] symmetricKeyBytes = symmetricKey.toBytes();
- algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));
+ algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
+ }
- @Test
- public void supportCiphertextTest(){
-
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
-
- byte[] ciphertextBytes = ciphertext.toBytes();
- assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
+ @Test
+ public void resolveSymmetricKeyTest() {
- algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.toBytes();
- byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
- assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes));
- }
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- @Test
- public void resolveCiphertextTest(){
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ byte[] symmetricKeyBytes = symmetricKey.toBytes();
+
+ SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);
+
+ assertEquals(SYMMETRIC.CODE, resolvedKey.getKeyType().CODE);
+ assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);
+ assertEquals(ClassicAlgorithm.AES, resolvedKey.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)), resolvedKey.getAlgorithm());
+ assertArrayEquals(symmetricKeyBytes, resolvedKey.toBytes());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
+
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
+
+ @Test
+ public void supportCiphertextTest() {
+
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
+
+ byte[] ciphertextBytes = ciphertext.toBytes();
+ assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.toBytes();
+ byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
+
+ assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes));
+ }
+
+ @Test
+ public void resolveCiphertextTest() {
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
- byte[] ciphertextBytes = ciphertext.toBytes();
+ byte[] ciphertextBytes = ciphertext.toBytes();
- Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
+ Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
- assertEquals(1024 + 16 , resolvedCiphertext.getRawCiphertext().length);
- assertEquals("AES",resolvedCiphertext.getAlgorithm().name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),resolvedCiphertext.getAlgorithm().code());
- assertArrayEquals(ciphertextBytes,resolvedCiphertext.toBytes());
+ assertEquals(1024 + 16, resolvedCiphertext.getRawCiphertext().length);
+ assertEquals(ClassicAlgorithm.AES, resolvedCiphertext.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),
+ resolvedCiphertext.getAlgorithm());
+ assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());
- assertArrayEquals(ciphertext.toBytes(),resolvedCiphertext.toBytes());
- assertArrayEquals(ciphertext.getRawCiphertext(),resolvedCiphertext.getRawCiphertext());
- assertEquals(ciphertext.getAlgorithm().name(),resolvedCiphertext.getAlgorithm().name());
- assertEquals(ciphertext.getAlgorithm().code(),resolvedCiphertext.getAlgorithm().code());
+ assertArrayEquals(ciphertext.toBytes(), resolvedCiphertext.toBytes());
+ assertArrayEquals(ciphertext.getRawCiphertext(), resolvedCiphertext.getRawCiphertext());
+ assertEquals(ciphertext.getAlgorithm(), resolvedCiphertext.getAlgorithm());
- algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
+ algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- symmetricEncryptionFunction.resolveCiphertext(ripemd160CiphertextBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ symmetricEncryptionFunction.resolveCiphertext(ripemd160CiphertextBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ 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 be0c6dc1..7c2bfdbe 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
@@ -1,16 +1,30 @@
package test.com.jd.blockchain.crypto.service.classic;
-import com.jd.blockchain.crypto.*;
-import com.jd.blockchain.utils.io.BytesUtils;
-import org.junit.Test;
-
-import java.util.Random;
-
import static com.jd.blockchain.crypto.CryptoAlgorithm.ASYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoAlgorithm.SIGNATURE_ALGORITHM;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+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.CryptoAlgorithm;
+import com.jd.blockchain.crypto.CryptoException;
+import com.jd.blockchain.crypto.CryptoKeyPair;
+import com.jd.blockchain.crypto.CryptoServiceProviders;
+import com.jd.blockchain.crypto.PrivKey;
+import com.jd.blockchain.crypto.PubKey;
+import com.jd.blockchain.crypto.SignatureDigest;
+import com.jd.blockchain.crypto.SignatureFunction;
+import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm;
+import com.jd.blockchain.utils.io.BytesUtils;
/**
* @author zhanglin33
@@ -60,10 +74,8 @@ public class ED25519SignatureFunctionTest {
assertEquals(PRIVATE.CODE,privKey.getKeyType().CODE);
assertEquals(32, privKey.getRawKeyBytes().length);
- assertEquals(algorithm.name(),pubKey.getAlgorithm().name());
- assertEquals(algorithm.code(),pubKey.getAlgorithm().code());
- assertEquals(algorithm.name(),privKey.getAlgorithm().name());
- assertEquals(algorithm.code(),privKey.getAlgorithm().code());
+ assertEquals(algorithm.code(),pubKey.getAlgorithm());
+ assertEquals(algorithm.code(),privKey.getAlgorithm());
assertEquals(2 + 1 + 32, pubKey.toBytes().length);
assertEquals(2 + 1 + 32, privKey.toBytes().length);
@@ -94,8 +106,7 @@ public class ED25519SignatureFunctionTest {
assertEquals(pubKey.getKeyType(),retrievedPubKey.getKeyType());
assertEquals(pubKey.getRawKeyBytes().length, retrievedPubKey.getRawKeyBytes().length);
- assertEquals(pubKey.getAlgorithm().name(),retrievedPubKey.getAlgorithm().name());
- assertEquals(pubKey.getAlgorithm().code(),retrievedPubKey.getAlgorithm().code());
+ assertEquals(pubKey.getAlgorithm(),retrievedPubKey.getAlgorithm());
assertArrayEquals(pubKey.toBytes(),retrievedPubKey.toBytes());
}
@@ -119,12 +130,11 @@ public class ED25519SignatureFunctionTest {
byte[] signatureBytes = signatureDigest.toBytes();
assertEquals(2 + 64, signatureBytes.length);
- CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm();
- assertEquals("ED25519",signatureAlgo.name());
+ assertEquals(ClassicAlgorithm.ED25519.code(),signatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
- signatureAlgo.code());
+ signatureDigest.getAlgorithm());
- byte[] algoBytes = CryptoAlgorithm.toBytes(signatureAlgo);
+ byte[] algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm());
byte[] rawSinatureBytes = signatureDigest.getRawDigest();
assertArrayEquals(BytesUtils.concat(algoBytes,rawSinatureBytes),signatureBytes);
}
@@ -192,9 +202,9 @@ public class ED25519SignatureFunctionTest {
assertEquals(PRIVATE.CODE,resolvedPrivKey.getKeyType().CODE);
assertEquals(32, resolvedPrivKey.getRawKeyBytes().length);
- assertEquals("ED25519",resolvedPrivKey.getAlgorithm().name());
+ assertEquals(ClassicAlgorithm.ED25519.code(),resolvedPrivKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
- resolvedPrivKey.getAlgorithm().code());
+ resolvedPrivKey.getAlgorithm());
assertArrayEquals(privKeyBytes,resolvedPrivKey.toBytes());
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
@@ -257,9 +267,9 @@ public class ED25519SignatureFunctionTest {
assertEquals(PUBLIC.CODE,resolvedPubKey.getKeyType().CODE);
assertEquals(32, resolvedPubKey.getRawKeyBytes().length);
- assertEquals("ED25519",resolvedPubKey.getAlgorithm().name());
+ assertEquals(ClassicAlgorithm.ED25519.code(),resolvedPubKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
- resolvedPubKey.getAlgorithm().code());
+ resolvedPubKey.getAlgorithm());
assertArrayEquals(pubKeyBytes,resolvedPubKey.toBytes());
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
@@ -336,9 +346,9 @@ public class ED25519SignatureFunctionTest {
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);
assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
- assertEquals("ED25519",resolvedSignatureDigest.getAlgorithm().name());
+ assertEquals(ClassicAlgorithm.ED25519,resolvedSignatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
- resolvedSignatureDigest.getAlgorithm().code());
+ resolvedSignatureDigest.getAlgorithm());
assertArrayEquals(signatureDigestBytes,resolvedSignatureDigest.toBytes());
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java
index da7fab02..81104494 100644
--- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java
+++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java
@@ -5,6 +5,7 @@ import com.jd.blockchain.crypto.CryptoException;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.crypto.HashFunction;
+import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;
@@ -22,152 +23,151 @@ import static org.junit.Assert.assertEquals;
*/
public class RIPEMD160HashFunctionTest {
- @Test
- public void getAlgorithmTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
- assertNotNull(algorithm);
+ @Test
+ public void getAlgorithmTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
+ assertNotNull(algorithm);
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
- assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
+ assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
- algorithm = CryptoServiceProviders.getAlgorithm("RIPEmd160");
- assertNotNull(algorithm);
+ algorithm = CryptoServiceProviders.getAlgorithm("RIPEmd160");
+ assertNotNull(algorithm);
- assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
+ assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
- algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD-160");
- assertNull(algorithm);
- }
+ algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD-160");
+ assertNull(algorithm);
+ }
- @Test
- public void hashTest(){
+ @Test
+ public void hashTest() {
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
- assertNotNull(algorithm);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
+ assertNotNull(algorithm);
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
- HashDigest digest = hashFunction.hash(data);
- byte[] rawDigestBytes = digest.getRawDigest();
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ HashDigest digest = hashFunction.hash(data);
+ byte[] rawDigestBytes = digest.getRawDigest();
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] digestBytes = digest.toBytes();
- assertEquals(160 / 8 + 2,digestBytes.length);
- assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes));
+ byte[] digestBytes = digest.toBytes();
+ assertEquals(160 / 8 + 2, digestBytes.length);
+ assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes));
- assertEquals(algorithm.code(),digest.getAlgorithm().code());
- assertEquals(algorithm.name(),digest.getAlgorithm().name());
+ assertEquals(algorithm.code(), digest.getAlgorithm());
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- data = null;
- hashFunction.hash(data);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
-
- @Test
- public void verifyTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- assertTrue(hashFunction.verify(digest,data));
- }
-
- @Test
- public void supportHashDigestTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- byte[] digestBytes = digest.toBytes();
- assertTrue(hashFunction.supportHashDigest(digestBytes));
-
- algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length);
- assertFalse(hashFunction.supportHashDigest(digestBytes));
- }
-
- @Test
- public void resolveHashDigestTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- byte[] digestBytes = digest.toBytes();
-
- HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
-
- assertEquals(160 / 8,resolvedDigest.getRawDigest().length);
- assertEquals("RIPEMD160",resolvedDigest.getAlgorithm().name());
- assertEquals((short) (HASH_ALGORITHM | ((byte) 25 & 0x00FF)),resolvedDigest.getAlgorithm().code());
- assertArrayEquals(digestBytes,resolvedDigest.toBytes());
-
- algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawDigestBytes = digest.getRawDigest();
- byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
-
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- hashFunction.resolveHashDigest(aesDigestBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
-
- algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
- algoBytes = CryptoAlgorithm.toBytes(algorithm);
- rawDigestBytes = digest.getRawDigest();
- byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
-
- expectedException = CryptoException.class;
- actualEx = null;
- try {
- hashFunction.resolveHashDigest(ripemd160DigestBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ data = null;
+ hashFunction.hash(data);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
+
+ @Test
+ public void verifyTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ assertTrue(hashFunction.verify(digest, data));
+ }
+
+ @Test
+ public void supportHashDigestTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ byte[] digestBytes = digest.toBytes();
+ assertTrue(hashFunction.supportHashDigest(digestBytes));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length);
+ assertFalse(hashFunction.supportHashDigest(digestBytes));
+ }
+
+ @Test
+ public void resolveHashDigestTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("RIPEMD160");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ byte[] digestBytes = digest.toBytes();
+
+ HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
+
+ assertEquals(160 / 8, resolvedDigest.getRawDigest().length);
+ assertEquals(ClassicAlgorithm.RIPEMD160, resolvedDigest.getAlgorithm());
+ assertEquals((short) (HASH_ALGORITHM | ((byte) 25 & 0x00FF)), resolvedDigest.getAlgorithm());
+ assertArrayEquals(digestBytes, resolvedDigest.toBytes());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawDigestBytes = digest.getRawDigest();
+ byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
+
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ hashFunction.resolveHashDigest(aesDigestBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
+ algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ rawDigestBytes = digest.getRawDigest();
+ byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
+
+ expectedException = CryptoException.class;
+ actualEx = null;
+ try {
+ hashFunction.resolveHashDigest(ripemd160DigestBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
}
diff --git a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java
index 020929c3..e6bb845d 100644
--- a/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java
+++ b/source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java
@@ -5,6 +5,7 @@ import com.jd.blockchain.crypto.CryptoException;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.crypto.HashFunction;
+import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;
@@ -22,152 +23,151 @@ import static org.junit.Assert.*;
public class SHA256HashFunctionTest {
- @Test
- public void getAlgorithmTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
+ @Test
+ public void getAlgorithmTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
- assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
+ assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
- algorithm = CryptoServiceProviders.getAlgorithm("SHa256");
- assertNotNull(algorithm);
+ algorithm = CryptoServiceProviders.getAlgorithm("SHa256");
+ assertNotNull(algorithm);
- assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
+ assertEquals(hashFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(hashFunction.getAlgorithm().code(), algorithm.code());
- algorithm = CryptoServiceProviders.getAlgorithm("sha-256");
- assertNull(algorithm);
- }
+ algorithm = CryptoServiceProviders.getAlgorithm("sha-256");
+ assertNull(algorithm);
+ }
- @Test
- public void hashTest(){
+ @Test
+ public void hashTest() {
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
- HashDigest digest = hashFunction.hash(data);
- byte[] rawDigestBytes = digest.getRawDigest();
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ HashDigest digest = hashFunction.hash(data);
+ byte[] rawDigestBytes = digest.getRawDigest();
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] digestBytes = digest.toBytes();
- assertEquals(256 / 8 + 2,digestBytes.length);
- assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes));
+ byte[] digestBytes = digest.toBytes();
+ assertEquals(256 / 8 + 2, digestBytes.length);
+ assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes));
- assertEquals(algorithm.code(),digest.getAlgorithm().code());
- assertEquals(algorithm.name(),digest.getAlgorithm().name());
+ assertEquals(algorithm.code(), digest.getAlgorithm());
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- data = null;
- hashFunction.hash(data);
- } catch (Exception e) {
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ data = null;
+ hashFunction.hash(data);
+ } catch (Exception e) {
actualEx = e;
}
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
-
- @Test
- public void verifyTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- assertTrue(hashFunction.verify(digest,data));
- }
-
- @Test
- public void supportHashDigestTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- byte[] digestBytes = digest.toBytes();
- assertTrue(hashFunction.supportHashDigest(digestBytes));
-
- algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length);
- assertFalse(hashFunction.supportHashDigest(digestBytes));
- }
-
- @Test
- public void resolveHashDigestTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
- assertNotNull(algorithm);
-
- HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
-
- HashDigest digest = hashFunction.hash(data);
-
- byte[] digestBytes = digest.toBytes();
-
- HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
-
- assertEquals(256 / 8,resolvedDigest.getRawDigest().length);
- assertEquals("SHA256",resolvedDigest.getAlgorithm().name());
- assertEquals((short) (HASH_ALGORITHM | ((byte) 24 & 0x00FF)),resolvedDigest.getAlgorithm().code());
- assertArrayEquals(digestBytes,resolvedDigest.toBytes());
-
- algorithm = CryptoServiceProviders.getAlgorithm("aes");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawDigestBytes = digest.getRawDigest();
- byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
-
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- hashFunction.resolveHashDigest(aesDigestBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
-
- algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
- assertNotNull(algorithm);
- algoBytes = CryptoAlgorithm.toBytes(algorithm);
- rawDigestBytes = digest.getRawDigest();
- byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
-
- expectedException = CryptoException.class;
- actualEx = null;
- try {
- hashFunction.resolveHashDigest(ripemd160DigestBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
+
+ @Test
+ public void verifyTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ assertTrue(hashFunction.verify(digest, data));
+ }
+
+ @Test
+ public void supportHashDigestTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ byte[] digestBytes = digest.toBytes();
+ assertTrue(hashFunction.supportHashDigest(digestBytes));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ System.arraycopy(algoBytes, 0, digestBytes, 0, algoBytes.length);
+ assertFalse(hashFunction.supportHashDigest(digestBytes));
+ }
+
+ @Test
+ public void resolveHashDigestTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sha256");
+ assertNotNull(algorithm);
+
+ HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
+
+ HashDigest digest = hashFunction.hash(data);
+
+ byte[] digestBytes = digest.toBytes();
+
+ HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
+
+ assertEquals(256 / 8, resolvedDigest.getRawDigest().length);
+ assertEquals(ClassicAlgorithm.SHA256, resolvedDigest.getAlgorithm());
+ assertEquals((short) (HASH_ALGORITHM | ((byte) 24 & 0x00FF)), resolvedDigest.getAlgorithm());
+ assertArrayEquals(digestBytes, resolvedDigest.toBytes());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawDigestBytes = digest.getRawDigest();
+ byte[] aesDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
+
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ hashFunction.resolveHashDigest(aesDigestBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
+ assertNotNull(algorithm);
+ algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ rawDigestBytes = digest.getRawDigest();
+ byte[] ripemd160DigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
+
+ expectedException = CryptoException.class;
+ actualEx = null;
+ try {
+ hashFunction.resolveHashDigest(ripemd160DigestBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AddressEncoding.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AddressEncoding.java
index 77db928b..a1bf5e9c 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AddressEncoding.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AddressEncoding.java
@@ -55,7 +55,7 @@ public class AddressEncoding {
public static Bytes generateAddress(PubKey pubKey) {
byte[] h1Bytes = ShaUtils.hash_256(pubKey.getRawKeyBytes());
byte[] h2Bytes = RipeMD160Utils.hash(h1Bytes);
- byte[] xBytes = BytesUtils.concat(new byte[] { AddressVersion.V1.CODE}, CryptoAlgorithm.toBytes(pubKey.getAlgorithm()), h2Bytes);
+ byte[] xBytes = BytesUtils.concat(new byte[] { AddressVersion.V1.CODE}, BytesUtils.toBytes(pubKey.getAlgorithm()), h2Bytes);
byte[] checksum = Arrays.copyOf(ShaUtils.hash_256(ShaUtils.hash_256(xBytes)), 4);
byte[] addressBytes = BytesUtils.concat(xBytes, checksum);
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AsymmetricCiphertext.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AsymmetricCiphertext.java
index 852d3a65..279d9c60 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AsymmetricCiphertext.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/AsymmetricCiphertext.java
@@ -11,7 +11,7 @@ public class AsymmetricCiphertext extends BaseCryptoBytes implements Ciphertext
}
@Override
- protected boolean support(CryptoAlgorithm algorithm) {
+ protected boolean support(short algorithm) {
return CryptoAlgorithm.isAsymmetricEncryptionAlgorithm(algorithm);
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java
index e51460f8..c986569a 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java
@@ -8,47 +8,60 @@ import com.jd.blockchain.utils.io.BytesUtils;
public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes {
- private CryptoAlgorithm algorithm;
-
+ private short algorithm;
+
public BaseCryptoBytes() {
super();
}
+
+ public BaseCryptoBytes(short algorithm, byte[] rawCryptoBytes) {
+ super(encodeBytes(algorithm, rawCryptoBytes));
+ this.algorithm = algorithm;
+ }
public BaseCryptoBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
super(encodeBytes(algorithm, rawCryptoBytes));
- this.algorithm = algorithm;
+ this.algorithm = algorithm.code();
}
public BaseCryptoBytes(byte[] cryptoBytes) {
super(cryptoBytes);
- CryptoAlgorithm algorithm = decodeAlgorithm(cryptoBytes);
+ short algorithm = decodeAlgorithm(cryptoBytes);
if (!support(algorithm)) {
- throw new CryptoException("Not supported algorithm[" + algorithm.toString() + "]!");
+ throw new CryptoException("Not supported algorithm [code:" + algorithm + "]!");
}
this.algorithm = algorithm;
}
+
+ static byte[] encodeBytes(short algorithm, byte[] rawCryptoBytes) {
+ return BytesUtils.concat(BytesUtils.toBytes(algorithm), rawCryptoBytes);
+ }
static byte[] encodeBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
return BytesUtils.concat(CryptoAlgorithm.toBytes(algorithm), rawCryptoBytes);
}
-
- static CryptoAlgorithm decodeAlgorithm(byte[] cryptoBytes) {
- short algorithmCode = BytesUtils.toShort(cryptoBytes, 0);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm(algorithmCode);
- if (algorithm == null) {
- throw new CryptoException("The algorithm with code[" + algorithmCode + "] is not supported!");
- }
- return algorithm;
+
+ static short decodeAlgorithm(byte[] cryptoBytes) {
+ return CryptoAlgorithm.resolveCode(cryptoBytes);
}
- protected abstract boolean support(CryptoAlgorithm algorithm);
+// static CryptoAlgorithm decodeAlgorithm(byte[] cryptoBytes) {
+// short algorithmCode = BytesUtils.toShort(cryptoBytes, 0);
+// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm(algorithmCode);
+// if (algorithm == null) {
+// throw new CryptoException("The algorithm with code[" + algorithmCode + "] is not supported!");
+// }
+// return algorithm;
+// }
+
+ protected abstract boolean support(short algorithm);
protected byte[] resolveRawCryptoBytes(byte[] cryptoBytes) {
return Arrays.copyOfRange(cryptoBytes, 1, cryptoBytes.length);
}
@Override
- public CryptoAlgorithm getAlgorithm() {
+ public short getAlgorithm() {
return algorithm;
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoKey.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoKey.java
index db95dbd4..75785f48 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoKey.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoKey.java
@@ -13,6 +13,10 @@ public abstract class BaseCryptoKey extends BaseCryptoBytes implements CryptoKey
// public BaseCryptoKey() {
// super();
// }
+
+ protected BaseCryptoKey(short algorithm, byte[] rawKeyBytes, CryptoKeyType keyType) {
+ super(algorithm, encodeKeyBytes(rawKeyBytes, keyType));
+ }
protected BaseCryptoKey(CryptoAlgorithm algorithm, byte[] rawKeyBytes, CryptoKeyType keyType) {
super(algorithm, encodeKeyBytes(rawKeyBytes, keyType));
@@ -35,7 +39,7 @@ public abstract class BaseCryptoKey extends BaseCryptoBytes implements CryptoKey
}
@Override
- protected boolean support(CryptoAlgorithm algorithm) {
+ protected boolean support(short algorithm) {
return CryptoAlgorithm.hasAsymmetricKey(algorithm) || CryptoAlgorithm.hasSymmetricKey(algorithm);
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java
index aa2303ab..19688f65 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java
@@ -1,5 +1,8 @@
package com.jd.blockchain.crypto;
+import java.io.InputStream;
+import java.io.OutputStream;
+
import com.jd.blockchain.binaryproto.DataContract;
import com.jd.blockchain.binaryproto.DataField;
import com.jd.blockchain.consts.TypeCodes;
@@ -90,6 +93,14 @@ public interface CryptoAlgorithm {
return BytesUtils.toShort(algorithmBytes, offset);
}
+ static short resolveCode(InputStream in) {
+ return BytesUtils.readShort(in);
+ }
+
+ static int writeCode(short code, OutputStream out) {
+ return BytesUtils.writeShort(code, out);
+ }
+
static boolean match(CryptoAlgorithm algorithm, byte[] algorithmBytes) {
return algorithm.code() == BytesUtils.toShort(algorithmBytes, 0);
}
@@ -116,6 +127,24 @@ public interface CryptoAlgorithm {
return HASH_ALGORITHM == (algorithm.code() & HASH_ALGORITHM);
}
+ /**
+ * 是否属于摘要算法;
+ *
+ * @return
+ */
+ static boolean isHashAlgorithm(short algorithm) {
+ return HASH_ALGORITHM == (algorithm & HASH_ALGORITHM);
+ }
+
+ /**
+ * 是否属于签名算法;
+ *
+ * @return
+ */
+ static boolean isSignatureAlgorithm(short algorithm) {
+ return SIGNATURE_ALGORITHM == (algorithm & SIGNATURE_ALGORITHM);
+ }
+
/**
* 是否属于签名算法;
*
@@ -125,6 +154,15 @@ public interface CryptoAlgorithm {
return SIGNATURE_ALGORITHM == (algorithm.code() & SIGNATURE_ALGORITHM);
}
+ /**
+ * 是否属于加密算法;
+ *
+ * @return
+ */
+ static boolean isEncryptionAlgorithm(short algorithm) {
+ return ENCRYPTION_ALGORITHM == (algorithm & ENCRYPTION_ALGORITHM);
+ }
+
/**
* 是否属于加密算法;
*
@@ -143,6 +181,15 @@ public interface CryptoAlgorithm {
return EXT_ALGORITHM == (algorithm.code() & 0xF000);
}
+ /**
+ * 算法是否包含非对称密钥;
+ *
+ * @return
+ */
+ static boolean hasAsymmetricKey(short algorithm) {
+ return ASYMMETRIC_KEY == (algorithm & ASYMMETRIC_KEY);
+ }
+
/**
* 算法是否包含非对称密钥;
*
@@ -152,6 +199,15 @@ public interface CryptoAlgorithm {
return ASYMMETRIC_KEY == (algorithm.code() & ASYMMETRIC_KEY);
}
+ /**
+ * 算法是否包含对称密钥;
+ *
+ * @return
+ */
+ static boolean hasSymmetricKey(short algorithm) {
+ return SYMMETRIC_KEY == (algorithm & SYMMETRIC_KEY);
+ }
+
/**
* 算法是否包含对称密钥;
*
@@ -171,6 +227,16 @@ public interface CryptoAlgorithm {
return isEncryptionAlgorithm(algorithm) && hasSymmetricKey(algorithm);
}
+ /**
+ * 是否属于非对称加密算法;
+ *
+ * @param algorithm
+ * @return
+ */
+ static boolean isAsymmetricEncryptionAlgorithm(short algorithm) {
+ return isEncryptionAlgorithm(algorithm) && hasAsymmetricKey(algorithm);
+ }
+
/**
* 是否属于非对称加密算法;
*
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoBytes.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoBytes.java
index 8950a1ee..6397f1f3 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoBytes.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoBytes.java
@@ -20,7 +20,7 @@ public interface CryptoBytes extends BytesSerializable {
*
* @return
*/
- CryptoAlgorithm getAlgorithm();
+ short getAlgorithm();
/**
* 返回编码后的摘要信息;
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyPair.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyPair.java
index 9caf28d4..283d217b 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyPair.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyPair.java
@@ -6,7 +6,7 @@ public class CryptoKeyPair {
private PrivKey privKey;
- public CryptoAlgorithm getAlgorithm() {
+ public short getAlgorithm() {
return pubKey.getAlgorithm();
}
@@ -19,7 +19,7 @@ public class CryptoKeyPair {
}
public CryptoKeyPair(PubKey pubKey, PrivKey privKey) {
- if (!CryptoAlgorithm.equals(pubKey.getAlgorithm(), privKey.getAlgorithm())) {
+ if (pubKey.getAlgorithm() != privKey.getAlgorithm()) {
throw new IllegalArgumentException("The algorithms of PubKey and PrivKey don't match!");
}
this.pubKey = pubKey;
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/HashDigest.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/HashDigest.java
index de317352..761dfb9e 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/HashDigest.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/HashDigest.java
@@ -19,7 +19,7 @@ public class HashDigest extends BaseCryptoBytes implements CryptoDigest,Serializ
}
@Override
- protected boolean support(CryptoAlgorithm algorithm) {
+ protected boolean support(short algorithm) {
return CryptoAlgorithm.isHashAlgorithm(algorithm);
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java
index f1c7466d..b15bb5e7 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java
@@ -8,6 +8,10 @@ package com.jd.blockchain.crypto;
*/
public class PrivKey extends BaseCryptoKey {
private static final long serialVersionUID = 6265440395252295646L;
+
+ public PrivKey(short algorithm, byte[] rawCryptoBytes) {
+ super(algorithm, rawCryptoBytes, CryptoKeyType.PRIVATE);
+ }
public PrivKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
super(algorithm, rawCryptoBytes, CryptoKeyType.PRIVATE);
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java
index f38f48bb..ca5b43c8 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java
@@ -9,6 +9,10 @@ package com.jd.blockchain.crypto;
public class PubKey extends BaseCryptoKey {
private static final long serialVersionUID = -2055071197736385328L;
+
+ public PubKey(short algorithm, byte[] rawCryptoBytes) {
+ super(algorithm, rawCryptoBytes, CryptoKeyType.PUBLIC);
+ }
public PubKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
super(algorithm, rawCryptoBytes, CryptoKeyType.PUBLIC);
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SignatureDigest.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SignatureDigest.java
index 4829e5f8..f4164931 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SignatureDigest.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SignatureDigest.java
@@ -14,7 +14,7 @@ public class SignatureDigest extends BaseCryptoBytes implements CryptoDigest {
}
@Override
- protected boolean support(CryptoAlgorithm algorithm) {
+ protected boolean support(short algorithm) {
return CryptoAlgorithm.isSignatureAlgorithm(algorithm);
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricCiphertext.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricCiphertext.java
index e88690e5..1f811478 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricCiphertext.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricCiphertext.java
@@ -11,7 +11,7 @@ public class SymmetricCiphertext extends BaseCryptoBytes implements Ciphertext {
}
@Override
- protected boolean support(CryptoAlgorithm algorithm) {
+ protected boolean support(short algorithm) {
return CryptoAlgorithm.isEncryptionAlgorithm(algorithm) && CryptoAlgorithm.hasSymmetricKey(algorithm);
}
diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/serialize/ByteArrayObjectSerializer.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/serialize/ByteArrayObjectSerializer.java
index 436a62e8..282605ed 100644
--- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/serialize/ByteArrayObjectSerializer.java
+++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/serialize/ByteArrayObjectSerializer.java
@@ -1,5 +1,7 @@
package com.jd.blockchain.crypto.serialize;
+import java.lang.reflect.Type;
+
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.jd.blockchain.crypto.HashDigest;
@@ -8,9 +10,6 @@ import com.jd.blockchain.crypto.SignatureDigest;
import com.jd.blockchain.utils.Bytes;
import com.jd.blockchain.utils.io.BytesSlice;
-import java.io.IOException;
-import java.lang.reflect.Type;
-
public class ByteArrayObjectSerializer implements ObjectSerializer {
private Class> clazz;
@@ -51,11 +50,13 @@ public class ByteArrayObjectSerializer implements ObjectSerializer {
this.value = value;
}
- public String getValue() {
+ @SuppressWarnings("unused")
+ public String getValue() {
return value;
}
- public void setValue(String value) {
+ @SuppressWarnings("unused")
+ public void setValue(String value) {
this.value = value;
}
}
diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java
index bc2bd33e..531c4801 100644
--- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java
+++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java
@@ -5,16 +5,25 @@ import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
-import com.jd.blockchain.crypto.*;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
+import com.jd.blockchain.crypto.AsymmetricCiphertext;
+import com.jd.blockchain.crypto.AsymmetricEncryptionFunction;
+import com.jd.blockchain.crypto.Ciphertext;
+import com.jd.blockchain.crypto.CryptoAlgorithm;
+import com.jd.blockchain.crypto.CryptoException;
+import com.jd.blockchain.crypto.CryptoKeyPair;
+import com.jd.blockchain.crypto.PrivKey;
+import com.jd.blockchain.crypto.PubKey;
+import com.jd.blockchain.crypto.SignatureDigest;
+import com.jd.blockchain.crypto.SignatureFunction;
import com.jd.blockchain.crypto.utils.sm.SM2Utils;
public class SM2CryptoFunction implements AsymmetricEncryptionFunction, SignatureFunction {
- private static final CryptoAlgorithm SM2 = SMCryptoService.SM2_ALGORITHM;
+ private static final CryptoAlgorithm SM2 = SMAlgorithm.SM2;
private static final int ECPOINT_SIZE = 65;
private static final int PRIVKEY_SIZE = 32;
@@ -39,7 +48,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
}
// 验证密钥数据的算法标识对应SM2算法
- if (pubKey.getAlgorithm().code() != SM2.code()) {
+ if (pubKey.getAlgorithm() != SM2.code()) {
throw new CryptoException("The is not sm2 public key!");
}
@@ -59,12 +68,12 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
}
// 验证密钥数据的算法标识对应SM2算法
- if (privKey.getAlgorithm().code() != SM2.code()) {
+ if (privKey.getAlgorithm() != SM2.code()) {
throw new CryptoException("This key is not SM2 private key!");
}
// 验证密文数据的算法标识对应SM2签名算法,并且原始摘要长度为64字节
- if (ciphertext.getAlgorithm().code() != SM2.code()
+ if (ciphertext.getAlgorithm() != SM2.code()
|| rawCiphertextBytes.length < ECPOINT_SIZE + HASHDIGEST_SIZE) {
throw new CryptoException("This is not SM2 ciphertext!");
}
@@ -147,7 +156,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
}
// 验证密钥数据的算法标识对应SM2签名算法
- if (privKey.getAlgorithm().code() != SM2.code()) {
+ if (privKey.getAlgorithm() != SM2.code()) {
throw new CryptoException("This key is not SM2 private key!");
}
@@ -167,12 +176,12 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
}
// 验证密钥数据的算法标识对应SM2签名算法
- if (pubKey.getAlgorithm().code() != SM2.code()) {
+ if (pubKey.getAlgorithm() != SM2.code()) {
throw new CryptoException("This key is not SM2 public key!");
}
// 验证签名数据的算法标识对应SM2签名算法,并且原始签名长度为64字节
- if (digest.getAlgorithm().code() != SM2.code() || rawDigestBytes.length != SIGNATUREDIGEST_SIZE) {
+ if (digest.getAlgorithm() != SM2.code() || rawDigestBytes.length != SIGNATUREDIGEST_SIZE) {
throw new CryptoException("This is not SM2 signature digest!");
}
diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java
index 32983e57..85fb943c 100644
--- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java
+++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM3HashFunction.java
@@ -11,7 +11,7 @@ import com.jd.blockchain.crypto.utils.sm.SM3Utils;
public class SM3HashFunction implements HashFunction {
- private static final CryptoAlgorithm SM3 = SMCryptoService.SM3_ALGORITHM;
+ private static final CryptoAlgorithm SM3 = SMAlgorithm.SM3;
private static final int DIGEST_BYTES = 256 / 8;
diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java
index 8810dd40..31932649 100644
--- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java
+++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java
@@ -13,7 +13,7 @@ import com.jd.blockchain.crypto.utils.sm.SM4Utils;
public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
- private static final CryptoAlgorithm SM4 = SMCryptoService.SM4_ALGORITHM;
+ private static final CryptoAlgorithm SM4 = SMAlgorithm.SM4;
private static final int KEY_SIZE = 128 / 8;
private static final int BLOCK_SIZE = 128 / 8;
@@ -38,7 +38,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密钥数据的算法标识对应SM4算法
- if (key.getAlgorithm().code() != SM4.code()) {
+ if (key.getAlgorithm() != SM4.code()) {
throw new CryptoException("The is not SM4 symmetric key!");
}
@@ -53,7 +53,8 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
try {
byte[] buffBytes = new byte[PLAINTEXT_BUFFER_LENGTH];
- // The final byte of plaintextWithPadding represents the length of padding in the first 256 bytes,
+ // The final byte of plaintextWithPadding represents the length of padding in
+ // the first 256 bytes,
// and the padded value in hexadecimal
byte[] plaintextWithPadding = new byte[buffBytes.length + 1];
@@ -62,21 +63,21 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
int len;
int i;
- while((len=in.read(buffBytes)) > 0) {
+ while ((len = in.read(buffBytes)) > 0) {
padding = (byte) (PLAINTEXT_BUFFER_LENGTH - len);
i = len;
while (i < plaintextWithPadding.length) {
plaintextWithPadding[i] = padding;
i++;
}
- out.write(encrypt(key,plaintextWithPadding).toBytes());
+ out.write(encrypt(key, plaintextWithPadding).toBytes());
}
-// byte[] sm4Data = new byte[in.available()];
-// in.read(sm4Data);
-// in.close();
-//
-// out.write(encrypt(key, sm4Data).toBytes());
-// out.close();
+ // byte[] sm4Data = new byte[in.available()];
+ // in.read(sm4Data);
+ // in.close();
+ //
+ // out.write(encrypt(key, sm4Data).toBytes());
+ // out.close();
} catch (IOException e) {
throw new CryptoException(e.getMessage(), e);
}
@@ -94,7 +95,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密钥数据的算法标识对应SM4算法
- if (key.getAlgorithm().code() != SM4.code()) {
+ if (key.getAlgorithm() != SM4.code()) {
throw new CryptoException("The is not SM4 symmetric key!");
}
@@ -104,7 +105,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
}
// 验证密文数据算法标识对应SM4算法
- if (ciphertext.getAlgorithm().code() != SM4.code()) {
+ if (ciphertext.getAlgorithm() != SM4.code()) {
throw new CryptoException("This is not SM4 ciphertext!");
}
@@ -122,7 +123,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
byte padding;
byte[] plaintext;
- int len,i;
+ int len, i;
while ((len = in.read(buffBytes)) > 0) {
if (len != CIPHERTEXT_BUFFER_LENGTH) {
throw new CryptoException("inputStream's length is wrong!");
@@ -131,7 +132,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
throw new CryptoException("InputStream is not valid SM4 ciphertext!");
}
- plaintextWithPadding = decrypt(key,resolveCiphertext(buffBytes));
+ plaintextWithPadding = decrypt(key, resolveCiphertext(buffBytes));
if (plaintextWithPadding.length != (PLAINTEXT_BUFFER_LENGTH + 1)) {
throw new CryptoException("The decrypted plaintext is invalid");
@@ -148,19 +149,19 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
i--;
}
plaintext = new byte[PLAINTEXT_BUFFER_LENGTH - padding];
- System.arraycopy(plaintextWithPadding,0,plaintext,0,plaintext.length);
+ System.arraycopy(plaintextWithPadding, 0, plaintext, 0, plaintext.length);
out.write(plaintext);
}
-// byte[] sm4Data = new byte[in.available()];
-// in.read(sm4Data);
-// in.close();
-//
-// if (!supportCiphertext(sm4Data)) {
-// throw new CryptoException("InputStream is not valid SM4 ciphertext!");
-// }
-//
-// out.write(decrypt(key, resolveCiphertext(sm4Data)));
-// out.close();
+ // byte[] sm4Data = new byte[in.available()];
+ // in.read(sm4Data);
+ // in.close();
+ //
+ // if (!supportCiphertext(sm4Data)) {
+ // throw new CryptoException("InputStream is not valid SM4 ciphertext!");
+ // }
+ //
+ // out.write(decrypt(key, resolveCiphertext(sm4Data)));
+ // out.close();
} catch (IOException e) {
throw new CryptoException(e.getMessage(), e);
}
diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMAlgorithm.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMAlgorithm.java
new file mode 100644
index 00000000..62e13a06
--- /dev/null
+++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMAlgorithm.java
@@ -0,0 +1,17 @@
+package com.jd.blockchain.crypto.service.sm;
+
+import com.jd.blockchain.crypto.CryptoAlgorithm;
+import com.jd.blockchain.crypto.CryptoAlgorithmDefinition;
+
+public final class SMAlgorithm {
+
+ public static final CryptoAlgorithm SM2 = CryptoAlgorithmDefinition.defineSignature("SM2", true, (byte) 2);
+
+ public static final CryptoAlgorithm SM3 = CryptoAlgorithmDefinition.defineHash("SM3", (byte) 3);
+
+ public static final CryptoAlgorithm SM4 = CryptoAlgorithmDefinition.defineSymmetricEncryption("SM4", (byte) 4);
+
+ private SMAlgorithm() {
+ }
+
+}
diff --git a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMCryptoService.java b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMCryptoService.java
index a4579d27..001a699f 100644
--- a/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMCryptoService.java
+++ b/source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SMCryptoService.java
@@ -5,8 +5,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import com.jd.blockchain.crypto.CryptoAlgorithm;
-import com.jd.blockchain.crypto.CryptoAlgorithmDefinition;
import com.jd.blockchain.crypto.CryptoFunction;
import com.jd.blockchain.crypto.CryptoService;
import com.jd.blockchain.provider.NamedProvider;
@@ -20,14 +18,6 @@ import com.jd.blockchain.provider.NamedProvider;
@NamedProvider("SM-SOFTWARE")
public class SMCryptoService implements CryptoService {
- public static final CryptoAlgorithm SM2_ALGORITHM = CryptoAlgorithmDefinition.defineSignature("SM2",
- true, (byte) 2);
-
- public static final CryptoAlgorithm SM3_ALGORITHM = CryptoAlgorithmDefinition.defineHash("SM3", (byte) 3);
-
- public static final CryptoAlgorithm SM4_ALGORITHM = CryptoAlgorithmDefinition.defineSymmetricEncryption("SM4",
- (byte) 4);
-
public static final SM2CryptoFunction SM2 = new SM2CryptoFunction();
public static final SM3HashFunction SM3 = new SM3HashFunction();
public static final SM4EncryptionFunction SM4 = new SM4EncryptionFunction();
diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java
index e49283b8..74221938 100644
--- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java
+++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java
@@ -1,6 +1,7 @@
package test.com.jd.blockchain.crypto.service.sm;
import com.jd.blockchain.crypto.*;
+import com.jd.blockchain.crypto.service.sm.SMAlgorithm;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;
@@ -19,479 +20,469 @@ import static org.junit.Assert.*;
*/
public class SM2CyptoFunctionTest {
- @Test
- public void getAlgorithmTest(){
+ @Test
+ public void getAlgorithmTest() {
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());
- assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());
+ algorithm = CryptoServiceProviders.getAlgorithm("sM2");
+ assertNotNull(algorithm);
- algorithm = CryptoServiceProviders.getAlgorithm("sM2");
- assertNotNull(algorithm);
+ assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());
- assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());
+ algorithm = CryptoServiceProviders.getAlgorithm("sm22");
+ assertNull(algorithm);
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm22");
- assertNull(algorithm);
- }
+ @Test
+ public void generateKeyPairTest() {
- @Test
- public void generateKeyPairTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PubKey pubKey = keyPair.getPubKey();
+ PrivKey privKey = keyPair.getPrivKey();
- PubKey pubKey = keyPair.getPubKey();
- PrivKey privKey = keyPair.getPrivKey();
+ assertEquals(PUBLIC.CODE, pubKey.getKeyType().CODE);
+ assertEquals(65, pubKey.getRawKeyBytes().length);
+ assertEquals(PRIVATE.CODE, privKey.getKeyType().CODE);
+ assertEquals(32, privKey.getRawKeyBytes().length);
- assertEquals(PUBLIC.CODE,pubKey.getKeyType().CODE);
- assertEquals(65, pubKey.getRawKeyBytes().length);
- assertEquals(PRIVATE.CODE,privKey.getKeyType().CODE);
- assertEquals(32, privKey.getRawKeyBytes().length);
+ assertEquals(algorithm.code(), pubKey.getAlgorithm());
+ assertEquals(algorithm.code(), privKey.getAlgorithm());
- assertEquals(algorithm.name(),pubKey.getAlgorithm().name());
- assertEquals(algorithm.code(),pubKey.getAlgorithm().code());
- assertEquals(algorithm.name(),privKey.getAlgorithm().name());
- assertEquals(algorithm.code(),privKey.getAlgorithm().code());
+ assertEquals(2 + 1 + 65, pubKey.toBytes().length);
+ assertEquals(2 + 1 + 32, privKey.toBytes().length);
- assertEquals(2 + 1 + 65, pubKey.toBytes().length);
- assertEquals(2 + 1 + 32, privKey.toBytes().length);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE };
+ byte[] rawPubKeyBytes = pubKey.getRawKeyBytes();
+ byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
+ assertArrayEquals(BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawPubKeyBytes), pubKey.toBytes());
+ assertArrayEquals(BytesUtils.concat(algoBytes, privKeyTypeBytes, rawPrivKeyBytes), privKey.toBytes());
+ }
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE};
- byte[] rawPubKeyBytes = pubKey.getRawKeyBytes();
- byte[] rawPrivKeyBytes = privKey.getRawKeyBytes();
- assertArrayEquals(BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawPubKeyBytes),pubKey.toBytes());
- assertArrayEquals(BytesUtils.concat(algoBytes,privKeyTypeBytes,rawPrivKeyBytes),privKey.toBytes());
- }
+ @Test
+ public void retrievePubKeyTest() {
- @Test
- public void retrievePubKeyTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PubKey pubKey = keyPair.getPubKey();
+ PrivKey privKey = keyPair.getPrivKey();
- PubKey pubKey = keyPair.getPubKey();
- PrivKey privKey = keyPair.getPrivKey();
+ PubKey retrievedPubKey = signatureFunction.retrievePubKey(privKey);
- PubKey retrievedPubKey = signatureFunction.retrievePubKey(privKey);
+ assertEquals(pubKey.getKeyType(), retrievedPubKey.getKeyType());
+ assertEquals(pubKey.getRawKeyBytes().length, retrievedPubKey.getRawKeyBytes().length);
+ assertEquals(pubKey.getAlgorithm(), retrievedPubKey.getAlgorithm());
+ assertArrayEquals(pubKey.toBytes(), retrievedPubKey.toBytes());
+ }
- assertEquals(pubKey.getKeyType(),retrievedPubKey.getKeyType());
- assertEquals(pubKey.getRawKeyBytes().length, retrievedPubKey.getRawKeyBytes().length);
- assertEquals(pubKey.getAlgorithm().name(),retrievedPubKey.getAlgorithm().name());
- assertEquals(pubKey.getAlgorithm().code(),retrievedPubKey.getAlgorithm().code());
- assertArrayEquals(pubKey.toBytes(),retrievedPubKey.toBytes());
- }
+ @Test
+ public void signTest() {
- @Test
- public void signTest(){
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PrivKey privKey = keyPair.getPrivKey();
+ SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);
- PrivKey privKey = keyPair.getPrivKey();
- SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
+ byte[] signatureBytes = signatureDigest.toBytes();
- byte[] signatureBytes = signatureDigest.toBytes();
+ assertEquals(2 + 64, signatureBytes.length);
+ assertEquals(algorithm.code(), signatureDigest.getAlgorithm());
- assertEquals(2 + 64, signatureBytes.length);
- CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm();
- assertEquals(algorithm.name(),signatureDigest.getAlgorithm().name());
- assertEquals(algorithm.code(),signatureDigest.getAlgorithm().code());
+ assertEquals(SMAlgorithm.SM2, signatureDigest.getAlgorithm());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ signatureDigest.getAlgorithm());
- assertEquals("SM2",signatureAlgo.name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- signatureAlgo.code());
+ byte[] algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm());
+ byte[] rawSinatureBytes = signatureDigest.getRawDigest();
+ assertArrayEquals(BytesUtils.concat(algoBytes, rawSinatureBytes), signatureBytes);
+ }
- byte[] algoBytes = CryptoAlgorithm.toBytes(signatureAlgo);
- byte[] rawSinatureBytes = signatureDigest.getRawDigest();
- assertArrayEquals(BytesUtils.concat(algoBytes,rawSinatureBytes),signatureBytes);
- }
+ @Test
+ public void verifyTest() {
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- @Test
- public void verifyTest(){
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PubKey pubKey = keyPair.getPubKey();
+ PrivKey privKey = keyPair.getPrivKey();
+ SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);
- PubKey pubKey = keyPair.getPubKey();
- PrivKey privKey = keyPair.getPrivKey();
- SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
+ assertTrue(signatureFunction.verify(signatureDigest, pubKey, data));
+ }
- assertTrue(signatureFunction.verify(signatureDigest,pubKey,data));
- }
+ @Test
+ public void encryptTest() {
- @Test
- public void encryptTest(){
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ AsymmetricEncryptionFunction asymmetricEncryptionFunction = CryptoServiceProviders
+ .getAsymmetricEncryptionFunction(algorithm);
- AsymmetricEncryptionFunction asymmetricEncryptionFunction =
- CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm);
+ CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
- CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
+ PubKey pubKey = keyPair.getPubKey();
- PubKey pubKey = keyPair.getPubKey();
+ Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data);
- Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data);
+ byte[] ciphertextBytes = ciphertext.toBytes();
+ assertEquals(2 + 65 + 256 / 8 + 1024, ciphertextBytes.length);
+ assertEquals(SMAlgorithm.SM2, ciphertext.getAlgorithm());
- byte[] ciphertextBytes = ciphertext.toBytes();
- assertEquals(2 + 65 + 256 / 8 + 1024, ciphertextBytes.length);
- CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
- assertEquals("SM2",ciphertext.getAlgorithm().name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- ciphertextAlgo.code());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ ciphertext.getAlgorithm());
- byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- assertArrayEquals(BytesUtils.concat(algoBytes,rawCiphertextBytes),ciphertextBytes);
- }
+ byte[] algoBytes = BytesUtils.toBytes(ciphertext.getAlgorithm());
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ assertArrayEquals(BytesUtils.concat(algoBytes, rawCiphertextBytes), ciphertextBytes);
+ }
+ @Test
+ public void decryptTest() {
- @Test
- public void decryptTest(){
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ AsymmetricEncryptionFunction asymmetricEncryptionFunction = CryptoServiceProviders
+ .getAsymmetricEncryptionFunction(algorithm);
- AsymmetricEncryptionFunction asymmetricEncryptionFunction =
- CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm);
+ CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
- CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
+ PubKey pubKey = keyPair.getPubKey();
+ PrivKey privKey = keyPair.getPrivKey();
- PubKey pubKey = keyPair.getPubKey();
- PrivKey privKey = keyPair.getPrivKey();
+ Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data);
- Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data);
+ byte[] decryptedPlaintext = asymmetricEncryptionFunction.decrypt(privKey, ciphertext);
- byte[] decryptedPlaintext = asymmetricEncryptionFunction.decrypt(privKey,ciphertext);
+ assertArrayEquals(data, decryptedPlaintext);
+ }
- assertArrayEquals(data,decryptedPlaintext);
- }
+ @Test
+ public void supportPrivKeyTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- @Test
- public void supportPrivKeyTest(){
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ PrivKey privKey = keyPair.getPrivKey();
+ byte[] privKeyBytes = privKey.toBytes();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ assertTrue(signatureFunction.supportPrivKey(privKeyBytes));
- PrivKey privKey = keyPair.getPrivKey();
- byte[] privKeyBytes = privKey.toBytes();
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = privKey.getRawKeyBytes();
+ byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
- assertTrue(signatureFunction.supportPrivKey(privKeyBytes));
+ assertFalse(signatureFunction.supportPrivKey(sm3PubKeyBytes));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = privKey.getRawKeyBytes();
- byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ @Test
+ public void resolvePrivKeyTest() {
- assertFalse(signatureFunction.supportPrivKey(sm3PubKeyBytes));
- }
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- @Test
- public void resolvePrivKeyTest(){
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ PrivKey privKey = keyPair.getPrivKey();
+ byte[] privKeyBytes = privKey.toBytes();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes);
- PrivKey privKey = keyPair.getPrivKey();
- byte[] privKeyBytes = privKey.toBytes();
+ assertEquals(PRIVATE.CODE, resolvedPrivKey.getKeyType().CODE);
+ assertEquals(32, resolvedPrivKey.getRawKeyBytes().length);
+ assertEquals(SMAlgorithm.SM2, resolvedPrivKey.getAlgorithm());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ resolvedPrivKey.getAlgorithm());
+ assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes());
- PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = privKey.getRawKeyBytes();
+ byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
- assertEquals(PRIVATE.CODE,resolvedPrivKey.getKeyType().CODE);
- assertEquals(32, resolvedPrivKey.getRawKeyBytes().length);
- assertEquals("SM2",resolvedPrivKey.getAlgorithm().name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- resolvedPrivKey.getAlgorithm().code());
- assertArrayEquals(privKeyBytes,resolvedPrivKey.toBytes());
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ signatureFunction.resolvePrivKey(sm3PubKeyBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = privKey.getRawKeyBytes();
- byte[] sm3PubKeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ @Test
+ public void supportPubKeyTest() {
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- signatureFunction.resolvePrivKey(sm3PubKeyBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- @Test
- public void supportPubKeyTest(){
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ PubKey pubKey = keyPair.getPubKey();
+ byte[] pubKeyBytes = pubKey.toBytes();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ assertTrue(signatureFunction.supportPubKey(pubKeyBytes));
- PubKey pubKey = keyPair.getPubKey();
- byte[] pubKeyBytes = pubKey.toBytes();
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE };
+ byte[] rawKeyBytes = pubKey.getRawKeyBytes();
+ byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes);
- assertTrue(signatureFunction.supportPubKey(pubKeyBytes));
+ assertFalse(signatureFunction.supportPubKey(sm3PrivKeyBytes));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE};
- byte[] rawKeyBytes = pubKey.getRawKeyBytes();
- byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes,privKeyTypeBytes,rawKeyBytes);
+ @Test
+ public void resolvePubKeyTest() {
- assertFalse(signatureFunction.supportPubKey(sm3PrivKeyBytes));
- }
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- @Test
- public void resolvePubKeyTest(){
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
+ PubKey pubKey = keyPair.getPubKey();
+ byte[] pubKeyBytes = pubKey.toBytes();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes);
- PubKey pubKey = keyPair.getPubKey();
- byte[] pubKeyBytes = pubKey.toBytes();
+ assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE);
+ assertEquals(65, resolvedPubKey.getRawKeyBytes().length);
+ assertEquals(SMAlgorithm.SM2, resolvedPubKey.getAlgorithm());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ resolvedPubKey.getAlgorithm());
+ assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes());
- PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE };
+ byte[] rawKeyBytes = pubKey.getRawKeyBytes();
+ byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes);
- assertEquals(PUBLIC.CODE,resolvedPubKey.getKeyType().CODE);
- assertEquals(65, resolvedPubKey.getRawKeyBytes().length);
- assertEquals("SM2",resolvedPubKey.getAlgorithm().name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- resolvedPubKey.getAlgorithm().code());
- assertArrayEquals(pubKeyBytes,resolvedPubKey.toBytes());
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ signatureFunction.resolvePrivKey(sm3PrivKeyBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE};
- byte[] rawKeyBytes = pubKey.getRawKeyBytes();
- byte[] sm3PrivKeyBytes = BytesUtils.concat(algoBytes,privKeyTypeBytes,rawKeyBytes);
+ @Test
+ public void supportDigestTest() {
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- signatureFunction.resolvePrivKey(sm3PrivKeyBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- @Test
- public void supportDigestTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction =
- CryptoServiceProviders.getSignatureFunction(algorithm);
+ PrivKey privKey = keyPair.getPrivKey();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);
- PrivKey privKey = keyPair.getPrivKey();
+ byte[] signatureDigestBytes = signatureDigest.toBytes();
+ assertTrue(signatureFunction.supportDigest(signatureDigestBytes));
- SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawDigestBytes = signatureDigest.toBytes();
+ byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
- byte[] signatureDigestBytes = signatureDigest.toBytes();
- assertTrue(signatureFunction.supportDigest(signatureDigestBytes));
+ assertFalse(signatureFunction.supportDigest(sm3SignatureBytes));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawDigestBytes = signatureDigest.toBytes();
- byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
+ @Test
+ public void resolveDigestTest() {
- assertFalse(signatureFunction.supportDigest(sm3SignatureBytes));
- }
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- @Test
- public void resolveDigestTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
- SignatureFunction signatureFunction =
- CryptoServiceProviders.getSignatureFunction(algorithm);
+ PrivKey privKey = keyPair.getPrivKey();
- CryptoKeyPair keyPair = signatureFunction.generateKeyPair();
+ SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);
- PrivKey privKey = keyPair.getPrivKey();
+ byte[] signatureDigestBytes = signatureDigest.toBytes();
- SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
+ SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);
- byte[] signatureDigestBytes = signatureDigest.toBytes();
+ assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
+ assertEquals(SMAlgorithm.SM2, resolvedSignatureDigest.getAlgorithm());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ resolvedSignatureDigest.getAlgorithm());
+ assertArrayEquals(signatureDigestBytes, resolvedSignatureDigest.toBytes());
- SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawDigestBytes = signatureDigest.getRawDigest();
+ byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);
- assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
- assertEquals("SM2",resolvedSignatureDigest.getAlgorithm().name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- resolvedSignatureDigest.getAlgorithm().code());
- assertArrayEquals(signatureDigestBytes,resolvedSignatureDigest.toBytes());
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ signatureFunction.resolveDigest(sm3SignatureDigestBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawDigestBytes = signatureDigest.getRawDigest();
- byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
+ @Test
+ public void supportCiphertextTest() {
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- signatureFunction.resolveDigest(sm3SignatureDigestBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- @Test
- public void supportCiphertextTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ AsymmetricEncryptionFunction asymmetricEncryptionFunction = CryptoServiceProviders
+ .getAsymmetricEncryptionFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
- AsymmetricEncryptionFunction asymmetricEncryptionFunction =
- CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm);
+ PubKey pubKey = keyPair.getPubKey();
- CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
+ Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data);
- PubKey pubKey = keyPair.getPubKey();
+ byte[] ciphertextBytes = ciphertext.toBytes();
- Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data);
+ assertTrue(asymmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
- byte[] ciphertextBytes = ciphertext.toBytes();
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.toBytes();
+ byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
- assertTrue(asymmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
+ assertFalse(asymmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes));
+ }
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.toBytes();
- byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
+ @Test
+ public void resolveCiphertextTest() {
- assertFalse(asymmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes));
- }
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- @Test
- public void resolveCiphertextTest(){
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
+ assertNotNull(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ AsymmetricEncryptionFunction asymmetricEncryptionFunction = CryptoServiceProviders
+ .getAsymmetricEncryptionFunction(algorithm);
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2");
- assertNotNull(algorithm);
+ CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
- AsymmetricEncryptionFunction asymmetricEncryptionFunction =
- CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm);
+ PubKey pubKey = keyPair.getPubKey();
- CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair();
+ Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data);
- PubKey pubKey = keyPair.getPubKey();
+ byte[] ciphertextBytes = ciphertext.toBytes();
- Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data);
+ Ciphertext resolvedCiphertext = asymmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
- byte[] ciphertextBytes = ciphertext.toBytes();
+ assertEquals(65 + 256 / 8 + 1024, resolvedCiphertext.getRawCiphertext().length);
+ assertEquals(SMAlgorithm.SM2, resolvedCiphertext.getAlgorithm());
+ assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
+ resolvedCiphertext.getAlgorithm());
+ assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());
- Ciphertext resolvedCiphertext = asymmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
- assertEquals(65 + 256 / 8 + 1024, resolvedCiphertext.getRawCiphertext().length);
- assertEquals("SM2",resolvedCiphertext.getAlgorithm().name());
- assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
- resolvedCiphertext.getAlgorithm().code());
- assertArrayEquals(ciphertextBytes,resolvedCiphertext.toBytes());
-
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
-
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- asymmetricEncryptionFunction.resolveCiphertext(sm3CiphertextBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ asymmetricEncryptionFunction.resolveCiphertext(sm3CiphertextBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
}
diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java
index b45b45d7..48c707dd 100644
--- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java
+++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java
@@ -5,6 +5,7 @@ import com.jd.blockchain.crypto.CryptoException;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.crypto.HashFunction;
+import com.jd.blockchain.crypto.service.sm.SMAlgorithm;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;
@@ -60,8 +61,7 @@ public class SM3HashFunctionTest {
assertEquals(256 / 8 + 2,digestBytes.length);
assertArrayEquals(digestBytes, BytesUtils.concat(algoBytes, rawDigestBytes));
- assertEquals(algorithm.code(),digest.getAlgorithm().code());
- assertEquals(algorithm.name(),digest.getAlgorithm().name());
+ assertEquals(algorithm.code(),digest.getAlgorithm());
Class> expectedException = CryptoException.class;
Exception actualEx = null;
@@ -132,8 +132,8 @@ public class SM3HashFunctionTest {
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
assertEquals(256 / 8,resolvedDigest.getRawDigest().length);
- assertEquals("SM3",resolvedDigest.getAlgorithm().name());
- assertEquals((short) (HASH_ALGORITHM | ((byte) 3 & 0x00FF)),resolvedDigest.getAlgorithm().code());
+ assertEquals(SMAlgorithm.SM3,resolvedDigest.getAlgorithm());
+ assertEquals((short) (HASH_ALGORITHM | ((byte) 3 & 0x00FF)),resolvedDigest.getAlgorithm());
assertArrayEquals(digestBytes,resolvedDigest.toBytes());
algorithm = CryptoServiceProviders.getAlgorithm("sm4");
diff --git a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java
index 016680e6..1ef4d89e 100644
--- a/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java
+++ b/source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java
@@ -1,16 +1,28 @@
package test.com.jd.blockchain.crypto.service.sm;
-import com.jd.blockchain.crypto.*;
-import com.jd.blockchain.utils.io.BytesUtils;
-import org.junit.Test;
-
-import java.util.Random;
-
import static com.jd.blockchain.crypto.CryptoAlgorithm.ENCRYPTION_ALGORITHM;
import static com.jd.blockchain.crypto.CryptoAlgorithm.SYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+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.CryptoServiceProviders;
+import com.jd.blockchain.crypto.SymmetricEncryptionFunction;
+import com.jd.blockchain.crypto.SymmetricKey;
+import com.jd.blockchain.crypto.service.sm.SMAlgorithm;
+import com.jd.blockchain.utils.io.BytesUtils;
/**
* @author zhanglin33
@@ -19,262 +31,256 @@ import static org.junit.Assert.*;
* @date 2019-04-03, 16:35
*/
public class SM4EncryptionFunctionTest {
- @Test
- public void getAlgorithmTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
-
- algorithm = CryptoServiceProviders.getAlgorithm("sM4");
- assertNotNull(algorithm);
-
- assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
- assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
-
- algorithm = CryptoServiceProviders.getAlgorithm("smm4");
- assertNull(algorithm);
- }
-
- @Test
- public void generateSymmetricKeyTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- assertEquals(SYMMETRIC.CODE,symmetricKey.getKeyType().CODE);
- assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);
-
- assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
- assertEquals(algorithm.code(),symmetricKey.getAlgorithm().code());
-
- assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] keyTypeBytes = new byte[] {SYMMETRIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- assertArrayEquals(BytesUtils.concat(algoBytes,keyTypeBytes,rawKeyBytes),symmetricKey.toBytes());
- }
-
-
-
- @Test
- public void encryptTest(){
-
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
-
- byte[] ciphertextBytes = ciphertext.toBytes();
- assertEquals(2 + 16 +16 + 1024 , ciphertextBytes.length);
- CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
- assertEquals("SM4",ciphertextAlgo.name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),ciphertextAlgo.code());
-
- byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- assertArrayEquals(BytesUtils.concat(algoBytes,rawCiphertextBytes),ciphertextBytes);
- }
-
-
- @Test
- public void decryptTest(){
-
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
-
- byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey,ciphertext);
-
- assertArrayEquals(data,decryptedPlaintext);
- }
-
-// @Test
-// public void streamEncryptTest(){
-//
-// byte[] data = new byte[1024];
-// Random random = new Random();
-// random.nextBytes(data);
-//
-//
-// InputStream inputStream = new ByteArrayInputStream(data);
-// OutputStream outputStream = new ByteArrayOutputStream();
-//
-// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
-// assertNotNull(algorithm);
-//
-// SymmetricEncryptionFunction symmetricEncryptionFunction =
-// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-//
-// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-//
-// symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
-//
-// assertNotNull(outputStream);
-//
-//
-// }
-
-
-
- @Test
- public void supportSymmetricKeyTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- byte[] symmetricKeyBytes = symmetricKey.toBytes();
-
- assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));
-
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ @Test
+ public void getAlgorithmTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
- assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
- }
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- @Test
- public void resolveSymmetricKeyTest(){
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ algorithm = CryptoServiceProviders.getAlgorithm("sM4");
+ assertNotNull(algorithm);
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- byte[] symmetricKeyBytes = symmetricKey.toBytes();
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
+ assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("smm4");
+ assertNull(algorithm);
+ }
+
+ @Test
+ public void generateSymmetricKeyTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);
+ assertEquals(SYMMETRIC.CODE, symmetricKey.getKeyType().CODE);
+ assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);
- assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE);
- assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);
- assertEquals("SM4",resolvedKey.getAlgorithm().name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),resolvedKey.getAlgorithm().code());
- assertArrayEquals(symmetricKeyBytes,resolvedKey.toBytes());
+ assertEquals(algorithm.code(), symmetricKey.getAlgorithm());
+
+ assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] keyTypeBytes = new byte[] { SYMMETRIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ assertArrayEquals(BytesUtils.concat(algoBytes, keyTypeBytes, rawKeyBytes), symmetricKey.toBytes());
+ }
+ @Test
+ public void encryptTest() {
+
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
- byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
- byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
-
- @Test
- public void supportCiphertextTest(){
-
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
-
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
-
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
-
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
-
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
-
- byte[] ciphertextBytes = ciphertext.toBytes();
- assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
-
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.toBytes();
- byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- assertFalse(symmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes));
- }
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
+
+ byte[] ciphertextBytes = ciphertext.toBytes();
+ assertEquals(2 + 16 + 16 + 1024, ciphertextBytes.length);
+ assertEquals(SMAlgorithm.SM4.code(), ciphertext.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)), ciphertext.getAlgorithm());
+
+ byte[] algoBytes = BytesUtils.toBytes(ciphertext.getAlgorithm());
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ assertArrayEquals(BytesUtils.concat(algoBytes, rawCiphertextBytes), ciphertextBytes);
+ }
+
+ @Test
+ public void decryptTest() {
+
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
+
+ byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey, ciphertext);
+
+ assertArrayEquals(data, decryptedPlaintext);
+ }
+
+ // @Test
+ // public void streamEncryptTest(){
+ //
+ // byte[] data = new byte[1024];
+ // Random random = new Random();
+ // random.nextBytes(data);
+ //
+ //
+ // InputStream inputStream = new ByteArrayInputStream(data);
+ // OutputStream outputStream = new ByteArrayOutputStream();
+ //
+ // CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
+ // assertNotNull(algorithm);
+ //
+ // SymmetricEncryptionFunction symmetricEncryptionFunction =
+ // CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ //
+ // SymmetricKey symmetricKey = (SymmetricKey)
+ // symmetricEncryptionFunction.generateSymmetricKey();
+ //
+ // symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
+ //
+ // assertNotNull(outputStream);
+ //
+ //
+ // }
+
+ @Test
+ public void supportSymmetricKeyTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
- @Test
- public void resolveCiphertextTest(){
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- byte[] data = new byte[1024];
- Random random = new Random();
- random.nextBytes(data);
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ byte[] symmetricKeyBytes = symmetricKey.toBytes();
- CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
- assertNotNull(algorithm);
+ assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));
- SymmetricEncryptionFunction symmetricEncryptionFunction =
- CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
+
+ assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
+ }
- SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ @Test
+ public void resolveSymmetricKeyTest() {
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+ byte[] symmetricKeyBytes = symmetricKey.toBytes();
+
+ SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);
+
+ assertEquals(SYMMETRIC.CODE, resolvedKey.getKeyType().CODE);
+ assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);
+ assertEquals(SMAlgorithm.SM4, resolvedKey.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),
+ resolvedKey.getAlgorithm());
+ assertArrayEquals(symmetricKeyBytes, resolvedKey.toBytes());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE };
+ byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
+ byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes);
+
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
+
+ @Test
+ public void supportCiphertextTest() {
+
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
+
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
+
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
+
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
+
+ byte[] ciphertextBytes = ciphertext.toBytes();
+ assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));
+
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.toBytes();
+ byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
+
+ assertFalse(symmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes));
+ }
+
+ @Test
+ public void resolveCiphertextTest() {
+
+ byte[] data = new byte[1024];
+ Random random = new Random();
+ random.nextBytes(data);
+
+ CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
+ assertNotNull(algorithm);
- Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);
+ SymmetricEncryptionFunction symmetricEncryptionFunction = CryptoServiceProviders
+ .getSymmetricEncryptionFunction(algorithm);
- byte[] ciphertextBytes = ciphertext.toBytes();
+ SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
- Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
+ Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey, data);
- assertEquals(1024 + 16 + 16, resolvedCiphertext.getRawCiphertext().length);
- assertEquals("SM4",resolvedCiphertext.getAlgorithm().name());
- assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),resolvedCiphertext.getAlgorithm().code());
- assertArrayEquals(ciphertextBytes,resolvedCiphertext.toBytes());
+ byte[] ciphertextBytes = ciphertext.toBytes();
+
+ Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);
- algorithm = CryptoServiceProviders.getAlgorithm("sm3");
- assertNotNull(algorithm);
- byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
- byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
- byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);
+ assertEquals(1024 + 16 + 16, resolvedCiphertext.getRawCiphertext().length);
+ assertEquals(SMAlgorithm.SM4, resolvedCiphertext.getAlgorithm());
+ assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),
+ resolvedCiphertext.getAlgorithm());
+ assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());
+
+ algorithm = CryptoServiceProviders.getAlgorithm("sm3");
+ assertNotNull(algorithm);
+ byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
+ byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
+ byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes);
- Class> expectedException = CryptoException.class;
- Exception actualEx = null;
- try {
- symmetricEncryptionFunction.resolveCiphertext(sm3CiphertextBytes);
- } catch (Exception e) {
- actualEx = e;
- }
- assertNotNull(actualEx);
- assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
- }
+ Class> expectedException = CryptoException.class;
+ Exception actualEx = null;
+ try {
+ symmetricEncryptionFunction.resolveCiphertext(sm3CiphertextBytes);
+ } catch (Exception e) {
+ actualEx = e;
+ }
+ assertNotNull(actualEx);
+ assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
+ }
}
diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/CryptoKeyEncoding.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/CryptoKeyEncoding.java
index 15cb3649..6b8dbff7 100644
--- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/CryptoKeyEncoding.java
+++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/CryptoKeyEncoding.java
@@ -12,6 +12,7 @@ import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.MagicNumber;
import com.jd.blockchain.utils.io.ByteArray;
import com.jd.blockchain.utils.io.BytesEncoding;
+import com.jd.blockchain.utils.io.BytesUtils;
import com.jd.blockchain.utils.io.NumberMask;
import com.jd.blockchain.utils.io.RuntimeIOException;
@@ -34,9 +35,9 @@ public class CryptoKeyEncoding {
}
out.write(magicNum);
- out.write(key.getAlgorithm().code());
+ BytesUtils.writeShort(key.getAlgorithm(), out);
- int size = 2;// 已经写入 2 字节;
+ int size = 3;// 已经写入 3 字节;
size += BytesEncoding.write(key.getRawKeyBytes(), NumberMask.SHORT, out);
return size;
} catch (IOException e) {
@@ -55,8 +56,7 @@ public class CryptoKeyEncoding {
if (magicNum != MagicNumber.PUB_KEY && magicNum != MagicNumber.PRIV_KEY) {
throw new IllegalArgumentException("The CryptoKey MagicNumber read from the InputStream is Illegal!");
}
- byte code = (byte) in.read();
- CryptoAlgorithm algorithm = CryptoAlgorithm.valueOf(code);
+ short algorithm = CryptoAlgorithm.resolveCode(in);
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in);
if (magicNum == MagicNumber.PUB_KEY) {
@@ -80,8 +80,7 @@ public class CryptoKeyEncoding {
if (magicNum != MagicNumber.PUB_KEY) {
throw new IllegalArgumentException("The PubKey MagicNumber read from the InputStream is Illegal!");
}
- byte code = (byte) in.read();
- CryptoAlgorithm algorithm = CryptoAlgorithm.valueOf(code);
+ short algorithm = CryptoAlgorithm.resolveCode(in);
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in);
return new PubKey(algorithm, value.bytes());
} catch (IOException e) {
@@ -100,8 +99,7 @@ public class CryptoKeyEncoding {
if (magicNum != MagicNumber.PRIV_KEY) {
throw new IllegalArgumentException("The PrivKey MagicNumber read from the InputStream is Illegal!");
}
- byte code = (byte) in.read();
- CryptoAlgorithm algorithm = CryptoAlgorithm.valueOf(code);
+ short algorithm = CryptoAlgorithm.resolveCode(in);
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in);
return new PrivKey(algorithm, value.bytes());
} catch (IOException e) {
diff --git a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java
index 24d405bc..1b202ed7 100644
--- a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java
+++ b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java
@@ -66,14 +66,14 @@ public class NodeSigningAppender implements TransactionService {
// 生成网关签名;
byte[] endpointRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class);
- CryptoAlgorithm signAlgorithm = nodeKeyPair.getAlgorithm();
- SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(nodeKeyPair.getAlgorithm());
+ short signAlgorithm = nodeKeyPair.getAlgorithm();
+ SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(signAlgorithm);
SignatureDigest signDigest = signFunc.sign(nodeKeyPair.getPrivKey(), endpointRequestBytes);
txMessage.addNodeSignatures(new DigitalSignatureBlob(nodeKeyPair.getPubKey(), signDigest));
// 计算交易哈希;
byte[] nodeRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class);
- HashFunction hashFunc = CryptoServiceProviders.getHashFunction(nodeKeyPair.getAlgorithm());
+ HashFunction hashFunc = CryptoServiceProviders.getHashFunction(signAlgorithm);
HashDigest txHash = hashFunc.hash(nodeRequestBytes);
txMessage.setHash(txHash);
diff --git a/source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java b/source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java
index 294055a6..40b19f96 100644
--- a/source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java
+++ b/source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java
@@ -3,7 +3,6 @@ package com.jd.blockchain.sdk.client;
import java.io.Closeable;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
-import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.SignatureDigest;
@@ -139,8 +138,7 @@ public class GatewayServiceFactory implements BlockchainServiceFactory, Closeabl
byte[] txContentBytes = BinaryEncodingUtils.encode(txRequest.getTransactionContent(),
TransactionContent.class);
PrivKey userPrivKey = userKey.getPrivKey();
- CryptoAlgorithm userAlgorithm = userKey.getAlgorithm();
- SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(userAlgorithm);
+ SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(userKey.getAlgorithm());
if (signatureFunction != null) {
SignatureDigest signatureDigest = signatureFunction.sign(userPrivKey, txContentBytes);
DigitalSignature signature = new DigitalSignatureBlob(userKey.getPubKey(), signatureDigest);
diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java
index 5448276d..30c5d286 100644
--- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java
+++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java
@@ -673,7 +673,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI
return false;
}
- return validateAndRecordDecision(targetDecision, resultHandle, privKey.getAlgorithm());
+ return validateAndRecordDecision(targetDecision, resultHandle);
} catch (Exception e) {
prompter.error(e, "Error occurred on synchronizing decision from participant[%s] to participant[%s] ! --%s",
currentId, targetId, e.getMessage());
@@ -693,7 +693,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI
* @return
*/
private synchronized boolean validateAndRecordDecision(LedgerInitDecision targetDecision,
- DecisionResultHandle resultHandle, CryptoAlgorithm hashAlgorithm) {
+ DecisionResultHandle resultHandle) {
if ((!localDecision.getLedgerHash().equals(targetDecision.getLedgerHash()))
&& resultHandle.getResult() == null) {
// 如果结果已经被
@@ -730,9 +730,8 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI
// 当前参与者尚未准备就绪,返回 null;
return null;
}
- PubKey pubKey = ledgerInitSetting.getConsensusParticipants()[remoteId].getPubKey();
DecisionResultHandle resultHandle = this.decisions[remoteId];
- if (!validateAndRecordDecision(initDecision, resultHandle, pubKey.getAlgorithm())) {
+ if (!validateAndRecordDecision(initDecision, resultHandle)) {
// 签名无效;
throw new LedgerInitException(
String.format("Reject decision because of invalid signature! --[Id=%s]", remoteId));
diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/BytesUtils.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/BytesUtils.java
index fc08418e..62d152bd 100644
--- a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/BytesUtils.java
+++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/BytesUtils.java
@@ -28,8 +28,10 @@ public class BytesUtils {
*
* 此方法不处理两者其中之一为 null 的情形,因为无法定义相等性,所以将引发 {@link NullPointerException} 异常;
*
- * @param bytes1 bytes1
- * @param bytes2 bytes2
+ * @param bytes1
+ * bytes1
+ * @param bytes2
+ * bytes2
* @return boolean
*/
public static boolean equals(byte[] bytes1, byte[] bytes2) {
@@ -59,9 +61,11 @@ public class BytesUtils {
}
/**
- * 将输入流的所有内容都读入到字节数组返回;
- * 如果输入流的长度超出 MAX_BUFFER_SIZE 定义的值,则抛出 IllegalArgumentException ;
- * @param in in
+ * 将输入流的所有内容都读入到字节数组返回; 如果输入流的长度超出 MAX_BUFFER_SIZE 定义的值,则抛出
+ * IllegalArgumentException ;
+ *
+ * @param in
+ * in
* @return byte[]
*/
public static byte[] copyToBytes(InputStream in) {
@@ -98,7 +102,8 @@ public class BytesUtils {
* @param maxSize
* 最大字节大小;
* @return 返回实际复制的字节数;
- * @throws IOException exception
+ * @throws IOException
+ * exception
*/
public static int copy(InputStream in, OutputStream out, int maxSize) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
@@ -121,7 +126,8 @@ public class BytesUtils {
/**
* 将 int 值转为4字节的二进制数组;
*
- * @param value value
+ * @param value
+ * value
* @return 转换后的二进制数组,高位在前,低位在后;
*/
public static byte[] toBytes(int value) {
@@ -139,7 +145,8 @@ public class BytesUtils {
/**
* 将 long 值转为8字节的二进制数组;
*
- * @param value value
+ * @param value
+ * value
* @return 转换后的二进制数组,高位在前,低位在后;
*/
public static byte[] toBytes(long value) {
@@ -206,7 +213,6 @@ public class BytesUtils {
return 4;
}
-
/**
* 将 int 值转为4字节的二进制数组;
*
@@ -218,20 +224,20 @@ public class BytesUtils { * 要保存转换结果的二进制数组;转换结果将从高位至低位的顺序写入数组从 offset 指定位置开始的4个元素; * @param offset * 写入转换结果的起始位置; - * @param len 写入长度;必须大于 0 ,小于等于 4; + * @param len + * 写入长度;必须大于 0 ,小于等于 4; * @return 返回写入的长度; */ public static int toBytesInReverse(int value, byte[] bytes, int offset, int len) { int i = 0; int l = len > 4 ? 4 : len; for (; i < l; i++) { - bytes[offset + i] = (byte) ((value >>> (8*i)) & 0x00FF); + bytes[offset + i] = (byte) ((value >>> (8 * i)) & 0x00FF); } - + return i; } - // public static int toBytes(int value, OutputStream out) { // try { // out.write((value >>> 24) & 0x00FF); @@ -486,27 +492,60 @@ public class BytesUtils { return value; } + /** + * 从指定的输入流中读入2个字节,由前到后按由高位到低位的方式转为 short 整数; + * + * @param in + * in + * @return short + */ + public static short readShort(InputStream in) { + try { + int v = in.read(); + if (v < 0) { + throw new IllegalDataException("No enough data to read as short from the specified input stream!"); + } + int value = (v & 0xFF) << 8; + + v = in.read(); + if (v < 0) { + throw new IllegalDataException("No enough data to read as short from the specified input stream!"); + } + value = value | (v & 0xFF); + return (short) value; + } catch (IOException e) { + throw new RuntimeIOException(e.getMessage(), e); + } + } + + public static int writeShort(short value, OutputStream out) { + try { + out.write((value >>> 8) & 0x00FF); + out.write(value & 0x00FF); + return 2; + } catch (IOException e) { + throw new RuntimeIOException(e.getMessage(), e); + } + } + /** * 从指定的输入流中读入4个字节,由前到后按由高位到低位的方式转为 int 整数; - * @param in in + * + * @param in + * in * @return int */ public static int readInt(InputStream in) { -// try { -// byte[] buf = new byte[4]; -// if (in.read(buf) < 4) { -// throw new IllegalDataException("No enough data to read as integer from the specified input stream!"); - // specified input stream!"); -// } -// return toInt(buf); -// } catch (IOException e) { -// throw new RuntimeIOException(e.getMessage(), e); -// } - try { int value = 0; + int v; for (int i = 0; i < 4; i++) { - value = value | ((in.read() & 0xFF) << (8 * (3 - i))); + v = in.read(); + if (v < 0) { + throw new IllegalDataException( + "No enough data to read as integer from the specified input stream!"); + } + value = value | ((v & 0xFF) << (8 * (3 - i))); } return value; } catch (IOException e) { @@ -515,13 +554,6 @@ public class BytesUtils { } public static int writeInt(int value, OutputStream out) { - // byte[] bytes = toBytes(value); - // try { - // out.write(bytes); - // } catch (IOException e) { - // throw new RuntimeIOException(e.getMessage(), e); - // } - try { out.write((value >>> 24) & 0x00FF); out.write((value >>> 16) & 0x00FF); @@ -534,17 +566,6 @@ public class BytesUtils { } public static long readLong(InputStream in) { - // try { - // byte[] buf = new byte[8]; - // if (in.read(buf) < 8) { - // throw new IllegalDataException( - // "No enough data to read as long integer from the specified input stream!"); - // } - // return toLong(buf); - // } catch (IOException e) { - // throw new RuntimeIOException(e.getMessage(), e); - // } - try { long value = 0; int v; @@ -565,13 +586,6 @@ public class BytesUtils { } public static int writeLong(long value, OutputStream out) { - // byte[] bytes = toBytes(value); - // try { - // out.write(bytes); - // } catch (IOException e) { - // throw new RuntimeIOException(e.getMessage(), e); - // } - try { out.write((int) ((value >>> 56) & 0x00FF)); out.write((int) ((value >>> 48) & 0x00FF)); @@ -644,25 +658,29 @@ public class BytesUtils { /** * 从字节数组获取对象 * - * @param objBytes objBytes + * @param objBytes + * objBytes * @return object - * @throws Exception exception + * @throws Exception + * exception */ -// public static Object getObjectFromBytes(byte[] objBytes) throws Exception { -// if (objBytes == null || objBytes.length == 0) { -// return null; -// } -// ByteArrayInputStream bi = new ByteArrayInputStream(objBytes); -// ObjectInputStream oi = new ObjectInputStream(bi); -// return oi.readObject(); -// } + // public static Object getObjectFromBytes(byte[] objBytes) throws Exception { + // if (objBytes == null || objBytes.length == 0) { + // return null; + // } + // ByteArrayInputStream bi = new ByteArrayInputStream(objBytes); + // ObjectInputStream oi = new ObjectInputStream(bi); + // return oi.readObject(); + // } /** * 从对象获取一个字节数组; * - * @param obj obj + * @param obj + * obj * @return byte array - * @throws Exception exception + * @throws Exception + * exception */ public static byte[] getBytesFromObject(Object obj) throws Exception { if (obj == null) {