public EmailDestination(byte[] bytes) throws GeneralSecurityException { // find the crypto implementation for this key length for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int byteArrayLength = cryptoImpl.getByteArrayPublicKeyPairLength(); // length of an email destination that uses this CryptoImplementation if (bytes.length == byteArrayLength) this.cryptoImpl = cryptoImpl; } if (cryptoImpl == null) throw new InvalidKeyException("Not a valid Email Destination: " + Arrays.toString(bytes)); PublicKeyPair keyPair = cryptoImpl.createPublicKeyPair(bytes); publicEncryptionKey = keyPair.encryptionKey; publicSigningKey = keyPair.signingKey; }
/** * @param address A string containing a valid base64-encoded Email Destination * @throws GeneralSecurityException If <code>address</code> doesn't contain a valid Email Destination */ public EmailDestination(String address) throws GeneralSecurityException { String base64Key = extractBase64Dest(address); if (base64Key == null) throw new GeneralSecurityException("No Email Destination found in string: <" + address + ">"); // find the crypto implementation for this key length for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int base64Length = cryptoImpl.getBase64PublicKeyPairLength(); // length of an email destination that uses this CryptoImplementation if (base64Key.length() == base64Length) this.cryptoImpl = cryptoImpl; } if (cryptoImpl == null) throw new InvalidKeyException("Not a valid Email Destination: <" + base64Key + ">"); PublicKeyPair keyPair = cryptoImpl.createPublicKeyPair(base64Key); publicEncryptionKey = keyPair.encryptionKey; publicSigningKey = keyPair.signingKey; }
/** * Creates a <code>EmailIdentity</code> from a Base64-encoded string. * The format can be any format supported by one of the {@link CryptoImplementation}s; * the length of the string must match {@link CryptoImplementation#getBase64CompleteKeySetLength()}. * @param base64Key * @throws GeneralSecurityException */ public EmailIdentity(String base64Key) throws GeneralSecurityException { // find the crypto implementation for this key length for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int base64Length = cryptoImpl.getBase64CompleteKeySetLength(); // length of an email identity with this CryptoImplementation if (base64Key.length() == base64Length) { this.cryptoImpl = cryptoImpl; break; } } if (cryptoImpl == null) throw new InvalidKeyException("Not a valid Email Identity, no CryptoImplementation (out of " + CryptoFactory.getInstances().size() + ") matches length " + base64Key.length() + ": <" + base64Key + ">"); String base64PublicKeys = base64Key.substring(0, cryptoImpl.getBase64PublicKeyPairLength()); // the two private keys start after the two public keys PublicKeyPair publicKeys = cryptoImpl.createPublicKeyPair(base64PublicKeys); String base64PrivateKeys = base64Key.substring(cryptoImpl.getBase64PublicKeyPairLength()); // the two private keys start after the two public keys PrivateKeyPair privateKeys = cryptoImpl.createPrivateKeyPair(base64PrivateKeys); publicEncryptionKey = publicKeys.encryptionKey; privateEncryptionKey = privateKeys.encryptionKey; publicSigningKey = publicKeys.signingKey; privateSigningKey = privateKeys.signingKey; identityConfig = new IdentityConfigImpl(); }
private TestIdentity(String base64PublicKeyPair, String base64PrivateKeyPair, CryptoImplementation cryptoImpl) throws GeneralSecurityException { this.base64PublicKeyPair = base64PublicKeyPair; this.base64PrivateKeyPair = base64PrivateKeyPair; this.cryptoImpl = cryptoImpl; publicKeys = cryptoImpl.createPublicKeyPair(base64PublicKeyPair); privateKeys = cryptoImpl.createPrivateKeyPair(base64PrivateKeyPair); signingKeys = new KeyPair(publicKeys.signingKey, privateKeys.signingKey); encryptionKeys = new KeyPair(publicKeys.encryptionKey, privateKeys.encryptionKey); identity = new EmailIdentity(base64PublicKeyPair + base64PrivateKeyPair); identity.setPublicName(cryptoImpl.getName()); } }
@Test public void testToByteArray() throws GeneralSecurityException { for (TestIdentity testData: testIdentities) { CryptoImplementation cryptoImpl = testData.cryptoImpl; // test public key pair PublicKeyPair originalPublicKeyPair = cryptoImpl.createPublicKeyPair(testData.base64PublicKeyPair); byte[] originalPublic = cryptoImpl.toByteArray(originalPublicKeyPair); PublicKeyPair keyPairPublic = cryptoImpl.createPublicKeyPair(testData.base64PublicKeyPair); byte[] reencodedPublic = cryptoImpl.toByteArray(keyPairPublic); assertArrayEquals(originalPublic, reencodedPublic); assertEquals(cryptoImpl.getByteArrayPublicKeyPairLength(), reencodedPublic.length); // test private key pair PrivateKeyPair originalPrivateKeyPair = cryptoImpl.createPrivateKeyPair(testData.base64PrivateKeyPair); byte[] originalPrivate = cryptoImpl.toByteArray(originalPrivateKeyPair); PrivateKeyPair keyPairPrivate = cryptoImpl.createPrivateKeyPair(testData.base64PrivateKeyPair); byte[] reencodedPrivate = cryptoImpl.toByteArray(keyPairPrivate); assertArrayEquals(originalPrivate, reencodedPrivate); } }
@Test public void encodeDecodeBase64() throws GeneralSecurityException { for (TestIdentity testData: testIdentities) { CryptoImplementation cryptoImpl = testData.cryptoImpl; // test a public key pair PublicKeyPair publicKeyPair = cryptoImpl.createPublicKeyPair(testData.base64PublicKeyPair); String reencodedPublic = cryptoImpl.toBase64(publicKeyPair); assertEquals(testData.base64PublicKeyPair, reencodedPublic); assertEquals("Wrong public key length!", cryptoImpl.getBase64PublicKeyPairLength(), testData.base64PublicKeyPair.length()); // test a private key pair PrivateKeyPair privateKeyPair = cryptoImpl.createPrivateKeyPair(testData.base64PrivateKeyPair); String reencodedPrivate = cryptoImpl.toBase64(privateKeyPair); assertEquals(testData.base64PrivateKeyPair, reencodedPrivate); assertEquals("Wrong private key length!", cryptoImpl.getBase64CompleteKeySetLength()-cryptoImpl.getBase64PublicKeyPairLength(), testData.base64PrivateKeyPair.length()); } }