Browse Source

Fixed errors of the test cases in crypto framework;

tags/1.0.0
huanghaiquan 5 years ago
parent
commit
3a88932051
7 changed files with 346 additions and 352 deletions
  1. +2
    -2
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
  2. +235
    -240
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunctionTest.java
  3. +1
    -1
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/RIPEMD160HashFunctionTest.java
  4. +1
    -1
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/SHA256HashFunctionTest.java
  5. +6
    -6
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java
  6. +98
    -98
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java
  7. +3
    -4
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java

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

@@ -191,7 +191,7 @@ public class AESEncryptionFunctionTest {

assertEquals(SYMMETRIC.CODE, resolvedKey.getKeyType().CODE);
assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);
assertEquals(ClassicAlgorithm.AES, resolvedKey.getAlgorithm());
assertEquals(ClassicAlgorithm.AES.code(), resolvedKey.getAlgorithm());
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)), resolvedKey.getAlgorithm());
assertArrayEquals(symmetricKeyBytes, resolvedKey.toBytes());

@@ -264,7 +264,7 @@ public class AESEncryptionFunctionTest {
Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);

assertEquals(1024 + 16, resolvedCiphertext.getRawCiphertext().length);
assertEquals(ClassicAlgorithm.AES, resolvedCiphertext.getAlgorithm());
assertEquals(ClassicAlgorithm.AES.code(), resolvedCiphertext.getAlgorithm());
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),
resolvedCiphertext.getAlgorithm());
assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());


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

@@ -34,337 +34,332 @@ import com.jd.blockchain.utils.io.BytesUtils;
*/
public class ED25519SignatureFunctionTest {

@Test
public void getAlgorithmTest(){
@Test
public void getAlgorithmTest() {

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
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("Ed25519");
assertNotNull(algorithm);

algorithm = CryptoServiceProviders.getAlgorithm("Ed25519");
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("eddsa");
assertNull(algorithm);
}

algorithm = CryptoServiceProviders.getAlgorithm("eddsa");
assertNull(algorithm);
}
@Test
public void generateKeyPairTest() {

@Test
public void generateKeyPairTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

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

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

AsymmetricKeypair 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(32, pubKey.getRawKeyBytes().length);
assertEquals(PRIVATE.CODE, privKey.getKeyType().CODE);
assertEquals(32, privKey.getRawKeyBytes().length);

assertEquals(PUBLIC.CODE,pubKey.getKeyType().CODE);
assertEquals(32, 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.code(),pubKey.getAlgorithm());
assertEquals(algorithm.code(),privKey.getAlgorithm());
assertEquals(2 + 1 + 32, pubKey.toBytes().length);
assertEquals(2 + 1 + 32, privKey.toBytes().length);

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

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("ed25519");
assertNotNull(algorithm);

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

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

AsymmetricKeypair 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(),retrievedPubKey.getAlgorithm());
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("ed25519");
assertNotNull(algorithm);

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

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

AsymmetricKeypair 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(ClassicAlgorithm.ED25519.code(), signatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
signatureDigest.getAlgorithm());

assertEquals(2 + 64, signatureBytes.length);
assertEquals(ClassicAlgorithm.ED25519.code(),signatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
signatureDigest.getAlgorithm());
byte[] algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm());
byte[] rawSinatureBytes = signatureDigest.getRawDigest();
assertArrayEquals(BytesUtils.concat(algoBytes, rawSinatureBytes), signatureBytes);
}

byte[] algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm());
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("ed25519");
assertNotNull(algorithm);

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

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

AsymmetricKeypair 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 supportPrivKeyTest() {

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

@Test
public void supportPrivKeyTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
PrivKey privKey = keyPair.getPrivKey();
byte[] privKeyBytes = privKey.toBytes();

AsymmetricKeypair keyPair = signatureFunction.generateKeypair();
assertTrue(signatureFunction.supportPrivKey(privKeyBytes));

PrivKey privKey = keyPair.getPrivKey();
byte[] privKeyBytes = privKey.toBytes();
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);

assertTrue(signatureFunction.supportPrivKey(privKeyBytes));
assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes));
}

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);
@Test
public void resolvePrivKeyTest() {

assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes));
}
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

@Test
public void resolvePrivKeyTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
PrivKey privKey = keyPair.getPrivKey();
byte[] privKeyBytes = privKey.toBytes();

AsymmetricKeypair 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(ClassicAlgorithm.ED25519.code(), resolvedPrivKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedPrivKey.getAlgorithm());
assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes());

PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(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);

assertEquals(PRIVATE.CODE,resolvedPrivKey.getKeyType().CODE);
assertEquals(32, resolvedPrivKey.getRawKeyBytes().length);
assertEquals(ClassicAlgorithm.ED25519.code(),resolvedPrivKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedPrivKey.getAlgorithm());
assertArrayEquals(privKeyBytes,resolvedPrivKey.toBytes());
Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolvePrivKey(ripemd160PubKeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}

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);
@Test
public void supportPubKeyTest() {

Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolvePrivKey(ripemd160PubKeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

@Test
public void supportPubKeyTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
PubKey pubKey = keyPair.getPubKey();
byte[] pubKeyBytes = pubKey.toBytes();

AsymmetricKeypair keyPair = signatureFunction.generateKeypair();
assertTrue(signatureFunction.supportPubKey(pubKeyBytes));

PubKey pubKey = keyPair.getPubKey();
byte[] pubKeyBytes = pubKey.toBytes();
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE };
byte[] rawKeyBytes = pubKey.getRawKeyBytes();
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes);

assertTrue(signatureFunction.supportPubKey(pubKeyBytes));
assertFalse(signatureFunction.supportPubKey(ripemd160PrivKeyBytes));
}

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE};
byte[] rawKeyBytes = pubKey.getRawKeyBytes();
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes,privKeyTypeBytes,rawKeyBytes);
@Test
public void resolvePubKeyTest() {

assertFalse(signatureFunction.supportPubKey(ripemd160PrivKeyBytes));
}
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

@Test
public void resolvePubKeyTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);
PubKey pubKey = keyPair.getPubKey();
byte[] pubKeyBytes = pubKey.toBytes();

AsymmetricKeypair keyPair = signatureFunction.generateKeypair();
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes);

PubKey pubKey = keyPair.getPubKey();
byte[] pubKeyBytes = pubKey.toBytes();
assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE);
assertEquals(32, resolvedPubKey.getRawKeyBytes().length);
assertEquals(ClassicAlgorithm.ED25519.code(), resolvedPubKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedPubKey.getAlgorithm());
assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes());

PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes);
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE };
byte[] rawKeyBytes = pubKey.getRawKeyBytes();
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes, privKeyTypeBytes, rawKeyBytes);

assertEquals(PUBLIC.CODE,resolvedPubKey.getKeyType().CODE);
assertEquals(32, resolvedPubKey.getRawKeyBytes().length);
assertEquals(ClassicAlgorithm.ED25519.code(),resolvedPubKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedPubKey.getAlgorithm());
assertArrayEquals(pubKeyBytes,resolvedPubKey.toBytes());
Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolvePrivKey(ripemd160PrivKeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] privKeyTypeBytes = new byte[] {PRIVATE.CODE};
byte[] rawKeyBytes = pubKey.getRawKeyBytes();
byte[] ripemd160PrivKeyBytes = BytesUtils.concat(algoBytes,privKeyTypeBytes,rawKeyBytes);
@Test
public void supportDigestTest() {

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

Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolvePrivKey(ripemd160PrivKeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

@Test
public void supportDigestTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
PrivKey privKey = keyPair.getPrivKey();

SignatureFunction signatureFunction =
CryptoServiceProviders.getSignatureFunction(algorithm);
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);

AsymmetricKeypair keyPair = signatureFunction.generateKeypair();
byte[] signatureDigestBytes = signatureDigest.toBytes();
assertTrue(signatureFunction.supportDigest(signatureDigestBytes));

PrivKey privKey = keyPair.getPrivKey();
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawDigestBytes = signatureDigest.toBytes();
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes, rawDigestBytes);

SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
assertFalse(signatureFunction.supportDigest(ripemd160SignatureBytes));
}

byte[] signatureDigestBytes = signatureDigest.toBytes();
assertTrue(signatureFunction.supportDigest(signatureDigestBytes));
@Test
public void resolveDigestTest() {

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawDigestBytes = signatureDigest.toBytes();
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

assertFalse(signatureFunction.supportDigest(ripemd160SignatureBytes));
}
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);

@Test
public void resolveDigestTest(){
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm);

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);
AsymmetricKeypair keyPair = signatureFunction.generateKeypair();

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519");
assertNotNull(algorithm);
PrivKey privKey = keyPair.getPrivKey();

