Home | History | Annotate | Download | only in pkcs
      1 package org.bouncycastle.asn1.pkcs;
      2 
      3 import java.math.BigInteger;
      4 import java.util.Enumeration;
      5 
      6 import org.bouncycastle.asn1.ASN1Encodable;
      7 import org.bouncycastle.asn1.ASN1EncodableVector;
      8 import org.bouncycastle.asn1.ASN1OctetString;
      9 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
     16     extends ASN1Encodable
     17 {
     18     ASN1OctetString     octStr;
     19     DERInteger          iterationCount;
     20     DERInteger          keyLength;
     21 
     22     public static PBKDF2Params getInstance(
     23         Object  obj)
     24     {
     25         if (obj instanceof PBKDF2Params)
     26         {
     27             return (PBKDF2Params)obj;
     28         }
     29 
     30         if (obj instanceof ASN1Sequence)
     31         {
     32             return new PBKDF2Params((ASN1Sequence)obj);
     33         }
     34 
     35         throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
     36     }
     37 
     38     public PBKDF2Params(
     39         byte[]  salt,
     40         int     iterationCount)
     41     {
     42         this.octStr = new DEROctetString(salt);
     43         this.iterationCount = new DERInteger(iterationCount);
     44     }
     45 
     46     public PBKDF2Params(
     47         ASN1Sequence  seq)
     48     {
     49         Enumeration e = seq.getObjects();
     50 
     51         octStr = (ASN1OctetString)e.nextElement();
     52         iterationCount = (DERInteger)e.nextElement();
     53 
     54         if (e.hasMoreElements())
     55         {
     56             keyLength = (DERInteger)e.nextElement();
     57         }
     58         else
     59         {
     60             keyLength = null;
     61         }
     62     }
     63 
     64     public byte[] getSalt()
     65     {
     66         return octStr.getOctets();
     67     }
     68 
     69     public BigInteger getIterationCount()
     70     {
     71         return iterationCount.getValue();
     72     }
     73 
     74     public BigInteger getKeyLength()
     75     {
     76         if (keyLength != null)
     77         {
     78             return keyLength.getValue();
     79         }
     80 
     81         return null;
     82     }
     83 
     84     public DERObject toASN1Object()
     85     {
     86         ASN1EncodableVector  v = new ASN1EncodableVector();
     87 
     88         v.add(octStr);
     89         v.add(iterationCount);
     90 
     91         if (keyLength != null)
     92         {
     93             v.add(keyLength);
     94         }
     95 
     96         return new DERSequence(v);
     97     }
     98 }
     99