@@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; | |||
*/ | |||
public class PaillierUtilsTest { | |||
@Test | |||
public void generateKeyPairTest() { | |||
public void test() { | |||
AsymmetricCipherKeyPair keyPair = PaillierUtils.generateKeyPair(); | |||
PaillierPublicKeyParameters pubKeyParams = (PaillierPublicKeyParameters) keyPair.getPublic(); | |||
@@ -58,13 +58,6 @@ public class PaillierUtilsTest { | |||
assertEquals(pInverseConverted, pInverse); | |||
assertEquals(muPConverted, muP); | |||
assertEquals(muQConverted, muQ); | |||
} | |||
@Test | |||
public void encryptTest() { | |||
AsymmetricCipherKeyPair keyPair = PaillierUtils.generateKeyPair(); | |||
PaillierPublicKeyParameters pubKeyParams = (PaillierPublicKeyParameters) keyPair.getPublic(); | |||
byte[] pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
@@ -77,23 +70,15 @@ public class PaillierUtilsTest { | |||
assertEquals(512,ciphertextFromParams.length); | |||
assertEquals(512,ciphertextFromBytes.length); | |||
} | |||
@Test | |||
public void decryptTest(){ | |||
AsymmetricCipherKeyPair keyPair = PaillierUtils.generateKeyPair(); | |||
PaillierPublicKeyParameters pubKeyParams = (PaillierPublicKeyParameters) keyPair.getPublic(); | |||
PaillierPrivateKeyParameters privKeyParams = (PaillierPrivateKeyParameters) keyPair.getPrivate(); | |||
byte[] pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
byte[] privKeyBytes = PaillierUtils.privKey2Bytes(privKeyParams); | |||
int input = 666; | |||
byte[] data = intToByteArray(input); | |||
byte[] inputBytes = intToByteArray(input); | |||
byte[] ciphertextFromParams = PaillierUtils.encrypt(data,pubKeyParams); | |||
byte[] ciphertextFromBytes = PaillierUtils.encrypt(data,pubKeyBytes); | |||
ciphertextFromParams = PaillierUtils.encrypt(inputBytes,pubKeyParams); | |||
ciphertextFromBytes = PaillierUtils.encrypt(inputBytes,pubKeyBytes); | |||
byte[] plaintextFromParams = PaillierUtils.decrypt(ciphertextFromBytes,privKeyParams); | |||
byte[] plaintextFromBytes = PaillierUtils.decrypt(ciphertextFromParams,privKeyBytes); | |||
@@ -103,16 +88,9 @@ public class PaillierUtilsTest { | |||
assertEquals(input,outputFromParams); | |||
assertEquals(input,outputFromBytes); | |||
} | |||
@Test | |||
public void addTest() { | |||
AsymmetricCipherKeyPair keyPair = PaillierUtils.generateKeyPair(); | |||
PaillierPublicKeyParameters pubKeyParams = (PaillierPublicKeyParameters) keyPair.getPublic(); | |||
PaillierPrivateKeyParameters privKeyParams = (PaillierPrivateKeyParameters) keyPair.getPrivate(); | |||
byte[] pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
int input1 = 600; | |||
int input2 = 60; | |||
@@ -139,26 +117,19 @@ public class PaillierUtilsTest { | |||
output = byteArrayToInt(plaintext); | |||
assertEquals(sum,output); | |||
} | |||
@Test | |||
public void scalarMultiplyTest() { | |||
AsymmetricCipherKeyPair keyPair = PaillierUtils.generateKeyPair(); | |||
PaillierPublicKeyParameters pubKeyParams = (PaillierPublicKeyParameters) keyPair.getPublic(); | |||
PaillierPrivateKeyParameters privKeyParams = (PaillierPrivateKeyParameters) keyPair.getPrivate(); | |||
byte[] pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
pubKeyBytes = PaillierUtils.pubKey2Bytes(pubKeyParams); | |||
int input = 111; | |||
input = 111; | |||
int scalar = 6; | |||
byte[] data = intToByteArray(input); | |||
data = intToByteArray(input); | |||
byte[] ciphertext = PaillierUtils.encrypt(data,pubKeyParams); | |||
byte[] ciphertextPowered = PaillierUtils.scalarMultiply(pubKeyBytes,ciphertext,scalar); | |||
byte[] plaintextMultiplied = PaillierUtils.decrypt(ciphertextPowered,privKeyParams); | |||
int output = byteArrayToInt(plaintextMultiplied); | |||
output = byteArrayToInt(plaintextMultiplied); | |||
assertEquals(input * scalar, output); | |||
} | |||
@@ -0,0 +1,49 @@ | |||
package com.jd.blockchain.crypto.utils.classic; | |||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter; | |||
import org.bouncycastle.crypto.util.OpenSSHPublicKeyUtil; | |||
import org.bouncycastle.jce.spec.OpenSSHPublicKeySpec; | |||
import org.bouncycastle.util.encoders.Base64; | |||
/** | |||
* @author zhanglin33 | |||
* @title: SSHKeyParser | |||
* @description: a parser for parsing asymmetric keys in Base64 format | |||
* @date 2019-05-17, 17:52 | |||
*/ | |||
public class SSHKeyParser { | |||
private String pubKeyFormat; | |||
private String pubKeyType; | |||
public AsymmetricKeyParameter pubKeyParse(String pubKeyStr) { | |||
byte[] pubKeyBytes; | |||
if (pubKeyStr.startsWith("ssh") || pubKeyStr.startsWith("ecdsa")) { | |||
String[] algoAndKeyAndLocal = pubKeyStr.split(" "); | |||
pubKeyBytes = Base64.decode(algoAndKeyAndLocal[1]); | |||
} else { | |||
pubKeyBytes = Base64.decode(pubKeyStr); | |||
} | |||
OpenSSHPublicKeySpec pubKeySpec = new OpenSSHPublicKeySpec(pubKeyBytes); | |||
pubKeyFormat = pubKeySpec.getFormat(); | |||
pubKeyType = pubKeySpec.getType(); | |||
return OpenSSHPublicKeyUtil.parsePublicKey(pubKeyBytes); | |||
} | |||
public String getPubKeyFormat() { | |||
return pubKeyFormat; | |||
} | |||
public String getPubKeyType() { | |||
return pubKeyType; | |||
} | |||
} |
@@ -42,7 +42,7 @@ public class RSACryptoFunctionTest { | |||
} | |||
@Test | |||
public void generateKeyPairTest() { | |||
public void test() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
@@ -72,20 +72,7 @@ public class RSACryptoFunctionTest { | |||
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 = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
PubKey retrievedPubKey = signatureFunction.retrievePubKey(privKey); | |||
@@ -93,23 +80,12 @@ public class RSACryptoFunctionTest { | |||
assertEquals(pubKey.getRawKeyBytes().length, retrievedPubKey.getRawKeyBytes().length); | |||
assertEquals(pubKey.getAlgorithm(), retrievedPubKey.getAlgorithm()); | |||
assertArrayEquals(pubKey.toBytes(), retrievedPubKey.toBytes()); | |||
} | |||
@Test | |||
public void signTest() { | |||
byte[] data = new byte[1024]; | |||
byte[] data = new byte[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureBytes = signatureDigest.toBytes(); | |||
@@ -121,48 +97,16 @@ public class RSACryptoFunctionTest { | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 23 & 0x00FF)), | |||
signatureDigest.getAlgorithm()); | |||
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); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair 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[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = Crypto | |||
.getAsymmetricEncryptionFunction(algorithm); | |||
AsymmetricKeypair keyPair = asymmetricEncryptionFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data); | |||
byte[] ciphertextBytes = ciphertext.toBytes(); | |||
@@ -172,90 +116,32 @@ public class RSACryptoFunctionTest { | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 23 & 0x00FF)), | |||
ciphertext.getAlgorithm()); | |||
byte[] algoBytes = BytesUtils.toBytes(ciphertext.getAlgorithm()); | |||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
assertArrayEquals(BytesUtils.concat(algoBytes, rawCiphertextBytes), ciphertextBytes); | |||
} | |||
@Test | |||
public void decryptTest() { | |||
byte[] data = new byte[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = Crypto | |||
.getAsymmetricEncryptionFunction(algorithm); | |||
AsymmetricKeypair 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 = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
byte[] privKeyBytes = privKey.toBytes(); | |||
assertTrue(signatureFunction.supportPrivKey(privKeyBytes)); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
byte[] rawKeyBytes = privKey.getRawKeyBytes(); | |||
byte[] ripemd160PubKeyBytes = BytesUtils.concat(algoBytes, pubKeyTypeBytes, rawKeyBytes); | |||
assertFalse(signatureFunction.supportPrivKey(ripemd160PubKeyBytes)); | |||
} | |||
@Test | |||
public void resolvePrivKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
byte[] privKeyBytes = privKey.toBytes(); | |||
PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes); | |||
assertEquals(PRIVATE.CODE, resolvedPrivKey.getKeyType().CODE); | |||
assertEquals(1155, resolvedPrivKey.getRawKeyBytes().length); | |||
assertEquals(ClassicAlgorithm.RSA.code(), resolvedPrivKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 23 & 0x00FF)), | |||
resolvedPrivKey.getAlgorithm()); | |||
assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes()); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(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 { | |||
@@ -265,64 +151,26 @@ public class RSACryptoFunctionTest { | |||
} | |||
assertNotNull(actualEx); | |||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||
} | |||
@Test | |||
public void supportPubKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
byte[] pubKeyBytes = pubKey.toBytes(); | |||
assertTrue(signatureFunction.supportPubKey(pubKeyBytes)); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(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 = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
byte[] pubKeyBytes = pubKey.toBytes(); | |||
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes); | |||
assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE); | |||
assertEquals(259, resolvedPubKey.getRawKeyBytes().length); | |||
assertEquals(ClassicAlgorithm.RSA.code(), resolvedPubKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 23 & 0x00FF)), | |||
resolvedPubKey.getAlgorithm()); | |||
assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes()); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(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) { | |||
@@ -330,57 +178,18 @@ public class RSACryptoFunctionTest { | |||
} | |||
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 = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||
assertTrue(signatureFunction.supportDigest(signatureDigestBytes)); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(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 = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes); | |||
@@ -392,12 +201,8 @@ public class RSACryptoFunctionTest { | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(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) { | |||
@@ -405,77 +210,24 @@ public class RSACryptoFunctionTest { | |||
} | |||
assertNotNull(actualEx); | |||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||
} | |||
@Test | |||
public void supportCiphertextTest() { | |||
byte[] data = new byte[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = Crypto | |||
.getAsymmetricEncryptionFunction(algorithm); | |||
AsymmetricKeypair keyPair = asymmetricEncryptionFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data); | |||
byte[] ciphertextBytes = ciphertext.toBytes(); | |||
assertTrue(asymmetricEncryptionFunction.supportCiphertext(ciphertextBytes)); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
byte[] rawCiphertextBytes = ciphertext.toBytes(); | |||
algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
assertFalse(asymmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes)); | |||
} | |||
@Test | |||
public void resolveCiphertextTest() { | |||
byte[] data = new byte[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("RSA"); | |||
assertNotNull(algorithm); | |||
AsymmetricEncryptionFunction asymmetricEncryptionFunction = Crypto | |||
.getAsymmetricEncryptionFunction(algorithm); | |||
AsymmetricKeypair keyPair = asymmetricEncryptionFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
Ciphertext ciphertext = asymmetricEncryptionFunction.encrypt(pubKey, data); | |||
byte[] ciphertextBytes = ciphertext.toBytes(); | |||
Ciphertext resolvedCiphertext = asymmetricEncryptionFunction.resolveCiphertext(ciphertextBytes); | |||
assertEquals(256, resolvedCiphertext.getRawCiphertext().length); | |||
assertEquals(ClassicAlgorithm.RSA.code(), resolvedCiphertext.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ENCRYPTION_ALGORITHM | ASYMMETRIC_KEY | ((byte) 23 & 0x00FF)), | |||
resolvedCiphertext.getAlgorithm()); | |||
assertArrayEquals(ciphertextBytes, resolvedCiphertext.toBytes()); | |||
algorithm = Crypto.getAlgorithm("ripemd160"); | |||
assertNotNull(algorithm); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes, rawCiphertextBytes); | |||
Class<?> expectedException = CryptoException.class; | |||
Exception actualEx = null; | |||
try { | |||
asymmetricEncryptionFunction.resolveCiphertext(ripemd160CiphertextBytes); | |||
} catch (Exception e) { | |||
@@ -484,5 +236,4 @@ public class RSACryptoFunctionTest { | |||
assertNotNull(actualEx); | |||
assertTrue(expectedException.isAssignableFrom(actualEx.getClass())); | |||
} | |||
} |
@@ -80,7 +80,7 @@ public class RSAUtilsTest { | |||
} | |||
@Test | |||
public void retrievePublicKeyTest(){ | |||
public void test(){ | |||
AsymmetricCipherKeyPair kp = RSAUtils.generateKeyPair(); | |||
RSAKeyParameters pubKey = (RSAKeyParameters) kp.getPublic(); | |||
@@ -91,38 +91,17 @@ public class RSAUtilsTest { | |||
byte[] retrievedPubKeyBytes = RSAUtils.retrievePublicKey(privKeyBytes); | |||
assertArrayEquals(pubKeyBytes,retrievedPubKeyBytes); | |||
} | |||
@Test | |||
public void signTest(){ | |||
byte[] data = new byte[1024]; | |||
byte[] data = new byte[128]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
byte[] privKeyBytes = RSAUtils.privKey2Bytes_RawKey((RSAPrivateCrtKeyParameters) privKey); | |||
byte[] signatureFromPrivKey = RSAUtils.sign(data, privKey); | |||
byte[] signatureFromPrivKeyBytes = RSAUtils.sign(data, privKeyBytes); | |||
assertNotNull(signatureFromPrivKey); | |||
assertEquals(2048 / 8, signatureFromPrivKey.length); | |||
assertArrayEquals(signatureFromPrivKeyBytes,signatureFromPrivKey); | |||
} | |||
@Test | |||
public void verifyTest(){ | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
AsymmetricKeyParameter pubKey = keyPair.getPublic(); | |||
byte[] pubKeyBytes = RSAUtils.pubKey2Bytes_RawKey((RSAKeyParameters) pubKey); | |||
byte[] signature = RSAUtils.sign(data,privKey); | |||
@@ -131,46 +110,23 @@ public class RSAUtilsTest { | |||
assertTrue(isValidFromPubKey); | |||
assertTrue(isValidFromPubKeyBytes); | |||
} | |||
@Test | |||
public void encryptTest(){ | |||
byte[] data = new byte[246]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter pubKey = keyPair.getPublic(); | |||
byte[] pubKeyBytes = RSAUtils.pubKey2Bytes_RawKey((RSAKeyParameters) pubKey); | |||
byte[] ciphertextFromPubKey = RSAUtils.encrypt(data,pubKey); | |||
byte[] ciphertextFromPubKeyBytes = RSAUtils.encrypt(data,pubKeyBytes); | |||
assertEquals(512,ciphertextFromPubKey.length); | |||
assertEquals(512,ciphertextFromPubKeyBytes.length); | |||
} | |||
@Test | |||
public void decryptTest(){ | |||
AsymmetricCipherKeyPair keyPair = RSAUtils.generateKeyPair(); | |||
AsymmetricKeyParameter pubKey = keyPair.getPublic(); | |||
AsymmetricKeyParameter privKey = keyPair.getPrivate(); | |||
byte[] privKeyBytes = RSAUtils.privKey2Bytes_RawKey((RSAPrivateCrtKeyParameters) privKey); | |||
assertEquals(256,ciphertextFromPubKey.length); | |||
assertEquals(256,ciphertextFromPubKeyBytes.length); | |||
Random random = new Random(); | |||
byte[] data; | |||
data = new byte[1024]; | |||
random.nextBytes(data); | |||
byte[] ciphertext = RSAUtils.encrypt(data, pubKey); | |||
data = new byte[1024]; | |||
random.nextBytes(data); | |||
byte[] ciphertext = RSAUtils.encrypt(data, pubKey); | |||
byte[] plaintextFromPrivKey = RSAUtils.decrypt(ciphertext, privKey); | |||
byte[] plaintextFromPrivKeyBytes = RSAUtils.decrypt(ciphertext, privKeyBytes); | |||
byte[] plaintextFromPrivKey = RSAUtils.decrypt(ciphertext, privKey); | |||
byte[] plaintextFromPrivKeyBytes = RSAUtils.decrypt(ciphertext, privKeyBytes); | |||
assertArrayEquals(data, plaintextFromPrivKey); | |||
assertArrayEquals(data, plaintextFromPrivKeyBytes); | |||
assertArrayEquals(data, plaintextFromPrivKey); | |||
assertArrayEquals(data, plaintextFromPrivKeyBytes); | |||
} | |||
@@ -1,5 +1,6 @@ | |||
//package test.com.jd.blockchain.crypto.utils.classic; | |||
// | |||
//import com.jd.blockchain.crypto.utils.classic.SSHKeyParser; | |||
//import org.bouncycastle.asn1.ASN1Sequence; | |||
//import org.bouncycastle.crypto.params.RSAKeyParameters; | |||
//import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters; | |||
@@ -17,6 +18,8 @@ | |||
//import java.io.StringReader; | |||
//import java.math.BigInteger; | |||
// | |||
//import static org.junit.Assert.assertEquals; | |||
// | |||
///** | |||
// * @author zhanglin33 | |||
// * @title: SSHKeyUtilsTest | |||
@@ -34,26 +37,40 @@ | |||
// "e/9gMujAtDoolJ181a9P06bvEpIw5cLtUnsm5CtvBuiL7WBXxDJ/IASJrKNGBdK8xib1+Kb8tNLAT6" + | |||
// "Dj25BwylqiRNhb5l1Ni4aKrE2FqSEc5Nx5+csQMEl9MBJ3pEsLHBNbohDL+jbwLguRVD6CJ"; | |||
// | |||
// byte[] pubKeyBytes = Base64.decode(pubKeyStr); | |||
// System.out.println(Hex.toHexString(pubKeyBytes)); | |||
// OpenSSHPublicKeySpec pubKeySpec = new OpenSSHPublicKeySpec(pubKeyBytes); | |||
// BigInteger exponent = new BigInteger("010001",16); | |||
// BigInteger modulus = new BigInteger("0098c0b378117cbb8345ee82fe2541fa2e8db0118b8f2" + | |||
// "03d2c55ba9b46b12a06aac4b4d1ef6b03bbf4ab314a6bd6619f0d569230a711745800c88200962" + | |||
// "433f98623c99bdea91c74d84659998f9e8a55f83e3a093141d78c9e47d9a3a0e3d7b38a635f7cc" + | |||
// "6bde2b1e4dc7c8ef76815ec1b4c2b5d374c755d64e3353e5b5ea3ffe205e42c4344539b4bb1654" + | |||
// "03aed0c13f35effd80cba302d0e8a25275f356bd3f4e9bbc4a48c3970bb549ec9b90adbc1ba22f" + | |||
// "b5815f10c9fc801226b28d18174af3189bd7e29bf2d34b013e838f6e41c3296a8913616f997536" + | |||
// "2e1a2ab13616a484739371e7e72c40c125f4c049de912c2c704d6e88432fe8dbc0b82e4550fa089",16); | |||
// String pubKeyFormat = "OpenSSH"; | |||
// String pubKeyType = "ssh-rsa"; | |||
// | |||
// String pubKeyFormat = pubKeySpec.getFormat(); | |||
// String pubKeyType = pubKeySpec.getType(); | |||
// System.out.println(pubKeyFormat); | |||
// System.out.println(pubKeyType); | |||
// SSHKeyParser parser = new SSHKeyParser(); | |||
// | |||
// RSAKeyParameters pubKey = (RSAKeyParameters) OpenSSHPublicKeyUtil.parsePublicKey(pubKeyBytes); | |||
// RSAKeyParameters pubKey = (RSAKeyParameters) parser.pubKeyParse(pubKeyStr); | |||
// BigInteger e = pubKey.getExponent(); | |||
// BigInteger n = pubKey.getModulus(); | |||
// System.out.println(Hex.toHexString(e.toByteArray())); | |||
// System.out.println(Hex.toHexString(n.toByteArray())); | |||
// | |||
// System.out.println(); | |||
// System.out.println("-------------------------------------------------------"); | |||
// System.out.println(); | |||
// | |||
// | |||
// assertEquals(exponent,e); | |||
// assertEquals(modulus,n); | |||
// assertEquals(pubKeyFormat,parser.getPubKeyFormat()); | |||
// assertEquals(pubKeyType,parser.getPubKeyType()); | |||
// | |||
// String pubKeyStrWithHead = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCYwLN4EXy7g0Xugv4lQfou" + | |||
// "jbARi48gPSxVuptGsSoGqsS00e9rA7v0qzFKa9Zhnw1WkjCnEXRYAMiCAJYkM/mGI8mb3qkcdNhGWZm" + | |||
// "PnopV+D46CTFB14yeR9mjoOPXs4pjX3zGveKx5Nx8jvdoFewbTCtdN0x1XWTjNT5bXqP/4gXkLENEU5" + | |||
// "tLsWVAOu0ME/Ne/9gMujAtDoolJ181a9P06bvEpIw5cLtUnsm5CtvBuiL7WBXxDJ/IASJrKNGBdK8xi" + | |||
// "b1+Kb8tNLAT6Dj25BwylqiRNhb5l1Ni4aKrE2FqSEc5Nx5+csQMEl9MBJ3pEsLHBNbohDL+jbwLguRV" + | |||
// "D6CJ zhanglin33@zhanglin33.local\n"; | |||
// pubKey = (RSAKeyParameters) parser.pubKeyParse(pubKeyStrWithHead); | |||
// e = pubKey.getExponent(); | |||
// n = pubKey.getModulus(); | |||
// assertEquals(exponent,e); | |||
// assertEquals(modulus,n); | |||
// assertEquals(pubKeyFormat,parser.getPubKeyFormat()); | |||
// assertEquals(pubKeyType,parser.getPubKeyType()); | |||
// } | |||
// | |||
// @Test | |||
@@ -93,6 +110,7 @@ | |||
// } catch (IOException e1) { | |||
// e1.printStackTrace(); | |||
// } | |||
// assert Bytes2 != null; | |||
// System.out.println(Hex.toHexString(Bytes2)); | |||
// | |||
// String str3 = "-----BEGIN OPENSSH PRIVATE KEY-----\n" + | |||
@@ -129,6 +147,7 @@ | |||
// } catch (IOException e1) { | |||
// e1.printStackTrace(); | |||
// } | |||
// assert Bytes3 != null; | |||
// System.out.println(Hex.toHexString(Bytes3)); | |||
//// System.out.println(Hex.toHexString(Base64.decode("oNE9iA4ZyuZLbpEL7B29NaxGi4puT2Y5RDaMoEkoAKI"))); | |||
//// String test = "1ac477fa"; | |||
@@ -37,7 +37,7 @@ public class CertParser { | |||
try { | |||
issuerCert.checkValidity(); | |||
} catch (CertificateExpiredException | CertificateNotYetValidException e) { | |||
e.printStackTrace(); | |||
throw new CryptoException(e.getMessage(), e); | |||
} | |||
PublicKey issuerPubKey = issuerCert.getPublicKey(); | |||
X500Principal issuerPrincipal = issuerCert.getSubjectX500Principal(); | |||
@@ -41,26 +41,21 @@ public class SHA1WITHRSA2048SignatureFunctionTest { | |||
} | |||
@Test | |||
public void generateKeyPairTest() { | |||
public void test() { | |||
// generateKeyPairTest | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
assertEquals(PUBLIC.CODE, pubKey.getKeyType().CODE); | |||
assertTrue(pubKey.getRawKeyBytes().length > 259); | |||
assertEquals(PRIVATE.CODE, privKey.getKeyType().CODE); | |||
assertTrue(privKey.getRawKeyBytes().length > 1155); | |||
assertEquals(algorithm.code(), pubKey.getAlgorithm()); | |||
assertEquals(algorithm.code(), privKey.getAlgorithm()); | |||
byte[] algoBytes = CryptoAlgorithm.getCodeBytes(algorithm); | |||
byte[] pubKeyTypeBytes = new byte[] { PUBLIC.CODE }; | |||
byte[] privKeyTypeBytes = new byte[] { PRIVATE.CODE }; | |||
@@ -68,200 +63,62 @@ public class SHA1WITHRSA2048SignatureFunctionTest { | |||
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 = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
// retrievePubKeyTest | |||
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()); | |||
} | |||
@Test | |||
public void signTest() { | |||
// signTest | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureBytes = signatureDigest.toBytes(); | |||
assertEquals(2 + 256, signatureBytes.length); | |||
assertEquals(algorithm.code(), signatureDigest.getAlgorithm()); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA2048.code(), signatureDigest.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 31 & 0x00FF)), | |||
signatureDigest.getAlgorithm()); | |||
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); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
// verifyTest | |||
assertTrue(signatureFunction.verify(signatureDigest, pubKey, data)); | |||
} | |||
@Test | |||
public void supportPrivKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
// supportPrivKeyTest | |||
byte[] privKeyBytes = privKey.toBytes(); | |||
assertTrue(signatureFunction.supportPrivKey(privKeyBytes)); | |||
} | |||
@Test | |||
public void resolvePrivKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
byte[] privKeyBytes = privKey.toBytes(); | |||
// resolvePrivKeyTest | |||
PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes); | |||
assertEquals(PRIVATE.CODE, resolvedPrivKey.getKeyType().CODE); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA2048.code(), resolvedPrivKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 31 & 0x00FF)), | |||
resolvedPrivKey.getAlgorithm()); | |||
assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes()); | |||
} | |||
@Test | |||
public void supportPubKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
// supportPubKeyTest | |||
byte[] pubKeyBytes = pubKey.toBytes(); | |||
assertTrue(signatureFunction.supportPubKey(pubKeyBytes)); | |||
} | |||
@Test | |||
public void resolvePubKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
byte[] pubKeyBytes = pubKey.toBytes(); | |||
// resolvedPubKeyTest | |||
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes); | |||
assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA2048.code(), resolvedPubKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 31 & 0x00FF)), | |||
resolvedPubKey.getAlgorithm()); | |||
assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes()); | |||
} | |||
@Test | |||
public void supportDigestTest() { | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
//supportDigestTest | |||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||
assertTrue(signatureFunction.supportDigest(signatureDigestBytes)); | |||
} | |||
@Test | |||
public void resolveDigestTest() { | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA2048"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
AsymmetricKeypair keyPair = signatureFunction.generateKeypair(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||
// resolveDigestTest | |||
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes); | |||
assertEquals(256, resolvedSignatureDigest.getRawDigest().length); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA2048.code(), resolvedSignatureDigest.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 31 & 0x00FF)), | |||
@@ -20,9 +20,6 @@ import static org.junit.Assert.*; | |||
*/ | |||
public class SHA1WITHRSA4096SignatureFunctionTest { | |||
private AsymmetricKeypair keyPair = Crypto.getSignatureFunction(Crypto.getAlgorithm("SHA1WITHRSA4096")). | |||
generateKeypair(); | |||
@Test | |||
public void getAlgorithmTest() { | |||
@@ -44,12 +41,14 @@ public class SHA1WITHRSA4096SignatureFunctionTest { | |||
assertNull(algorithm); | |||
} | |||
@Test | |||
//@Test | |||
public void generateKeyPairTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
AsymmetricKeypair keyPair = Crypto.getSignatureFunction(algorithm).generateKeypair(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
@@ -68,125 +67,58 @@ public class SHA1WITHRSA4096SignatureFunctionTest { | |||
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 = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
// retrievePubKeyTest | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
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(), retrievedPubKey.getAlgorithm()); | |||
assertArrayEquals(pubKey.toBytes(), retrievedPubKey.toBytes()); | |||
} | |||
@Test | |||
public void signAndVerifyTest() { | |||
// signAndVerifyTest | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
byte[] signatureBytes = signatureDigest.toBytes(); | |||
assertEquals(2 + 512, signatureBytes.length); | |||
assertEquals(algorithm.code(), signatureDigest.getAlgorithm()); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA4096.code(), signatureDigest.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 32 & 0x00FF)), | |||
signatureDigest.getAlgorithm()); | |||
byte[] algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm()); | |||
algoBytes = BytesUtils.toBytes(signatureDigest.getAlgorithm()); | |||
byte[] rawSinatureBytes = signatureDigest.getRawDigest(); | |||
assertArrayEquals(BytesUtils.concat(algoBytes, rawSinatureBytes), signatureBytes); | |||
assertTrue(signatureFunction.verify(signatureDigest, pubKey, data)); | |||
} | |||
@Test | |||
public void supportAndResolvePrivKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
// supportAndResolvePrivKeyTest | |||
byte[] privKeyBytes = privKey.toBytes(); | |||
assertTrue(signatureFunction.supportPrivKey(privKeyBytes)); | |||
PrivKey resolvedPrivKey = signatureFunction.resolvePrivKey(privKeyBytes); | |||
assertEquals(PRIVATE.CODE, resolvedPrivKey.getKeyType().CODE); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA4096.code(), resolvedPrivKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 32 & 0x00FF)), | |||
resolvedPrivKey.getAlgorithm()); | |||
assertArrayEquals(privKeyBytes, resolvedPrivKey.toBytes()); | |||
} | |||
@Test | |||
public void supportAndResolvePubKeyTest() { | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
PubKey pubKey = keyPair.getPubKey(); | |||
// supportAndResolvePubKeyTest | |||
byte[] pubKeyBytes = pubKey.toBytes(); | |||
assertTrue(signatureFunction.supportPubKey(pubKeyBytes)); | |||
PubKey resolvedPubKey = signatureFunction.resolvePubKey(pubKeyBytes); | |||
assertEquals(PUBLIC.CODE, resolvedPubKey.getKeyType().CODE); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA4096.code(), resolvedPubKey.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 32 & 0x00FF)), | |||
resolvedPubKey.getAlgorithm()); | |||
assertArrayEquals(pubKeyBytes, resolvedPubKey.toBytes()); | |||
} | |||
@Test | |||
public void supportAndResolveDigestTest() { | |||
byte[] data = new byte[1024]; | |||
Random random = new Random(); | |||
random.nextBytes(data); | |||
CryptoAlgorithm algorithm = Crypto.getAlgorithm("SHA1WITHRSA4096"); | |||
assertNotNull(algorithm); | |||
SignatureFunction signatureFunction = Crypto.getSignatureFunction(algorithm); | |||
PrivKey privKey = keyPair.getPrivKey(); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, data); | |||
// supportAndResolveDigestTest | |||
byte[] signatureDigestBytes = signatureDigest.toBytes(); | |||
assertTrue(signatureFunction.supportDigest(signatureDigestBytes)); | |||
SignatureDigest resolvedSignatureDigest = signatureFunction.resolveDigest(signatureDigestBytes); | |||
assertEquals(512, resolvedSignatureDigest.getRawDigest().length); | |||
assertEquals(PKIAlgorithm.SHA1WITHRSA4096.code(), resolvedSignatureDigest.getAlgorithm()); | |||
assertEquals((short) (SIGNATURE_ALGORITHM | ASYMMETRIC_KEY | ((byte) 32 & 0x00FF)), | |||
@@ -203,7 +203,7 @@ public class CSRBuilderTest { | |||
} | |||
@Test | |||
// @Test | |||
public void SHA1withRSA4096CSRTest(){ | |||
String countryName = "CN"; | |||