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