Home | History | Annotate | Download | only in pkcs
      1 package org.bouncycastle.asn1.pkcs;
      2 
      3 import java.math.BigInteger;
      4 
      5 import org.bouncycastle.asn1.ASN1Encodable;
      6 import org.bouncycastle.asn1.ASN1EncodableVector;
      7 import org.bouncycastle.asn1.ASN1OctetString;
      8 import org.bouncycastle.asn1.ASN1Sequence;
      9 import org.bouncycastle.asn1.DERInteger;
     10 import org.bouncycastle.asn1.DERObject;
     11 import org.bouncycastle.asn1.DEROctetString;
     12 import org.bouncycastle.asn1.DERSequence;
     13 import org.bouncycastle.asn1.x509.DigestInfo;
     14 
     15 public class MacData
     16     extends ASN1Encodable
     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 instanceof ASN1Sequence)
     32         {
     33             return new MacData((ASN1Sequence)obj);
     34         }
     35 
     36         throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
     37     }
     38 
     39     public 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 = ((DERInteger)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 DERObject construction.
     91      */
     92     public DERObject toASN1Object()
     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 DERInteger(iterationCount));
    102         }
    103 
    104         return new DERSequence(v);
    105     }
    106 }
    107