SignatureFunction signatureFunction =
CryptoServiceProviders.getSignatureFunction(algorithm);
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data);

AsymmetricKeypair keyPair = signatureFunction.generateKeypair();
byte[] signatureDigestBytes = signatureDigest.toBytes();

PrivKey privKey = keyPair.getPrivKey();
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);

SignatureDigest signatureDigest = signatureFunction.sign(privKey,data);
assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
assertEquals(ClassicAlgorithm.ED25519.code(), resolvedSignatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedSignatureDigest.getAlgorithm());
assertArrayEquals(signatureDigestBytes, resolvedSignatureDigest.toBytes());

byte[] signatureDigestBytes = signatureDigest.toBytes();
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawDigestBytes = signatureDigest.getRawDigest();
byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes, rawDigestBytes);

SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);

assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
assertEquals(ClassicAlgorithm.ED25519,resolvedSignatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)),
resolvedSignatureDigest.getAlgorithm());
assertArrayEquals(signatureDigestBytes,resolvedSignatureDigest.toBytes());

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawDigestBytes = signatureDigest.getRawDigest();
byte[] ripemd160SignatureDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);

Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolveDigest(ripemd160SignatureDigestBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
signatureFunction.resolveDigest(ripemd160SignatureDigestBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

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

@@ -134,7 +134,7 @@ public class RIPEMD160HashFunctionTest {
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);

assertEquals(160 / 8, resolvedDigest.getRawDigest().length);
assertEquals(ClassicAlgorithm.RIPEMD160, resolvedDigest.getAlgorithm());
assertEquals(ClassicAlgorithm.RIPEMD160.code(), resolvedDigest.getAlgorithm());
assertEquals((short) (HASH_ALGORITHM | ((byte) 25 & 0x00FF)), resolvedDigest.getAlgorithm());
assertArrayEquals(digestBytes, resolvedDigest.toBytes());



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

@@ -134,7 +134,7 @@ public class SHA256HashFunctionTest {
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);

assertEquals(256 / 8, resolvedDigest.getRawDigest().length);
assertEquals(ClassicAlgorithm.SHA256, resolvedDigest.getAlgorithm());
assertEquals(ClassicAlgorithm.SHA256.code(), resolvedDigest.getAlgorithm());
assertEquals((short) (HASH_ALGORITHM | ((byte) 24 & 0x00FF)), resolvedDigest.getAlgorithm());
assertArrayEquals(digestBytes, resolvedDigest.toBytes());



+ 6
- 6
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM2CyptoFunctionTest.java View File

@@ -117,7 +117,7 @@ public class SM2CyptoFunctionTest {
assertEquals(2 + 64, signatureBytes.length);
assertEquals(algorithm.code(), signatureDigest.getAlgorithm());

assertEquals(SMAlgorithm.SM2, signatureDigest.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), signatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
signatureDigest.getAlgorithm());

@@ -167,7 +167,7 @@ public class SM2CyptoFunctionTest {

byte[] ciphertextBytes = ciphertext.toBytes();
assertEquals(2 + 65 + 256 / 8 + 1024, ciphertextBytes.length);
assertEquals(SMAlgorithm.SM2, ciphertext.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), ciphertext.getAlgorithm());

assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
ciphertext.getAlgorithm());
@@ -244,7 +244,7 @@ public class SM2CyptoFunctionTest {

assertEquals(PRIVATE.CODE, resolvedPrivKey.getKeyType().CODE);
assertEquals(32, resolvedPrivKey.getRawKeyBytes().length);
assertEquals(SMAlgorithm.SM2, resolvedPrivKey.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), resolvedPrivKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
resolvedPrivKey.getAlgorithm());
assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes());
@@ -309,7 +309,7 @@ public class SM2CyptoFunctionTest {

assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE);
assertEquals(65, resolvedPubKey.getRawKeyBytes().length);
assertEquals(SMAlgorithm.SM2, resolvedPubKey.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), resolvedPubKey.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
resolvedPubKey.getAlgorithm());
assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes());
@@ -385,7 +385,7 @@ public class SM2CyptoFunctionTest {
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes);

assertEquals(64, resolvedSignatureDigest.getRawDigest().length);
assertEquals(SMAlgorithm.SM2, resolvedSignatureDigest.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), resolvedSignatureDigest.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
resolvedSignatureDigest.getAlgorithm());
assertArrayEquals(signatureDigestBytes, resolvedSignatureDigest.toBytes());
@@ -463,7 +463,7 @@ public class SM2CyptoFunctionTest {
Ciphertext resolvedCiphertext = asymmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);

