Browse Source

modify the "resolveXXX" method

tags/1.0.0
zhanglin33 5 years ago
parent
commit
33f1537d9e
7 changed files with 326 additions and 24 deletions
  1. +10
    -4
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java
  2. +15
    -6
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java
  3. +10
    -0
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java
  4. +2
    -2
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
  5. +259
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java
  6. +20
    -8
      source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java
  7. +10
    -4
      source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java

+ 10
- 4
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java View File

@@ -189,8 +189,11 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {


@Override @Override
public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) { public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SymmetricKey(symmetricKeyBytes);
if (supportSymmetricKey(symmetricKeyBytes)) {
return new SymmetricKey(symmetricKeyBytes);
} else {
throw new CryptoException("symmetricKeyBytes is invalid!");
}
} }


@Override @Override
@@ -202,8 +205,11 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {


@Override @Override
public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SymmetricCiphertext(ciphertextBytes);
if (supportCiphertext(ciphertextBytes)) {
return new SymmetricCiphertext(ciphertextBytes);
} else {
throw new CryptoException("ciphertextBytes is invalid!");
}
} }


@Override @Override


+ 15
- 6
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java View File

@@ -101,8 +101,11 @@ public class ED25519SignatureFunction implements SignatureFunction {


@Override @Override
public PrivKey resolvePrivKey(byte[] privKeyBytes) { public PrivKey resolvePrivKey(byte[] privKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new PrivKey(privKeyBytes);
if (supportPrivKey(privKeyBytes)) {
return new PrivKey(privKeyBytes);
} else {
throw new CryptoException("privKeyBytes is invalid!");
}
} }


@Override @Override
@@ -115,8 +118,11 @@ public class ED25519SignatureFunction implements SignatureFunction {


@Override @Override
public PubKey resolvePubKey(byte[] pubKeyBytes) { public PubKey resolvePubKey(byte[] pubKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new PubKey(pubKeyBytes);
if (supportPubKey(pubKeyBytes)) {
return new PubKey(pubKeyBytes);
} else {
throw new CryptoException("pubKeyBytes is invalid!");
}
} }


@Override @Override
@@ -127,8 +133,11 @@ public class ED25519SignatureFunction implements SignatureFunction {


@Override @Override
public SignatureDigest resolveDigest(byte[] digestBytes) { public SignatureDigest resolveDigest(byte[] digestBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SignatureDigest(digestBytes);
if (supportDigest(digestBytes)) {
return new SignatureDigest(digestBytes);
} else {
throw new CryptoException("digestBytes is invalid!");
}
} }


@Override @Override


+ 10
- 0
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/ED25519Utils.java View File

@@ -0,0 +1,10 @@
package com.jd.blockchain.crypto.utils.classic;

/**
* @author zhanglin33
* @title: ED25519Utils
* @description: ED25519 signature algorithm
* @date 2019-04-04, 20:01
*/
public class ED25519Utils {
}

+ 2
- 2
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java View File

@@ -89,8 +89,8 @@ public class AESEncryptionFunctionTest {
byte[] ciphertextBytes = ciphertext.toBytes(); byte[] ciphertextBytes = ciphertext.toBytes();
assertEquals(2 + 16 + 1024 , ciphertextBytes.length); assertEquals(2 + 16 + 1024 , ciphertextBytes.length);
CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm(); CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString());
assertEquals(algorithm.name(),ciphertext.getAlgorithm().name());
assertEquals(algorithm.toString(),ciphertext.getAlgorithm().toString());


byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();


+ 259
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java View File

@@ -1,5 +1,21 @@
package test.com.jd.blockchain.crypto.service.classic; package test.com.jd.blockchain.crypto.service.classic;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;

import java.util.Random;

import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
import static org.junit.Assert.*;

/** /**
* @author zhanglin33 * @author zhanglin33
* @title: ED25519SignatureFunctionTest * @title: ED25519SignatureFunctionTest
@@ -7,4 +23,247 @@ package test.com.jd.blockchain.crypto.service.classic;
* @date 2019-04-01, 14:01 * @date 2019-04-01, 14:01
*/ */
public class ED25519SignatureFunctionTest { public class ED25519SignatureFunctionTest {
@Test
public void getAlgorithmTest(){

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);


assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("Ed25519");
assertNotNull(algorithm);

assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name());
assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("eddsa");
assertNull(algorithm);
}

@Test
public void generateKeyPairTest(){

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);


CryptoKeyPair keyPair = signatureFunction.generateKeyPair();

PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();

assertEquals(PUBLIC.CODE,pubKey.getKeyType().CODE);
assertEquals(32, pubKey.getRawKeyBytes().length);
assertEquals(PRIVATE.CODE,privKey.getKeyType().CODE);
assertEquals(32, privKey.getRawKeyBytes().length);

assertEquals(algorithm.name(),pubKey.getAlgorithm().name());
assertEquals(algorithm.toString(),pubKey.getAlgorithm().toString());
assertEquals(algorithm.name(),privKey.getAlgorithm().name());
assertEquals(algorithm.toString(),privKey.getAlgorithm().toString());

assertEquals(2 + 1 + 32, 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());



}



@Test
public void signTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoKeyPair keyPair = signatureFunction.generateKeyPair();

PrivKey privKey = keyPair.getPrivKey();
SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);

byte[] signatureBytes = signatureDigest.toBytes();

assertEquals(2 + 64, signatureBytes.length);
CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm();
assertEquals(algorithm.name(),signatureDigest.getAlgorithm().name());
assertEquals(algorithm.toString(),signatureDigest.getAlgorithm().toString());

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);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoKeyPair keyPair = signatureFunction.generateKeyPair();

