org.spongycastle.asn1.pkcs.MacData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scprov-jdk15 Show documentation
Show all versions of scprov-jdk15 Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for Android.
Android ships with a stripped-down version of Bouncy Castle - this causes classloader collisions if you try to add
an alternative (updated/complete) Bouncy Castle jar.
This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5.
package org.spongycastle.asn1.pkcs;
import java.math.BigInteger;
import org.spongycastle.asn1.ASN1Encodable;
import org.spongycastle.asn1.ASN1EncodableVector;
import org.spongycastle.asn1.ASN1OctetString;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.DERInteger;
import org.spongycastle.asn1.DERObject;
import org.spongycastle.asn1.DEROctetString;
import org.spongycastle.asn1.DERSequence;
import org.spongycastle.asn1.x509.DigestInfo;
public class MacData
extends ASN1Encodable
{
private static final BigInteger ONE = BigInteger.valueOf(1);
DigestInfo digInfo;
byte[] salt;
BigInteger iterationCount;
public static MacData getInstance(
Object obj)
{
if (obj instanceof MacData)
{
return (MacData)obj;
}
else if (obj instanceof ASN1Sequence)
{
return new MacData((ASN1Sequence)obj);
}
throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
}
public MacData(
ASN1Sequence seq)
{
this.digInfo = DigestInfo.getInstance(seq.getObjectAt(0));
this.salt = ((ASN1OctetString)seq.getObjectAt(1)).getOctets();
if (seq.size() == 3)
{
this.iterationCount = ((DERInteger)seq.getObjectAt(2)).getValue();
}
else
{
this.iterationCount = ONE;
}
}
public MacData(
DigestInfo digInfo,
byte[] salt,
int iterationCount)
{
this.digInfo = digInfo;
this.salt = salt;
this.iterationCount = BigInteger.valueOf(iterationCount);
}
public DigestInfo getMac()
{
return digInfo;
}
public byte[] getSalt()
{
return salt;
}
public BigInteger getIterationCount()
{
return iterationCount;
}
/**
*
* MacData ::= SEQUENCE {
* mac DigestInfo,
* macSalt OCTET STRING,
* iterations INTEGER DEFAULT 1
* -- Note: The default is for historic reasons and its use is deprecated. A
* -- higher value, like 1024 is recommended.
*
* @return the basic DERObject construction.
*/
public DERObject toASN1Object()
{
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(digInfo);
v.add(new DEROctetString(salt));
if (!iterationCount.equals(ONE))
{
v.add(new DERInteger(iterationCount));
}
return new DERSequence(v);
}
}