/** * Converts a specified key for a specified algorithm to a PublicKey which is then stored in * a KeyPair. The private key of the KeyPair is null. Can not handle DSA keys. * @param type The type of key being passed in * @param pub Public key as byte[] * @return Public key as KeyPair with a null private key */ public static KeyPair getPublicKeyPair(KeyPairType type, byte[] pub) { return getKeyPair(getPublicKey(type, pub), null); }
/** * Converts the specified keys for a specified algorithm to PrivateKey and PublicKey * respectively. These are then placed in a KeyPair. Can not handle DSA keys. * @param type The type of key being passed in * @param pub Public key as ByteBuffer * @param pri Private key as ByteBuffer * @return The public key and private key in a KeyPair */ public static KeyPair getKeyPair(KeyPairType type, ByteBuffer pub, ByteBuffer pri) { return getKeyPair(type, Fields.copyToArray(pub), Fields.copyToArray(pri)); }
return getKeyPair(pubK, privK); } catch (UnsupportedTypeException e) { throw new Error(e); // Should be impossible
@Test (expected = UnsupportedTypeException.class) public void testGetKeyPairKeyPairTypeByteArrayDSAType() { byte[] nullArray = null; KeyGenUtils.getKeyPair(falseKeyPairType, nullArray, nullArray); }
@Test (expected = NullPointerException.class) public void testGetKeyPairKeyPairTypeByteArrayNullInput2() { KeyGenUtils.getKeyPair(trueKeyPairTypes[0], null, truePrivateKeys[0]); }
@Test (expected = NullPointerException.class) public void testGetKeyPairKeyPairTypeByteArrayNullInput3() { KeyGenUtils.getKeyPair(trueKeyPairTypes[0], truePublicKeys[0], null); }
@Test (expected = NullPointerException.class) public void testGetKeyPairKeyPairTypeByteArrayNullInput1() { KeyGenUtils.getKeyPair(null, truePublicKeys[0], truePrivateKeys[0]); }
@Test public void testGetKeyPairPublicKeyPrivateKeySamePublic() { for(int i = 0; i < trueKeyPairTypes.length; i++){ KeyPair pair = KeyGenUtils.getKeyPair(publicKeys[i], privateKeys[i]); assertEquals("KeyPairType: "+trueKeyPairTypes[i].name(), pair.getPublic(), publicKeys[i]); } }
@Test public void testGetKeyPairPublicKeyPrivateKeySamePrivate() { for(int i = 0; i < trueKeyPairTypes.length; i++){ KeyPair pair = KeyGenUtils.getKeyPair(publicKeys[i], privateKeys[i]); assertEquals("KeyPairType: "+trueKeyPairTypes[i].name(), pair.getPrivate(), privateKeys[i]); } }
@Test public void testGetKeyPairKeyPairTypeByteArrayByteArray() { for(int i = 0; i < trueKeyPairTypes.length; i++){ KeyPairType type = trueKeyPairTypes[i]; assertNotNull("KeyPairType: "+type.name(), KeyGenUtils.getKeyPair(type, truePublicKeys[i], truePrivateKeys[i])); } }
@Test public void testGetKeyPairPublicKeyPrivateKey() { for(int i = 0; i < trueKeyPairTypes.length; i++){ assertNotNull("KeyPairType: "+trueKeyPairTypes[i].name(), KeyGenUtils.getKeyPair(publicKeys[i], privateKeys[i])); } }