use of java.security.KeyPairGenerator in project nhin-d by DirectProject.
the class CRLRevocationManager_getCrlFromUriTest method testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound.
public void testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound() throws Exception {
CRLRevocationManager.initCRLCacheLocation();
String uri = "http://localhost:8080/certs.crl";
X509CRL crl = (X509CRL) TestUtils.loadCRL("certs.crl");
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
KeyPair pair = kpGen.generateKeyPair();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 10);
X509V2CRLGenerator crlGen = new X509V2CRLGenerator();
crlGen.setIssuerDN(new X500Principal("CN=Test CRL"));
crlGen.setNextUpdate(cal.getTime());
crlGen.setSignatureAlgorithm("SHA256withRSAEncryption");
crlGen.setThisUpdate(Calendar.getInstance().getTime());
crlGen.addCRL(crl);
crl = crlGen.generate(pair.getPrivate(), "BC");
CRLRevocationManager.INSTANCE.writeCRLCacheFile(uri, crl);
X509CRL retCrl = CRLRevocationManager.getInstance().getCrlFromUri(uri);
assertNotNull(retCrl);
assertEquals(crl, retCrl);
}
use of java.security.KeyPairGenerator in project android_frameworks_base by DirtyUnicorns.
the class AndroidKeyPairGeneratorTest method testKeyPairGenerator_GenerateKeyPair_EC_Unencrypted_Success.
public void testKeyPairGenerator_GenerateKeyPair_EC_Unencrypted_Success() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC", "AndroidKeyStore");
generator.initialize(new KeyGenParameterSpec.Builder(TEST_ALIAS_1, KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY).setCertificateSubject(TEST_DN_1).setCertificateSerialNumber(TEST_SERIAL_1).setCertificateNotBefore(NOW).setCertificateNotAfter(NOW_PLUS_10_YEARS).setDigests(KeyProperties.DIGEST_SHA256).build());
final KeyPair pair = generator.generateKeyPair();
assertNotNull("The KeyPair returned should not be null", pair);
assertKeyPairCorrect(pair, TEST_ALIAS_1, "EC", 256, null, TEST_DN_1, TEST_SERIAL_1, NOW, NOW_PLUS_10_YEARS);
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class FinalizeHalf method test.
static void test(String algo, Provider provider, boolean priv, Consumer<Key> method) throws Exception {
KeyPairGenerator generator;
try {
generator = KeyPairGenerator.getInstance(algo, provider);
} catch (NoSuchAlgorithmException nsae) {
return;
}
System.out.println("Checking " + provider.getName() + ", " + algo);
KeyPair pair = generator.generateKeyPair();
Key key = priv ? pair.getPrivate() : pair.getPublic();
pair = null;
for (int i = 0; i < 32; ++i) {
System.gc();
}
try {
method.accept(key);
} catch (ProviderException pe) {
failures++;
}
}
use of java.security.KeyPairGenerator in project kdeconnect-android by KDE.
the class RsaHelper method initialiseRsaKeys.
public static void initialiseRsaKeys(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
if (!settings.contains("publicKey") || !settings.contains("privateKey")) {
KeyPair keyPair;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
keyPair = keyGen.genKeyPair();
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/initializeRsaKeys", "Exception");
return;
}
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
SharedPreferences.Editor edit = settings.edit();
edit.putString("publicKey", Base64.encodeToString(publicKey, 0).trim() + "\n");
edit.putString("privateKey", Base64.encodeToString(privateKey, 0));
edit.apply();
}
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class Offsets method init.
static Offsets init(String provider, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
// fill the cleartext data with random bytes
byte[] cleartext = new byte[100];
RandomFactory.getRandom().nextBytes(cleartext);
// NONEwith requires input to be of 20 bytes
int size = algorithm.contains("NONEwith") ? 20 : 100;
// create signature instance
Signature signature = Signature.getInstance(algorithm, provider);
String keyAlgo;
if (algorithm.contains("RSA")) {
keyAlgo = "RSA";
} else if (algorithm.contains("ECDSA")) {
keyAlgo = "EC";
} else if (algorithm.contains("DSA")) {
keyAlgo = "DSA";
} else {
throw new RuntimeException("Test doesn't support this signature " + "algorithm: " + algorithm);
}
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
KeyPair kp = kpg.generateKeyPair();
PublicKey pubkey = kp.getPublic();
PrivateKey privkey = kp.getPrivate();
return new Offsets(signature, pubkey, privkey, size, cleartext);
}
Aggregations