01: package org.bouncycastle.asn1.pkcs;
02:
03: import java.math.BigInteger;
04: import java.util.Enumeration;
05:
06: import org.bouncycastle.asn1.ASN1Encodable;
07: import org.bouncycastle.asn1.ASN1EncodableVector;
08: import org.bouncycastle.asn1.ASN1OctetString;
09: import org.bouncycastle.asn1.ASN1Sequence;
10: import org.bouncycastle.asn1.DERInteger;
11: import org.bouncycastle.asn1.DERObject;
12: import org.bouncycastle.asn1.DEROctetString;
13: import org.bouncycastle.asn1.DERSequence;
14:
15: public class PBKDF2Params extends ASN1Encodable {
16: ASN1OctetString octStr;
17: DERInteger iterationCount;
18: DERInteger keyLength;
19:
20: public static PBKDF2Params getInstance(Object obj) {
21: if (obj instanceof PBKDF2Params) {
22: return (PBKDF2Params) obj;
23: }
24:
25: if (obj instanceof ASN1Sequence) {
26: return new PBKDF2Params((ASN1Sequence) obj);
27: }
28:
29: throw new IllegalArgumentException("unknown object in factory");
30: }
31:
32: public PBKDF2Params(byte[] salt, int iterationCount) {
33: this .octStr = new DEROctetString(salt);
34: this .iterationCount = new DERInteger(iterationCount);
35: }
36:
37: public PBKDF2Params(ASN1Sequence seq) {
38: Enumeration e = seq.getObjects();
39:
40: octStr = (ASN1OctetString) e.nextElement();
41: iterationCount = (DERInteger) e.nextElement();
42:
43: if (e.hasMoreElements()) {
44: keyLength = (DERInteger) e.nextElement();
45: } else {
46: keyLength = null;
47: }
48: }
49:
50: public byte[] getSalt() {
51: return octStr.getOctets();
52: }
53:
54: public BigInteger getIterationCount() {
55: return iterationCount.getValue();
56: }
57:
58: public BigInteger getKeyLength() {
59: if (keyLength != null) {
60: return keyLength.getValue();
61: }
62:
63: return null;
64: }
65:
66: public DERObject toASN1Object() {
67: ASN1EncodableVector v = new ASN1EncodableVector();
68:
69: v.add(octStr);
70: v.add(iterationCount);
71:
72: if (keyLength != null) {
73: v.add(keyLength);
74: }
75:
76: return new DERSequence(v);
77: }
78: }
|