PubKey pubKey = keyPair.getPubKey();
PrivKey privKey = keyPair.getPrivKey();
SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);

assertTrue(signatureFunction.verify(signatureDigest,pubKey,data));
}


@Test
public void supportPrivKeyTest(){

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);


CryptoKeyPair keyPair = signatureFunction.generateKeyPair();

PrivKey privKey = keyPair.getPrivKey();
byte[] privKeyBytes = privKey.toBytes();

assertTrue(signatureFunction.supportPrivKey(privKeyBytes));

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
byte[] rawKeyBytes = privKey.getRawKeyBytes();
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);

assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes));
}

// @Test
// public void resolveSymmetricKeyTest(){
// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
// 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(algorithm.name(),resolvedKey.getAlgorithm().name());
// assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString());
//
//
// 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);
//
// 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);
//
// Class<?> expectedException = CryptoException.class;
// Exception actualEx = null;
// try {
// symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes);
// } catch (Exception e) {
// actualEx = e;
// }
// assertNotNull(actualEx);
// assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
// }
} }

+ 20
- 8
source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java View File

@@ -95,8 +95,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur


@Override @Override
public PrivKey resolvePrivKey(byte[] privKeyBytes) { public PrivKey resolvePrivKey(byte[] privKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new PrivKey(privKeyBytes);
if (supportPrivKey(privKeyBytes)) {
return new PrivKey(privKeyBytes);
} else {
throw new CryptoException("privKeyBytes is invalid!");
}
} }


@Override @Override
@@ -108,8 +111,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur


@Override @Override
public PubKey resolvePubKey(byte[] pubKeyBytes) { public PubKey resolvePubKey(byte[] pubKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new PubKey(pubKeyBytes);
if (supportPubKey(pubKeyBytes)) {
return new PubKey(pubKeyBytes);
} else {
throw new CryptoException("pubKeyBytes is invalid!");
}
} }


@Override @Override
@@ -121,8 +127,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur


@Override @Override
public AsymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { public AsymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new AsymmetricCiphertext(ciphertextBytes);
if (supportCiphertext(ciphertextBytes)) {
return new AsymmetricCiphertext(ciphertextBytes);
} else {
throw new CryptoException("ciphertextBytes is invalid!");
}
} }


@Override @Override
@@ -177,8 +186,11 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur


@Override @Override
public SignatureDigest resolveDigest(byte[] digestBytes) { public SignatureDigest resolveDigest(byte[] digestBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SignatureDigest(digestBytes);
if (supportDigest(digestBytes)) {
return new SignatureDigest(digestBytes);
} else {
throw new CryptoException("digestBytes is invalid!");
}
} }


@Override @Override


+ 10
- 4
source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java View File

@@ -177,8 +177,11 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {


@Override @Override
public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) { public SymmetricKey resolveSymmetricKey(byte[] symmetricKeyBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SymmetricKey(symmetricKeyBytes);
if (supportSymmetricKey(symmetricKeyBytes)) {
return new SymmetricKey(symmetricKeyBytes);
} else {
throw new CryptoException("symmetricKeyBytes is invalid!");
}
} }


@Override @Override
@@ -190,8 +193,11 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {


@Override @Override
public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) { public SymmetricCiphertext resolveCiphertext(byte[] ciphertextBytes) {
// 由框架调用 support 方法检查有效性,在此不做重复检查;
return new SymmetricCiphertext(ciphertextBytes);
if (supportCiphertext(ciphertextBytes)) {
return new SymmetricCiphertext(ciphertextBytes);
} else {
throw new CryptoException("ciphertextBytes is invalid!");
}
} }


@Override @Override


Loading…
Cancel
Save