assertEquals(65 + 256 / 8 + 1024, resolvedCiphertext.getRawCiphertext().length);
assertEquals(SMAlgorithm.SM2, resolvedCiphertext.getAlgorithm());
assertEquals(SMAlgorithm.SM2.code(), resolvedCiphertext.getAlgorithm());
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)),
resolvedCiphertext.getAlgorithm());
assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());


+ 98
- 98
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM3HashFunctionTest.java View File

@@ -21,135 +21,135 @@ import static org.junit.Assert.*;
* @date 2019-04-03, 16:33
*/
public class SM3HashFunctionTest {
@Test
public void getAlgorithmTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3");
assertNotNull(algorithm);
@Test
public void getAlgorithmTest() {
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3");
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("Sm3");
assertNotNull(algorithm);
algorithm = CryptoServiceProviders.getAlgorithm("Sm3");
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("sm3333");
assertNull(algorithm);
}
algorithm = CryptoServiceProviders.getAlgorithm("sm3333");
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("sm3");
assertNotNull(algorithm);
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm3");
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());
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()));
}
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);
@Test
public void verifyTest() {
byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

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

HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);

HashDigest digest = hashFunction.hash(data);
HashDigest digest = hashFunction.hash(data);

assertTrue(hashFunction.verify(digest,data));
}
assertTrue(hashFunction.verify(digest, data));
}

@Test
public void supportHashDigestTest(){
byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);
@Test
public void supportHashDigestTest() {
byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

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

HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);

HashDigest digest = hashFunction.hash(data);
HashDigest digest = hashFunction.hash(data);

byte[] digestBytes = digest.toBytes();
assertTrue(hashFunction.supportHashDigest(digestBytes));
byte[] digestBytes = digest.toBytes();
assertTrue(hashFunction.supportHashDigest(digestBytes));

algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
System.arraycopy(algoBytes,0,digestBytes,0,algoBytes.length);
assertFalse(hashFunction.supportHashDigest(digestBytes));
}
algorithm = CryptoServiceProviders.getAlgorithm("sm4");
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);
@Test
public void resolveHashDigestTest() {
byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

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

HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);
HashFunction hashFunction = CryptoServiceProviders.getHashFunction(algorithm);

HashDigest digest = hashFunction.hash(data);
HashDigest digest = hashFunction.hash(data);

byte[] digestBytes = digest.toBytes();
byte[] digestBytes = digest.toBytes();

HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes);

assertEquals(256 / 8,resolvedDigest.getRawDigest().length);
assertEquals(SMAlgorithm.SM3,resolvedDigest.getAlgorithm());
assertEquals((short) (HASH_ALGORITHM | ((byte) 3 & 0x00FF)),resolvedDigest.getAlgorithm());
assertArrayEquals(digestBytes,resolvedDigest.toBytes());
assertEquals(256 / 8, resolvedDigest.getRawDigest().length);
assertEquals(SMAlgorithm.SM3.code(), resolvedDigest.getAlgorithm());
assertEquals((short) (HASH_ALGORITHM | ((byte) 3 & 0x00FF)), resolvedDigest.getAlgorithm());
assertArrayEquals(digestBytes, resolvedDigest.toBytes());

algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawDigestBytes = digest.getRawDigest();
byte[] aesDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes);
algorithm = CryptoServiceProviders.getAlgorithm("sm4");
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()));
}
Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
hashFunction.resolveHashDigest(aesDigestBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
}

+ 3
- 4
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java View File

@@ -188,9 +188,8 @@ public class SM4EncryptionFunctionTest {

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());
assertEquals(SMAlgorithm.SM4.code(), resolvedKey.getAlgorithm());
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)), resolvedKey.getAlgorithm());
assertArrayEquals(symmetricKeyBytes, resolvedKey.toBytes());

algorithm = CryptoServiceProviders.getAlgorithm("sm3");
@@ -262,7 +261,7 @@ public class SM4EncryptionFunctionTest {
Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes);

assertEquals(1024 + 16 + 16, resolvedCiphertext.getRawCiphertext().length);
assertEquals(SMAlgorithm.SM4, resolvedCiphertext.getAlgorithm());
assertEquals(SMAlgorithm.SM4.code(), resolvedCiphertext.getAlgorithm());
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),
resolvedCiphertext.getAlgorithm());
assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes());


Loading…
Cancel
Save