org.bouncycastle.pkcs.bc.BcPKCS12MacCalculatorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-jdk15to18 Show documentation
Show all versions of bcpkix-jdk15to18 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.5 to JDK 1.8. 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.pkcs.bc;
import java.security.SecureRandom;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.operator.MacCalculator;
import org.bouncycastle.pkcs.PKCS12MacCalculatorBuilder;
public class BcPKCS12MacCalculatorBuilder
implements PKCS12MacCalculatorBuilder
{
private ExtendedDigest digest;
private AlgorithmIdentifier algorithmIdentifier;
private SecureRandom random;
private int saltLength;
private int iterationCount = 1024;
public BcPKCS12MacCalculatorBuilder()
{
this(new SHA1Digest(), new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE));
}
public BcPKCS12MacCalculatorBuilder(ExtendedDigest digest, AlgorithmIdentifier algorithmIdentifier)
{
this.digest = digest;
this.algorithmIdentifier = algorithmIdentifier;
this.saltLength = digest.getDigestSize();
}
public BcPKCS12MacCalculatorBuilder setIterationCount(int iterationCount)
{
this.iterationCount = iterationCount;
return this;
}
public AlgorithmIdentifier getDigestAlgorithmIdentifier()
{
return algorithmIdentifier;
}
public MacCalculator build(final char[] password)
{
if (random == null)
{
random = new SecureRandom();
}
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
return PKCS12PBEUtils.createMacCalculator(algorithmIdentifier.getAlgorithm(), digest, new PKCS12PBEParams(salt, iterationCount), password);
}
}