/** * Creates a random <code>EmailIdentity</code>. * @param cryptoImpl * @param vanityPrefix Base64 chars that the Email Destination should start with; <code>null</code> or an empty string for no vanity Destination. * @throws GeneralSecurityException * @throws IllegalDestinationParametersException if <code>cryptoImpl</code> and <code>vanityPrefix</code> aren't compatible */ public EmailIdentity(CryptoImplementation cryptoImpl, String vanityPrefix) throws GeneralSecurityException, IllegalDestinationParametersException { super(); this.cryptoImpl = cryptoImpl; if ("".equals(vanityPrefix)) vanityPrefix = null; if (vanityPrefix!=null && !cryptoImpl.getBase64InitialCharacters().contains(vanityPrefix.substring(0, 1))) { throw new IllegalDestinationParametersException(vanityPrefix.charAt(0), cryptoImpl.getBase64InitialCharacters()); } KeyPair encryptionKeys; do { encryptionKeys = cryptoImpl.generateEncryptionKeyPair(); } while (vanityPrefix!=null && !cryptoImpl.encryptionKeyToBase64(encryptionKeys.getPublic()).startsWith(vanityPrefix)); KeyPair signingKeys = cryptoImpl.generateSigningKeyPair(); publicEncryptionKey = encryptionKeys.getPublic(); privateEncryptionKey = encryptionKeys.getPrivate(); publicSigningKey = signingKeys.getPublic(); privateSigningKey = signingKeys.getPrivate(); identityConfig = new IdentityConfigImpl(); }
/** * 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(); }
public IdentityConfig wrap(Configuration configuration) { return new IdentityConfigImpl(properties, configuration); }