use of java.security.Provider.Service in project j2objc by google.
the class SecureRandom method getInstanceFromCryptoProvider.
private static SecureRandom getInstanceFromCryptoProvider(String algorithm) throws NoSuchAlgorithmException {
Provider cryptoProvider;
try {
cryptoProvider = (Provider) SecureRandom.class.getClassLoader().loadClass("org.apache.harmony.security.provider.crypto.CryptoProvider").newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
Service service = cryptoProvider.getService("SecureRandom", algorithm);
Instance instance = GetInstance.getInstance(service, SecureRandomSpi.class);
return new SecureRandom((SecureRandomSpi) instance.impl, instance.provider, algorithm);
}
use of java.security.Provider.Service in project robovm by robovm.
the class ProviderTest method testGetServices.
// END android-added
// BEGIN android-added
public final void testGetServices() {
MyProvider myProvider = new MyProvider(null, 1, null);
Set<Provider.Service> services = myProvider.getServices();
assertEquals(0, services.size());
Provider.Service[] s = new Provider.Service[3];
s[0] = new Provider.Service(p, "type1", "algorithm1", "className1", null, null);
s[1] = new Provider.Service(p, "type2", "algorithm2", "className2", null, null);
s[2] = new Provider.Service(p, "type3", "algorithm3", "className3", null, null);
myProvider.putService(s[0]);
myProvider.putService(s[1]);
assertEquals(2, myProvider.getNumServices());
Set<Service> actual = myProvider.getServices();
assertTrue(actual.contains(s[0]));
assertTrue(actual.contains(s[1]));
assertTrue(!actual.contains(s[2]));
myProvider.removeService(s[1]);
actual = myProvider.getServices();
assertEquals(1, myProvider.getNumServices());
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(!actual.contains(s[2]));
myProvider.putService(s[2]);
actual = myProvider.getServices();
assertEquals(2, myProvider.getNumServices());
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
}
use of java.security.Provider.Service in project robovm by robovm.
the class ProviderTest method testPutObjectObject.
/*
* Class under test for Object put(Object, Object)
*/
public final void testPutObjectObject() {
p.put("MessageDigest.SHA-1", "aaa.bbb.ccc.ddd");
p.put("Type.Algorithm", "className");
assertEquals("aaa.bbb.ccc.ddd", p.getProperty("MessageDigest.SHA-1").trim());
Set services = p.getServices();
assertEquals(3, services.size());
for (Iterator it = services.iterator(); it.hasNext(); ) {
Provider.Service s = (Provider.Service) it.next();
if ("Type".equals(s.getType()) && "Algorithm".equals(s.getAlgorithm()) && "className".equals(s.getClassName())) {
continue;
}
if ("MessageDigest".equals(s.getType()) && "SHA-1".equals(s.getAlgorithm()) && "aaa.bbb.ccc.ddd".equals(s.getClassName())) {
continue;
}
if ("MessageDigest".equals(s.getType()) && "abc".equals(s.getAlgorithm()) && "SomeClassName".equals(s.getClassName())) {
continue;
}
fail("Incorrect service");
}
}
use of java.security.Provider.Service in project robovm by robovm.
the class KeyPairGeneratorTest method test_KeyWithAllKeyFactories.
private void test_KeyWithAllKeyFactories(Key k) throws Exception {
byte[] encoded = k.getEncoded();
String keyAlgo = k.getAlgorithm();
Provider[] providers = Security.getProviders();
for (Provider p : providers) {
Set<Provider.Service> services = p.getServices();
for (Provider.Service service : services) {
if (!"KeyFactory".equals(service.getType())) {
continue;
}
if (!service.getAlgorithm().equals(keyAlgo)) {
continue;
}
if ("PKCS#8".equals(k.getFormat())) {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
PrivateKey privKey = kf.generatePrivate(spec);
assertNotNull(k.getAlgorithm() + ", provider=" + p.getName(), privKey);
/*
* EC keys are unique because they can have explicit parameters or a curve
* name. Check them specially so this test can continue to function.
*/
if (k instanceof ECPrivateKey) {
assertECPrivateKeyEquals((ECPrivateKey) k, (ECPrivateKey) privKey);
} else {
assertEquals(k.getAlgorithm() + ", provider=" + p.getName(), Arrays.toString(encoded), Arrays.toString(privKey.getEncoded()));
}
} else if ("X.509".equals(k.getFormat())) {
X509EncodedKeySpec spec = new X509EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
PublicKey pubKey = kf.generatePublic(spec);
assertNotNull(pubKey);
assertTrue(Arrays.equals(encoded, pubKey.getEncoded()));
}
}
}
}
use of java.security.Provider.Service in project robovm by robovm.
the class KeyPairGeneratorTest method testDSAGeneratorWithParams.
public void testDSAGeneratorWithParams() throws Exception {
final DSAParameterSpec dsaSpec = new DSAParameterSpec(DSA_P, DSA_Q, DSA_G);
boolean failure = false;
final Provider[] providers = Security.getProviders();
for (final Provider p : providers) {
Service s = p.getService("KeyPairGenerator", "DSA");
if (s == null) {
continue;
}
final KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
kpg.initialize(dsaSpec);
KeyPair pair = kpg.generateKeyPair();
DSAPrivateKey privKey = (DSAPrivateKey) pair.getPrivate();
DSAPublicKey pubKey = (DSAPublicKey) pair.getPublic();
DSAParams actualParams = privKey.getParams();
assertNotNull("DSA params should not be null", actualParams);
assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
actualParams = pubKey.getParams();
assertNotNull("DSA params should not be null", actualParams);
assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
}
}
Aggregations