public List<CryptoImplementation> getCryptoImplementations() { return CryptoFactory.getInstances(); }
for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int base64Length = cryptoImpl.getBase64PublicKeyPairLength(); // length of an email destination with this CryptoImplementation
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(); }