@@ -21,8 +21,8 @@ public class ECDSASignatureFunction implements SignatureFunction { | |||||
} | } | ||||
@Override | @Override | ||||
public byte[] retrievePubKeyBytes(byte[] privKeyBytes) { | |||||
return new byte[0]; | |||||
public PubKey retrievePubKey(PrivKey privKey) { | |||||
return null; | |||||
} | } | ||||
@Override | @Override | ||||
@@ -83,14 +83,23 @@ public class ED25519SignatureFunction implements SignatureFunction { | |||||
} | } | ||||
@Override | @Override | ||||
public byte[] retrievePubKeyBytes(byte[] privKeyBytes) { | |||||
byte[] rawPrivKeyBytes = resolvePrivKey(privKeyBytes).getRawKeyBytes(); | |||||
public PubKey retrievePubKey(PrivKey privKey) { | |||||
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); | |||||
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512); | EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512); | ||||
EdDSAPrivateKeySpec privateKeySpec = new EdDSAPrivateKeySpec(rawPrivKeyBytes, spec); | EdDSAPrivateKeySpec privateKeySpec = new EdDSAPrivateKeySpec(rawPrivKeyBytes, spec); | ||||
byte[] rawPubKeyBytes = privateKeySpec.getA().toByteArray(); | byte[] rawPubKeyBytes = privateKeySpec.getA().toByteArray(); | ||||
return new PubKey(ED25519, rawPubKeyBytes).toBytes(); | |||||
return new PubKey(ED25519, rawPubKeyBytes); | |||||
} | } | ||||
// | |||||
// @Override | |||||
// public byte[] retrievePubKey(byte[] privKeyBytes) { | |||||
// | |||||
// byte[] rawPrivKeyBytes = resolvePrivKey(privKeyBytes).getRawKeyBytes(); | |||||
// EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512); | |||||
// EdDSAPrivateKeySpec privateKeySpec = new EdDSAPrivateKeySpec(rawPrivKeyBytes, spec); | |||||
// byte[] rawPubKeyBytes = privateKeySpec.getA().toByteArray(); | |||||
// return new PubKey(ED25519, rawPubKeyBytes).toBytes(); | |||||
// } | |||||
@Override | @Override | ||||
public boolean supportPrivKey(byte[] privKeyBytes) { | public boolean supportPrivKey(byte[] privKeyBytes) { | ||||
@@ -24,18 +24,18 @@ public class RSACryptoFunction implements AsymmetricEncryptionFunction, Signatur | |||||
} | } | ||||
@Override | @Override | ||||
public SignatureDigest sign(PrivKey privKey, byte[] data) { | |||||
public PubKey retrievePubKey(PrivKey privKey) { | |||||
return null; | return null; | ||||
} | } | ||||
@Override | @Override | ||||
public boolean verify(SignatureDigest digest, PubKey pubKey, byte[] data) { | |||||
return false; | |||||
public SignatureDigest sign(PrivKey privKey, byte[] data) { | |||||
return null; | |||||
} | } | ||||
@Override | @Override | ||||
public byte[] retrievePubKeyBytes(byte[] privKeyBytes) { | |||||
return new byte[0]; | |||||
public boolean verify(SignatureDigest digest, PubKey pubKey, byte[] data) { | |||||
return false; | |||||
} | } | ||||
@Override | @Override | ||||
@@ -698,7 +698,7 @@ | |||||
// byte[] expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | // byte[] expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | ||||
// byte[] expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | // byte[] expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | ||||
// | // | ||||
// byte[] pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes); | |||||
// byte[] pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); | |||||
// | // | ||||
// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | // assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | ||||
// | // | ||||
@@ -711,7 +711,7 @@ | |||||
// expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | // expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | ||||
// expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | // expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | ||||
// | // | ||||
// pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes); | |||||
// pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); | |||||
// | // | ||||
// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | // assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | ||||
// | // | ||||
@@ -724,7 +724,7 @@ | |||||
// expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | // expectedPrivKeyBytes = keyPair.getPrivKey().toBytes(); | ||||
// expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | // expectedPubKeyBytes = keyPair.getPubKey().toBytes(); | ||||
// | // | ||||
// pubKeyBytes = asymmetricCrypto.retrievePubKeyBytes(expectedPrivKeyBytes); | |||||
// pubKeyBytes = asymmetricCrypto.retrievePubKey(expectedPrivKeyBytes); | |||||
// | // | ||||
// assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | // assertArrayEquals(expectedPubKeyBytes,pubKeyBytes); | ||||
// | // | ||||
@@ -11,6 +11,8 @@ import java.io.InputStream; | |||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.util.Random; | 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.PUBLIC; | ||||
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC; | import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC; | ||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
@@ -58,7 +60,7 @@ public class AESEncryptionFunctionTest { | |||||
assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length); | assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length); | ||||
assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name()); | assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name()); | ||||
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString()); | |||||
assertEquals(algorithm.code(),symmetricKey.getAlgorithm().code()); | |||||
assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | ||||
@@ -89,8 +91,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(),ciphertext.getAlgorithm().name()); | |||||
assertEquals(algorithm.toString(),ciphertext.getAlgorithm().toString()); | |||||
assertEquals("AES",ciphertextAlgo.name()); | |||||
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),ciphertextAlgo.code()); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); | byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); | ||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
@@ -150,6 +152,7 @@ public class AESEncryptionFunctionTest { | |||||
@Test | @Test | ||||
public void supportSymmetricKeyTest(){ | public void supportSymmetricKeyTest(){ | ||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); | CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -173,6 +176,7 @@ public class AESEncryptionFunctionTest { | |||||
@Test | @Test | ||||
public void resolveSymmetricKeyTest(){ | public void resolveSymmetricKeyTest(){ | ||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); | CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -186,10 +190,9 @@ public class AESEncryptionFunctionTest { | |||||
assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE); | assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE); | ||||
assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length); | assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length); | ||||
assertEquals(algorithm.name(),resolvedKey.getAlgorithm().name()); | |||||
assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString()); | |||||
assertEquals("AES",resolvedKey.getAlgorithm().name()); | |||||
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 26 & 0x00FF)),resolvedKey.getAlgorithm().code()); | |||||
assertArrayEquals(symmetricKeyBytes,resolvedKey.toBytes()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); | algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -257,18 +260,29 @@ public class AESEncryptionFunctionTest { | |||||
Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); | Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); | ||||
byte[] ciphertextBytes = ciphertext.toBytes(); | byte[] ciphertextBytes = ciphertext.toBytes(); | ||||
assertTrue(symmetricEncryptionFunction.supportCiphertext(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()); | |||||
assertArrayEquals(ciphertext.toBytes(),resolvedCiphertext.toBytes()); | |||||
assertArrayEquals(ciphertext.getRawCiphertext(),resolvedCiphertext.getRawCiphertext()); | |||||
assertEquals(ciphertext.getAlgorithm().name(),resolvedCiphertext.getAlgorithm().name()); | |||||
assertEquals(ciphertext.getAlgorithm().code(),resolvedCiphertext.getAlgorithm().code()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); | algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | ||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | ||||
Class<?> expectedException = CryptoException.class; | Class<?> expectedException = CryptoException.class; | ||||
Exception actualEx = null; | Exception actualEx = null; | ||||
try { | try { | ||||
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes); | |||||
symmetricEncryptionFunction.resolveCiphertext(ripemd160CiphertextBytes); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
actualEx = e; | actualEx = e; | ||||
} | } | ||||
@@ -1,9 +1,6 @@ | |||||
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.*; | |||||
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | ||||
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | ||||
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | ||||
@@ -12,6 +9,8 @@ import org.junit.Test; | |||||
import java.util.Random; | 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.PRIVATE; | ||||
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC; | import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC; | ||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
@@ -23,6 +22,7 @@ import static org.junit.Assert.*; | |||||
* @date 2019-04-01, 14:01 | * @date 2019-04-01, 14:01 | ||||
*/ | */ | ||||
public class ED25519SignatureFunctionTest { | public class ED25519SignatureFunctionTest { | ||||
@Test | @Test | ||||
public void getAlgorithmTest(){ | public void getAlgorithmTest(){ | ||||
@@ -53,7 +53,6 @@ public class ED25519SignatureFunctionTest { | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | ||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | ||||
PubKey pubKey = keyPair.getPubKey(); | PubKey pubKey = keyPair.getPubKey(); | ||||
@@ -65,9 +64,9 @@ public class ED25519SignatureFunctionTest { | |||||
assertEquals(32, privKey.getRawKeyBytes().length); | assertEquals(32, privKey.getRawKeyBytes().length); | ||||
assertEquals(algorithm.name(),pubKey.getAlgorithm().name()); | assertEquals(algorithm.name(),pubKey.getAlgorithm().name()); | ||||
assertEquals(algorithm.toString(),pubKey.getAlgorithm().toString()); | |||||
assertEquals(algorithm.code(),pubKey.getAlgorithm().code()); | |||||
assertEquals(algorithm.name(),privKey.getAlgorithm().name()); | assertEquals(algorithm.name(),privKey.getAlgorithm().name()); | ||||
assertEquals(algorithm.toString(),privKey.getAlgorithm().toString()); | |||||
assertEquals(algorithm.code(),privKey.getAlgorithm().code()); | |||||
assertEquals(2 + 1 + 32, pubKey.toBytes().length); | assertEquals(2 + 1 + 32, pubKey.toBytes().length); | ||||
assertEquals(2 + 1 + 32, privKey.toBytes().length); | assertEquals(2 + 1 + 32, privKey.toBytes().length); | ||||
@@ -79,12 +78,29 @@ public class ED25519SignatureFunctionTest { | |||||
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); | byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); | ||||
assertArrayEquals(BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawPubKeyBytes),pubKey.toBytes()); | assertArrayEquals(BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawPubKeyBytes),pubKey.toBytes()); | ||||
assertArrayEquals(BytesUtils.concat(algoBytes,privKeyTypeBytes,rawPrivKeyBytes),privKey.toBytes()); | assertArrayEquals(BytesUtils.concat(algoBytes,privKeyTypeBytes,rawPrivKeyBytes),privKey.toBytes()); | ||||
} | |||||
@Test | |||||
public void retrievePubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); | |||||
assertNotNull(algorithm); | |||||
} | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
PubKey retrievedPubKey = signatureFunction.retrievePubKey(privKey); | |||||
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 | @Test | ||||
public void signTest(){ | public void signTest(){ | ||||
@@ -107,8 +123,9 @@ public class ED25519SignatureFunctionTest { | |||||
assertEquals(2 + 64, signatureBytes.length); | assertEquals(2 + 64, signatureBytes.length); | ||||
CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm(); | CryptoAlgorithm signatureAlgo = signatureDigest.getAlgorithm(); | ||||
assertEquals(algorithm.name(),signatureDigest.getAlgorithm().name()); | |||||
assertEquals(algorithm.toString(),signatureDigest.getAlgorithm().toString()); | |||||
assertEquals("ED25519",signatureAlgo.name()); | |||||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)), | |||||
signatureAlgo.code()); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(signatureAlgo); | byte[] algoBytes = CryptoAlgorithm.toBytes(signatureAlgo); | ||||
byte[] rawSinatureBytes = signatureDigest.getRawDigest(); | byte[] rawSinatureBytes = signatureDigest.getRawDigest(); | ||||
@@ -144,7 +161,6 @@ public class ED25519SignatureFunctionTest { | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | ||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | ||||
PrivKey privKey = keyPair.getPrivKey(); | PrivKey privKey = keyPair.getPrivKey(); | ||||
@@ -162,108 +178,186 @@ public class ED25519SignatureFunctionTest { | |||||
assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes)); | 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())); | |||||
// } | |||||
@Test | |||||
public void resolvePrivKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
byte[] privKeyBytes = privKey.toBytes(); | |||||
PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes); | |||||
assertEquals(PRIVATE.CODE,resolvedPrivKey.getKeyType().CODE); | |||||
assertEquals(32, resolvedPrivKey.getRawKeyBytes().length); | |||||
assertEquals("ED25519",resolvedPrivKey.getAlgorithm().name()); | |||||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)), | |||||
resolvedPrivKey.getAlgorithm().code()); | |||||
assertArrayEquals(privKeyBytes,resolvedPrivKey.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); | |||||
Class<?> expectedException = CryptoException.class; | |||||
Exception actualEx = null; | |||||
try { | |||||
signatureFunction.resolvePrivKey(ripemd160PubKeyBytes); | |||||
} catch (Exception e) { | |||||
actualEx = e; | |||||
} | |||||
assertNotNull(actualEx); | |||||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||||
} | |||||
@Test | |||||
public void supportPubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
byte[] pubKeyBytes = pubKey.toBytes(); | |||||
assertTrue(signatureFunction.supportPubKey(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); | |||||
assertFalse(signatureFunction.supportPubKey(ripemd160PrivKeyBytes)); | |||||
} | |||||
@Test | |||||
public void resolvePubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("ed25519"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
byte[] pubKeyBytes = pubKey.toBytes(); | |||||
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes); | |||||
assertEquals(PUBLIC.CODE,resolvedPubKey.getKeyType().CODE); | |||||
assertEquals(32, resolvedPubKey.getRawKeyBytes().length); | |||||
assertEquals("ED25519",resolvedPubKey.getAlgorithm().name()); | |||||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)), | |||||
resolvedPubKey.getAlgorithm().code()); | |||||
assertArrayEquals(pubKeyBytes,resolvedPubKey.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); | |||||
Class<?> expectedException = CryptoException.class; | |||||
Exception actualEx = null; | |||||
try { | |||||
signatureFunction.resolvePrivKey(ripemd160PrivKeyBytes); | |||||
} catch (Exception e) { | |||||
actualEx = e; | |||||
} | |||||
assertNotNull(actualEx); | |||||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||||
} | |||||
@Test | |||||
public void supportDigestTest(){ | |||||
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[] signatureDigestBytes = signatureDigest.toBytes(); | |||||
assertTrue(signatureFunction.supportDigest(signatureDigestBytes)); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("ripemd160"); | |||||
assertNotNull(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | |||||
byte[] ripemd160SignatureBytes = BytesUtils.concat(algoBytes,rawDigestBytes); | |||||
assertFalse(signatureFunction.supportDigest(ripemd160SignatureBytes)); | |||||
} | |||||
@Test | |||||
public void resolveDigestTest(){ | |||||
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[] signatureDigestBytes = signatureDigest.toBytes(); | |||||
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes); | |||||
assertEquals(64, resolvedSignatureDigest.getRawDigest().length); | |||||
assertEquals("ED25519",resolvedSignatureDigest.getAlgorithm().name()); | |||||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 21 & 0x00FF)), | |||||
resolvedSignatureDigest.getAlgorithm().code()); | |||||
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())); | |||||
} | |||||
} | } |
@@ -10,6 +10,7 @@ import org.junit.Test; | |||||
import java.util.Random; | import java.util.Random; | ||||
import static com.jd.blockchain.crypto.CryptoAlgorithm.HASH_ALGORITHM; | |||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
@@ -132,11 +133,10 @@ public class RIPEMD160HashFunctionTest { | |||||
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | ||||
assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); | |||||
assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); | |||||
assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); | |||||
assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); | |||||
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"); | algorithm = CryptoServiceProviders.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -10,6 +10,7 @@ import org.junit.Test; | |||||
import java.util.Random; | import java.util.Random; | ||||
import static com.jd.blockchain.crypto.CryptoAlgorithm.HASH_ALGORITHM; | |||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
/** | /** | ||||
@@ -132,11 +133,10 @@ public class SHA256HashFunctionTest { | |||||
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | ||||
assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); | |||||
assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); | |||||
assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); | |||||
assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); | |||||
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"); | algorithm = CryptoServiceProviders.getAlgorithm("aes"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -69,7 +69,7 @@ | |||||
// * @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组 | // * @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组 | ||||
// * @return 包含算法标识、密钥掩码和公钥的字节数组 | // * @return 包含算法标识、密钥掩码和公钥的字节数组 | ||||
// */ | // */ | ||||
// byte[] retrievePubKeyBytes(byte[] privKeyBytes); | |||||
// byte[] retrievePubKey(byte[] privKeyBytes); | |||||
// | // | ||||
// byte[] tryRetrievePubKeyBytes(byte[] privKeyBytes); | // byte[] tryRetrievePubKeyBytes(byte[] privKeyBytes); | ||||
// | // | ||||
@@ -26,12 +26,12 @@ public interface AsymmetricEncryptionFunction extends CryptoKeyPairGenerator, Cr | |||||
byte[] decrypt(PrivKey privKey, Ciphertext ciphertext); | byte[] decrypt(PrivKey privKey, Ciphertext ciphertext); | ||||
/** | /** | ||||
* 使用字节数组形式的私钥生成字节数组形式的公钥; | |||||
* 使用私钥恢复公钥; | |||||
* | * | ||||
* @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组 | |||||
* @return 包含算法标识、密钥掩码和公钥的字节数组 | |||||
* @param privKey PrivKey形式的私钥信息 | |||||
* @return PubKey形式的公钥信息 | |||||
*/ | */ | ||||
byte[] retrievePubKeyBytes(byte[] privKeyBytes); | |||||
PubKey retrievePubKey(PrivKey privKey); | |||||
/** | /** | ||||
* 校验私钥格式是否满足要求; | * 校验私钥格式是否满足要求; | ||||
@@ -25,12 +25,12 @@ public interface SignatureFunction extends CryptoKeyPairGenerator, CryptoFunctio | |||||
boolean verify(SignatureDigest digest, PubKey pubKey, byte[] data); | boolean verify(SignatureDigest digest, PubKey pubKey, byte[] data); | ||||
/** | /** | ||||
* 使用字节数组形式的私钥生成字节数组形式的公钥; | |||||
* 使用私钥恢复公钥; | |||||
* | * | ||||
* @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组 | |||||
* @return 包含算法标识、密钥掩码和公钥的字节数组 | |||||
* @param privKey PrivKey形式的私钥信息 | |||||
* @return PubKey形式的公钥信息 | |||||
*/ | */ | ||||
byte[] retrievePubKeyBytes(byte[] privKeyBytes); | |||||
PubKey retrievePubKey(PrivKey privKey); | |||||
/** | /** | ||||
* 校验私钥格式是否满足要求; | * 校验私钥格式是否满足要求; | ||||
@@ -79,13 +79,20 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur | |||||
} | } | ||||
@Override | @Override | ||||
public byte[] retrievePubKeyBytes(byte[] privKeyBytes) { | |||||
byte[] rawPrivKeyBytes = resolvePrivKey(privKeyBytes).getRawKeyBytes(); | |||||
public PubKey retrievePubKey(PrivKey privKey) { | |||||
byte[] rawPrivKeyBytes = privKey.getRawKeyBytes(); | |||||
byte[] rawPubKeyBytes = SM2Utils.retrievePublicKey(rawPrivKeyBytes); | byte[] rawPubKeyBytes = SM2Utils.retrievePublicKey(rawPrivKeyBytes); | ||||
return new PubKey(SM2, rawPubKeyBytes).toBytes(); | |||||
return new PubKey(SM2, rawPubKeyBytes); | |||||
} | } | ||||
// @Override | |||||
// public byte[] retrievePubKey(byte[] privKeyBytes) { | |||||
// | |||||
// byte[] rawPrivKeyBytes = resolvePrivKey(privKeyBytes).getRawKeyBytes(); | |||||
// byte[] rawPubKeyBytes = SM2Utils.retrievePublicKey(rawPrivKeyBytes); | |||||
// return new PubKey(SM2, rawPubKeyBytes).toBytes(); | |||||
// } | |||||
@Override | @Override | ||||
public boolean supportPrivKey(byte[] privKeyBytes) { | public boolean supportPrivKey(byte[] privKeyBytes) { | ||||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应SM2算法,并且密钥类型是私钥 | // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应SM2算法,并且密钥类型是私钥 | ||||
@@ -1,5 +1,20 @@ | |||||
package test.com.jd.blockchain.crypto.service.sm; | package test.com.jd.blockchain.crypto.service.sm; | ||||
import com.jd.blockchain.crypto.*; | |||||
import com.jd.blockchain.crypto.asymmetric.AsymmetricEncryptionFunction; | |||||
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.CryptoAlgorithm.*; | |||||
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: SM2CyptoFunctionTest | * @title: SM2CyptoFunctionTest | ||||
@@ -7,4 +22,480 @@ package test.com.jd.blockchain.crypto.service.sm; | |||||
* @date 2019-04-03, 16:32 | * @date 2019-04-03, 16:32 | ||||
*/ | */ | ||||
public class SM2CyptoFunctionTest { | public class SM2CyptoFunctionTest { | ||||
@Test | |||||
public void getAlgorithmTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name()); | |||||
assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sM2"); | |||||
assertNotNull(algorithm); | |||||
assertEquals(signatureFunction.getAlgorithm().name(), algorithm.name()); | |||||
assertEquals(signatureFunction.getAlgorithm().code(), algorithm.code()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm22"); | |||||
assertNull(algorithm); | |||||
} | |||||
@Test | |||||
public void generateKeyPairTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
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(65, pubKey.getRawKeyBytes().length); | |||||
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(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()); | |||||
} | |||||
@Test | |||||
public void retrievePubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
PubKey retrievedPubKey = signatureFunction.retrievePubKey(privKey); | |||||
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(){ | |||||
byte[] data = new byte[1024]; | |||||
Random random = new Random(); | |||||
random.nextBytes(data); | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
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.code(),signatureDigest.getAlgorithm().code()); | |||||
assertEquals("SM2",signatureAlgo.name()); | |||||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 2 & 0x00FF)), | |||||
signatureAlgo.code()); | |||||
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("sm2"); | |||||
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 encryptTest(){ | |||||
byte[] data = new byte[1024]; | |||||
Random random = new Random(); | |||||
random.nextBytes(data); | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = | |||||
CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm); | |||||
CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data); | |||||
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()); | |||||
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("sm2"); | |||||
assertNotNull(algorithm); | |||||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = | |||||
CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm); | |||||
CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data); | |||||
byte[] decryptedPlaintext = asymmetricEncryptionFunction.decrypt(privKey,ciphertext); | |||||
assertArrayEquals(data,decryptedPlaintext); | |||||
} | |||||
@Test | |||||
public void supportPrivKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
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("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); | |||||
assertFalse(signatureFunction.supportPrivKey(sm3PubKeyBytes)); | |||||
} | |||||
@Test | |||||
public void resolvePrivKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
byte[] privKeyBytes = privKey.toBytes(); | |||||
PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes); | |||||
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()); | |||||
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); | |||||
Class<?> expectedException = CryptoException.class; | |||||
Exception actualEx = null; | |||||
try { | |||||
signatureFunction.resolvePrivKey(sm3PubKeyBytes); | |||||
} catch (Exception e) { | |||||
actualEx = e; | |||||
} | |||||
assertNotNull(actualEx); | |||||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||||
} | |||||
@Test | |||||
public void supportPubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
byte[] pubKeyBytes = pubKey.toBytes(); | |||||
assertTrue(signatureFunction.supportPubKey(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); | |||||
assertFalse(signatureFunction.supportPubKey(sm3PrivKeyBytes)); | |||||
} | |||||
@Test | |||||
public void resolvePubKeyTest(){ | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
byte[] pubKeyBytes = pubKey.toBytes(); | |||||
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes); | |||||
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()); | |||||
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); | |||||
Class<?> expectedException = CryptoException.class; | |||||
Exception actualEx = null; | |||||
try { | |||||
signatureFunction.resolvePrivKey(sm3PrivKeyBytes); | |||||
} catch (Exception e) { | |||||
actualEx = e; | |||||
} | |||||
assertNotNull(actualEx); | |||||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||||
} | |||||
@Test | |||||
public void supportDigestTest(){ | |||||
byte[] data = new byte[1024]; | |||||
Random random = new Random(); | |||||
random.nextBytes(data); | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = | |||||
CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
SignatureDigest signatureDigest = signatureFunction.sign(privKey,data); | |||||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||||
assertTrue(signatureFunction.supportDigest(signatureDigestBytes)); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | |||||
assertNotNull(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.toBytes(); | |||||
byte[] sm3SignatureBytes = BytesUtils.concat(algoBytes,rawDigestBytes); | |||||
assertFalse(signatureFunction.supportDigest(sm3SignatureBytes)); | |||||
} | |||||
@Test | |||||
public void resolveDigestTest(){ | |||||
byte[] data = new byte[1024]; | |||||
Random random = new Random(); | |||||
random.nextBytes(data); | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
SignatureFunction signatureFunction = | |||||
CryptoServiceProviders.getSignatureFunction(algorithm); | |||||
CryptoKeyPair keyPair = signatureFunction.generateKeyPair(); | |||||
PrivKey privKey = keyPair.getPrivKey(); | |||||
SignatureDigest signatureDigest = signatureFunction.sign(privKey,data); | |||||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||||
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes); | |||||
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()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | |||||
assertNotNull(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] rawDigestBytes = signatureDigest.getRawDigest(); | |||||
byte[] sm3SignatureDigestBytes = BytesUtils.concat(algoBytes,rawDigestBytes); | |||||
Class<?> expectedException = CryptoException.class; | |||||
Exception actualEx = null; | |||||
try { | |||||
signatureFunction.resolveDigest(sm3SignatureDigestBytes); | |||||
} 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("sm2"); | |||||
assertNotNull(algorithm); | |||||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = | |||||
CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm); | |||||
CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data); | |||||
byte[] ciphertextBytes = ciphertext.toBytes(); | |||||
assertTrue(asymmetricEncryptionFunction.supportCiphertext(ciphertextBytes)); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | |||||
assertNotNull(algorithm); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | |||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | |||||
assertFalse(asymmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes)); | |||||
} | |||||
@Test | |||||
public void resolveCiphertextTest(){ | |||||
byte[] data = new byte[1024]; | |||||
Random random = new Random(); | |||||
random.nextBytes(data); | |||||
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm2"); | |||||
assertNotNull(algorithm); | |||||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = | |||||
CryptoServiceProviders.getAsymmetricEncryptionFunction(algorithm); | |||||
CryptoKeyPair keyPair = asymmetricEncryptionFunction.generateKeyPair(); | |||||
PubKey pubKey = keyPair.getPubKey(); | |||||
Ciphertext ciphertext =asymmetricEncryptionFunction.encrypt(pubKey,data); | |||||
byte[] ciphertextBytes = ciphertext.toBytes(); | |||||
Ciphertext resolvedCiphertext = asymmetricEncryptionFunction.resolveCiphertext(ciphertextBytes); | |||||
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())); | |||||
} | |||||
} | } |
@@ -10,6 +10,7 @@ import org.junit.Test; | |||||
import java.util.Random; | import java.util.Random; | ||||
import static com.jd.blockchain.crypto.CryptoAlgorithm.HASH_ALGORITHM; | |||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
/** | /** | ||||
@@ -130,11 +131,10 @@ public class SM3HashFunctionTest { | |||||
HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | HashDigest resolvedDigest = hashFunction.resolveHashDigest(digestBytes); | ||||
assertArrayEquals(digest.toBytes(),resolvedDigest.toBytes()); | |||||
assertArrayEquals(digest.getRawDigest(),resolvedDigest.getRawDigest()); | |||||
assertEquals(digest.getAlgorithm().name(),resolvedDigest.getAlgorithm().name()); | |||||
assertEquals(digest.getAlgorithm().code(),resolvedDigest.getAlgorithm().code()); | |||||
assertEquals(256 / 8,resolvedDigest.getRawDigest().length); | |||||
assertEquals("SM3",resolvedDigest.getAlgorithm().name()); | |||||
assertEquals((short) (HASH_ALGORITHM | ((byte) 3 & 0x00FF)),resolvedDigest.getAlgorithm().code()); | |||||
assertArrayEquals(digestBytes,resolvedDigest.toBytes()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm4"); | algorithm = CryptoServiceProviders.getAlgorithm("sm4"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
@@ -151,21 +151,5 @@ public class SM3HashFunctionTest { | |||||
} | } | ||||
assertNotNull(actualEx); | assertNotNull(actualEx); | ||||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | 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())); | |||||
} | } | ||||
} | } |
@@ -7,6 +7,8 @@ import org.junit.Test; | |||||
import java.util.Random; | 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.PUBLIC; | ||||
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC; | import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC; | ||||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
@@ -53,7 +55,7 @@ public class SM4EncryptionFunctionTest { | |||||
assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length); | assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length); | ||||
assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name()); | assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name()); | ||||
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString()); | |||||
assertEquals(algorithm.code(),symmetricKey.getAlgorithm().code()); | |||||
assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | ||||
@@ -84,8 +86,8 @@ public class SM4EncryptionFunctionTest { | |||||
byte[] ciphertextBytes = ciphertext.toBytes(); | byte[] ciphertextBytes = ciphertext.toBytes(); | ||||
assertEquals(2 + 16 +16 + 1024 , ciphertextBytes.length); | assertEquals(2 + 16 +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("SM4",ciphertextAlgo.name()); | |||||
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),ciphertextAlgo.code()); | |||||
byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); | byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo); | ||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | ||||
@@ -181,9 +183,9 @@ public class SM4EncryptionFunctionTest { | |||||
assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE); | assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE); | ||||
assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length); | assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length); | ||||
assertEquals(algorithm.name(),resolvedKey.getAlgorithm().name()); | |||||
assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString()); | |||||
assertEquals("SM4",resolvedKey.getAlgorithm().name()); | |||||
assertEquals((short) (ENCRYPTION_ALGORITHM | SYMMETRIC_KEY | ((byte) 4 & 0x00FF)),resolvedKey.getAlgorithm().code()); | |||||
assertArrayEquals(symmetricKeyBytes,resolvedKey.toBytes()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | ||||
@@ -229,9 +231,9 @@ public class SM4EncryptionFunctionTest { | |||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | ||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | byte[] rawCiphertextBytes = ciphertext.toBytes(); | ||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | |||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | |||||
assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes)); | |||||
assertFalse(symmetricEncryptionFunction.supportCiphertext(sm3CiphertextBytes)); | |||||
} | } | ||||
@Test | @Test | ||||
@@ -252,18 +254,24 @@ public class SM4EncryptionFunctionTest { | |||||
Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); | Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data); | ||||
byte[] ciphertextBytes = ciphertext.toBytes(); | byte[] ciphertextBytes = ciphertext.toBytes(); | ||||
assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes)); | |||||
Ciphertext resolvedCiphertext = symmetricEncryptionFunction.resolveCiphertext(ciphertextBytes); | |||||
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()); | |||||
algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | algorithm = CryptoServiceProviders.getAlgorithm("sm3"); | ||||
assertNotNull(algorithm); | assertNotNull(algorithm); | ||||
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm); | ||||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | |||||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||||
byte[] sm3CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes); | |||||
Class<?> expectedException = CryptoException.class; | Class<?> expectedException = CryptoException.class; | ||||
Exception actualEx = null; | Exception actualEx = null; | ||||
try { | try { | ||||
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes); | |||||
symmetricEncryptionFunction.resolveCiphertext(sm3CiphertextBytes); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
actualEx = e; | actualEx = e; | ||||
} | } | ||||