org.spongycastle.pkcs.jcajce.JcePKCS12MacCalculatorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkix Show documentation
Show all versions of pkix Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
package org.spongycastle.pkcs.jcajce;
import java.io.OutputStream;
import java.security.Provider;
import java.security.SecureRandom;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.PBEParameterSpec;
import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.DERNull;
import org.spongycastle.asn1.oiw.OIWObjectIdentifiers;
import org.spongycastle.asn1.pkcs.PKCS12PBEParams;
import org.spongycastle.asn1.x509.AlgorithmIdentifier;
import org.spongycastle.jcajce.PKCS12Key;
import org.spongycastle.jcajce.io.MacOutputStream;
import org.spongycastle.jcajce.util.DefaultJcaJceHelper;
import org.spongycastle.jcajce.util.JcaJceHelper;
import org.spongycastle.jcajce.util.NamedJcaJceHelper;
import org.spongycastle.jcajce.util.ProviderJcaJceHelper;
import org.spongycastle.operator.GenericKey;
import org.spongycastle.operator.MacCalculator;
import org.spongycastle.operator.OperatorCreationException;
import org.spongycastle.pkcs.PKCS12MacCalculatorBuilder;
public class JcePKCS12MacCalculatorBuilder
implements PKCS12MacCalculatorBuilder
{
private JcaJceHelper helper = new DefaultJcaJceHelper();
private ASN1ObjectIdentifier algorithm;
private SecureRandom random;
private int saltLength;
private int iterationCount = 1024;
public JcePKCS12MacCalculatorBuilder()
{
this(OIWObjectIdentifiers.idSHA1);
}
public JcePKCS12MacCalculatorBuilder(ASN1ObjectIdentifier hashAlgorithm)
{
this.algorithm = hashAlgorithm;
}
public JcePKCS12MacCalculatorBuilder setProvider(Provider provider)
{
this.helper = new ProviderJcaJceHelper(provider);
return this;
}
public JcePKCS12MacCalculatorBuilder setProvider(String providerName)
{
this.helper = new NamedJcaJceHelper(providerName);
return this;
}
public JcePKCS12MacCalculatorBuilder setIterationCount(int iterationCount)
{
this.iterationCount = iterationCount;
return this;
}
public AlgorithmIdentifier getDigestAlgorithmIdentifier()
{
return new AlgorithmIdentifier(algorithm, DERNull.INSTANCE);
}
public MacCalculator build(final char[] password)
throws OperatorCreationException
{
if (random == null)
{
random = new SecureRandom();
}
try
{
final Mac mac = helper.createMac(algorithm.getId());
saltLength = mac.getMacLength();
final byte[] salt = new byte[saltLength];
random.nextBytes(salt);
PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
final SecretKey key = new PKCS12Key(password);
mac.init(key, defParams);
return new MacCalculator()
{
public AlgorithmIdentifier getAlgorithmIdentifier()
{
return new AlgorithmIdentifier(algorithm, new PKCS12PBEParams(salt, iterationCount));
}
public OutputStream getOutputStream()
{
return new MacOutputStream(mac);
}
public byte[] getMac()
{
return mac.doFinal();
}
public GenericKey getKey()
{
return new GenericKey(getAlgorithmIdentifier(), key.getEncoded());
}
};
}
catch (Exception e)
{
throw new OperatorCreationException("unable to create MAC calculator: " + e.getMessage(), e);
}
}
}