MacData.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.pkcs;
2
3import java.math.BigInteger;
4
5import org.bouncycastle.asn1.ASN1EncodableVector;
6import org.bouncycastle.asn1.ASN1Integer;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1OctetString;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.DEROctetString;
12import org.bouncycastle.asn1.DERSequence;
13import org.bouncycastle.asn1.x509.DigestInfo;
14
15public class MacData
16    extends ASN1Object
17{
18    private static final BigInteger ONE = BigInteger.valueOf(1);
19
20    DigestInfo                  digInfo;
21    byte[]                      salt;
22    BigInteger                  iterationCount;
23
24    public static MacData getInstance(
25        Object  obj)
26    {
27        if (obj instanceof MacData)
28        {
29            return (MacData)obj;
30        }
31        else if (obj != null)
32        {
33            return new MacData(ASN1Sequence.getInstance(obj));
34        }
35
36        return null;
37    }
38
39    private MacData(
40        ASN1Sequence seq)
41    {
42        this.digInfo = DigestInfo.getInstance(seq.getObjectAt(0));
43
44        this.salt = ((ASN1OctetString)seq.getObjectAt(1)).getOctets();
45
46        if (seq.size() == 3)
47        {
48            this.iterationCount = ((ASN1Integer)seq.getObjectAt(2)).getValue();
49        }
50        else
51        {
52            this.iterationCount = ONE;
53        }
54    }
55
56    public MacData(
57        DigestInfo  digInfo,
58        byte[]      salt,
59        int         iterationCount)
60    {
61        this.digInfo = digInfo;
62        this.salt = salt;
63        this.iterationCount = BigInteger.valueOf(iterationCount);
64    }
65
66    public DigestInfo getMac()
67    {
68        return digInfo;
69    }
70
71    public byte[] getSalt()
72    {
73        return salt;
74    }
75
76    public BigInteger getIterationCount()
77    {
78        return iterationCount;
79    }
80
81    /**
82     * <pre>
83     * MacData ::= SEQUENCE {
84     *     mac      DigestInfo,
85     *     macSalt  OCTET STRING,
86     *     iterations INTEGER DEFAULT 1
87     *     -- Note: The default is for historic reasons and its use is deprecated. A
88     *     -- higher value, like 1024 is recommended.
89     * </pre>
90     * @return the basic ASN1Primitive construction.
91     */
92    public ASN1Primitive toASN1Primitive()
93    {
94        ASN1EncodableVector  v = new ASN1EncodableVector();
95
96        v.add(digInfo);
97        v.add(new DEROctetString(salt));
98
99        if (!iterationCount.equals(ONE))
100        {
101            v.add(new ASN1Integer(iterationCount));
102        }
103
104        return new DERSequence(v);
105    }
106}
107