use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.
the class RandomPrivateKeyX509ExtendedKeyManager method getPrivateKey.
@Override
public PrivateKey getPrivateKey(String alias) {
PrivateKey originalPrivateKey = super.getPrivateKey(alias);
if (originalPrivateKey == null) {
return null;
}
PrivateKey result;
String keyAlgorithm = originalPrivateKey.getAlgorithm();
try {
KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
if ("RSA".equals(keyAlgorithm)) {
RSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, RSAPrivateKeySpec.class);
int keyLengthBits = originalKeySpec.getModulus().bitLength();
// Use a cache because RSA key generation is slow.
String cacheKey = keyAlgorithm + "-" + keyLengthBits;
result = cachedKeys.get(cacheKey);
if (result == null) {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
keyPairGenerator.initialize(keyLengthBits);
result = keyPairGenerator.generateKeyPair().getPrivate();
cachedKeys.put(cacheKey, result);
}
} else if ("DSA".equals(keyAlgorithm)) {
DSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, DSAPrivateKeySpec.class);
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
keyPairGenerator.initialize(new DSAParameterSpec(originalKeySpec.getP(), originalKeySpec.getQ(), originalKeySpec.getG()));
result = keyPairGenerator.generateKeyPair().getPrivate();
} else {
Assert.fail("Unsupported key algorithm: " + originalPrivateKey.getAlgorithm());
result = null;
}
} catch (GeneralSecurityException e) {
Assert.fail("Failed to generate private key: " + e);
result = null;
}
return result;
}
use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.
the class DSAPrivateKeySpecTest method testGetX.
/**
* getX() test
*/
public final void testGetX() {
DSAPrivateKeySpec dpks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
assertEquals(1, dpks.getX().intValue());
}
use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.
the class DSAPrivateKeySpecTest method testGetG.
/**
* getG() test
*/
public final void testGetG() {
DSAPrivateKeySpec dpks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
assertEquals(4, dpks.getG().intValue());
}
use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA1withDSA_Key_Success.
public void testSign_SHA1withDSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(DSA_priv, DSA_P, DSA_Q, DSA_G);
PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.
the class KeyStore2Test method test_getCreationDate.
/**
* java.security.KeyStore#getCreationDate(String)
*/
public void test_getCreationDate() throws Exception {
String type = "DSA";
KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
try {
keyTest.getCreationDate("anAlias");
fail();
} catch (KeyStoreException expected) {
}
keyTest.load(null, pssWord);
assertNull(keyTest.getCreationDate(""));
try {
keyTest.getCreationDate(null);
fail();
} catch (NullPointerException expected) {
}
Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
PrivateKey privateKey1 = KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0")));
KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
keyTest.setEntry("alias1", pke, pp);
keyTest.setEntry("alias2", pke1, pp);
Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int dayExpected = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int monthExpected = Calendar.getInstance().get(Calendar.MONTH);
int yearExpected = Calendar.getInstance().get(Calendar.YEAR);
int hourExpected = Calendar.getInstance().get(Calendar.HOUR);
int minuteExpected = Calendar.getInstance().get(Calendar.MINUTE);
Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias1").getTime());
int dayActual1 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int monthActual1 = Calendar.getInstance().get(Calendar.MONTH);
int yearActual1 = Calendar.getInstance().get(Calendar.YEAR);
int hourActual1 = Calendar.getInstance().get(Calendar.HOUR);
int minuteActual1 = Calendar.getInstance().get(Calendar.MINUTE);
assertEquals(dayExpected, dayActual1);
assertEquals(monthExpected, monthActual1);
assertEquals(yearExpected, yearActual1);
assertEquals(hourExpected, hourActual1);
assertEquals(minuteExpected, minuteActual1);
Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias2").getTime());
int dayActual2 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int monthActual2 = Calendar.getInstance().get(Calendar.MONTH);
int yearActual2 = Calendar.getInstance().get(Calendar.YEAR);
int hourActual2 = Calendar.getInstance().get(Calendar.HOUR);
int minuteActual2 = Calendar.getInstance().get(Calendar.MINUTE);
assertEquals(dayExpected, dayActual2);
assertEquals(monthExpected, monthActual2);
assertEquals(yearExpected, yearActual2);
assertEquals(hourExpected, hourActual2);
assertEquals(minuteExpected, minuteActual2);
try {
keyTest.getCreationDate(null);
fail();
} catch (NullPointerException expected) {
}
}
Aggregations