org.bouncycastle.cert.dane.DANEEntrySelectorFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-jdk14 Show documentation
Show all versions of bcpkix-jdk14 Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.4. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
package org.bouncycastle.cert.dane;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.operator.DigestCalculator;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;
/**
* Factory for creating selector objects to use with the DANECertificateStore.
*/
public class DANEEntrySelectorFactory
{
private final DigestCalculator digestCalculator;
/**
* Base constructor.
*
* At the moment you would call this as:
*
* new DANEEntrySelectorFactory(new TruncatingDigestCalculator(new SHA256DigestCalculator()));
*
* or some equivalent.
*
* @param digestCalculator a calculator for the message digest to filter email addresses currently truncated SHA-256 (originally SHA-224).
*/
public DANEEntrySelectorFactory(DigestCalculator digestCalculator)
{
this.digestCalculator = digestCalculator;
}
/**
* Create a selector for the passed in email address.
* @param emailAddress the emails address of interest.
* @throws DANEException in case of issue generating a matching name.
*/
public DANEEntrySelector createSelector(String emailAddress)
throws DANEException
{
final byte[] enc = Strings.toUTF8ByteArray(emailAddress.substring(0, emailAddress.indexOf('@')));
try
{
OutputStream cOut = digestCalculator.getOutputStream();
cOut.write(enc);
cOut.close();
}
catch (IOException e)
{
throw new DANEException("Unable to calculate digest string: " + e.getMessage(), e);
}
byte[] hash = digestCalculator.getDigest();
final String domainName = Strings.fromByteArray(Hex.encode(hash)) + "._smimecert." + emailAddress.substring(emailAddress.indexOf('@') + 1);
return new DANEEntrySelector(domainName);
}
}