use of java.security.SecureRandom in project robovm by robovm.
the class KeyGeneratorTest method test_KeyGenerator.
private void test_KeyGenerator(KeyGenerator kg) throws Exception {
if (isUnsupported(kg)) {
return;
}
kg.init((SecureRandom) null);
test_SecretKey(kg, kg.generateKey());
kg.init(new SecureRandom());
test_SecretKey(kg, kg.generateKey());
String algorithm = kg.getAlgorithm();
List<Integer> keySizes = getKeySizes(algorithm);
for (int keySize : keySizes) {
kg.init(keySize);
test_SecretKey(kg, kg.generateKey());
kg.init(keySize, (SecureRandom) null);
test_SecretKey(kg, kg.generateKey());
kg.init(keySize, new SecureRandom());
test_SecretKey(kg, kg.generateKey());
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class CipherTest method getEncryptAlgorithmParameterSpec.
private static AlgorithmParameterSpec getEncryptAlgorithmParameterSpec(String algorithm) {
if (isPBE(algorithm)) {
final byte[] salt = new byte[8];
new SecureRandom().nextBytes(salt);
return new PBEParameterSpec(salt, 1024);
}
if (algorithm.equals("AES/CBC/NOPADDING") || algorithm.equals("AES/CBC/PKCS5PADDING") || algorithm.equals("AES/CFB/NOPADDING") || algorithm.equals("AES/CTR/NOPADDING") || algorithm.equals("AES/CTS/NOPADDING") || algorithm.equals("AES/OFB/NOPADDING")) {
final byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}
return null;
}
use of java.security.SecureRandom in project robovm by robovm.
the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.
public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
// java.lang.Class)
for (int i = 0; i < keyfactAlgs.length; i++) {
KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
// We don't use getInstance
SecureRandom random = new SecureRandom();
keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
KeyPair keys = keyGen.generateKeyPair();
if (keepalive != null) {
keepalive.interrupt();
}
KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
PublicKey publicKey = fact.generatePublic(publicKeySpec);
assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class KeyPairGenerator1Test method testKeyPairGenerator13.
/**
* Test for methods: <code>initialize(int keysize)</code>
* <code>initialize(int keysize, SecureRandom random)</code>
* <code>initialize(AlgorithmParameterSpec param)</code>
* <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
* <code>generateKeyPair()</code>
* <code>genKeyPair()</code>
* Assertion: initialize(int ...) throws InvalidParameterException when
* keysize in incorrect Assertion: initialize(AlgorithmParameterSpec
* ...)throws UnsupportedOperationException Assertion: generateKeyPair() and
* genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2
* is used
*/
public void testKeyPairGenerator13() {
int[] keys = { -1, -250, 1, 63, -512, -1024 };
SecureRandom random = new SecureRandom();
KeyPairGenerator mKPG = new MyKeyPairGenerator2(null);
assertEquals("Algorithm must be null", mKPG.getAlgorithm(), MyKeyPairGenerator2.getResAlgorithm());
assertNull("genKeyPair() must return null", mKPG.genKeyPair());
assertNull("generateKeyPair() mut return null", mKPG.generateKeyPair());
for (int i = 0; i < keys.length; i++) {
try {
mKPG.initialize(keys[i]);
fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
} catch (InvalidParameterException e) {
}
try {
mKPG.initialize(keys[i], random);
fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
} catch (InvalidParameterException e) {
}
}
try {
mKPG.initialize(64);
} catch (InvalidParameterException e) {
fail("Unexpected InvalidParameterException was thrown");
}
try {
mKPG.initialize(64, null);
} catch (InvalidParameterException e) {
fail("Unexpected InvalidParameterException was thrown");
}
try {
mKPG.initialize(null, random);
} catch (UnsupportedOperationException e) {
// on j2se1.4 this exception is not thrown
} catch (InvalidAlgorithmParameterException e) {
fail("Unexpected InvalidAlgorithmParameterException was thrown");
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class KeyPairGenerator1Test method testKeyPairGenerator11.
/**
* Test for methods:
* <code>initialize(int keysize)</code>
* <code>initialize(int keysize, SecureRandom random)</code>
* <code>initialize(AlgorithmParameterSpec param)</code>
* <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
* Assertion: throws InvalidParameterException or
* InvalidAlgorithmParameterException when parameters keysize or param are
* incorrect
*/
public void testKeyPairGenerator11() throws NoSuchAlgorithmException, NoSuchProviderException {
if (!DSASupported) {
fail(NotSupportMsg);
return;
}
int[] keys = { -10000, -1024, -1, 0, 10000 };
KeyPairGenerator[] kpg = createKPGen();
assertNotNull("KeyPairGenerator objects were not created", kpg);
SecureRandom random = new SecureRandom();
AlgorithmParameterSpec aps = null;
for (int i = 0; i < kpg.length; i++) {
for (int j = 0; j < keys.length; j++) {
try {
kpg[i].initialize(keys[j]);
kpg[i].initialize(keys[j], random);
} catch (InvalidParameterException e) {
}
}
try {
kpg[i].initialize(aps);
kpg[i].initialize(aps, random);
} catch (InvalidAlgorithmParameterException e) {
}
}
}
Aggregations