PBEParameter.java revision ea26f188e84c7677b943d83cec49372793e81445
1ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrompackage org.bouncycastle.asn1.pkcs;
2ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
3ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport java.math.BigInteger;
4ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
5ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.ASN1Encodable;
6ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.ASN1EncodableVector;
7ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.ASN1OctetString;
8ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.ASN1Sequence;
9ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.DERInteger;
10ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.DERObject;
11ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.DEROctetString;
12ea26f188e84c7677b943d83cec49372793e81445Brian Carlstromimport org.bouncycastle.asn1.DERSequence;
13ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
14ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrompublic class PBEParameter
15ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    extends ASN1Encodable
16ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom{
17ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    DERInteger      iterations;
18ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    ASN1OctetString salt;
19ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
20ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public PBEParameter(
21ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        byte[]      salt,
22ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        int         iterations)
23ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
24ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        if (salt.length != 8)
25ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        {
26ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom            throw new IllegalArgumentException("salt length must be 8");
27ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        }
28ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        this.salt = new DEROctetString(salt);
29ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        this.iterations = new DERInteger(iterations);
30ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
31ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
32ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public PBEParameter(
33ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        ASN1Sequence  seq)
34ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
35ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        salt = (ASN1OctetString)seq.getObjectAt(0);
36ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        iterations = (DERInteger)seq.getObjectAt(1);
37ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
38ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
39ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public static PBEParameter getInstance(
40ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        Object  obj)
41ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
42ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        if (obj instanceof PBEParameter)
43ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        {
44ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom            return (PBEParameter)obj;
45ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        }
46ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        else if (obj instanceof ASN1Sequence)
47ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        {
48ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom            return new PBEParameter((ASN1Sequence)obj);
49ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        }
50ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
51ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
52ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
53ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
54ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public BigInteger getIterationCount()
55ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
56ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        return iterations.getValue();
57ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
58ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
59ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public byte[] getSalt()
60ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
61ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        return salt.getOctets();
62ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
63ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
64ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    public DERObject toASN1Object()
65ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    {
66ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        ASN1EncodableVector  v = new ASN1EncodableVector();
67ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
68ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        v.add(salt);
69ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        v.add(iterations);
70ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom
71ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom        return new DERSequence(v);
72ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom    }
73ea26f188e84c7677b943d83cec49372793e81445Brian Carlstrom}