2003/02/12 06:21:04
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / DigestInfo.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.util.Enumeration;
4
5 import org.bouncycastle.asn1.*;
6 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
7
8 /**
9  * <pre>
10  * DigestInfo::=SEQUENCE{
11  *          digestAlgorithm  AlgorithmIdentifier,
12  *          digest OCTET STRING }
13  * </pre>
14  */
15 public class DigestInfo
16     implements DEREncodable
17 {
18     private byte[]                  digest;
19     private AlgorithmIdentifier     algId;
20
21     public static DigestInfo getInstance(
22         ASN1TaggedObject obj,
23         boolean          explicit)
24     {
25         return getInstance(ASN1Sequence.getInstance(obj, explicit));
26     }
27
28     public static DigestInfo getInstance(
29         Object  obj)
30     {
31         if (obj instanceof DigestInfo)
32         {
33             return (DigestInfo)obj;
34         }
35         else if (obj instanceof ASN1Sequence)
36         {
37             return new DigestInfo((ASN1Sequence)obj);
38         }
39
40         throw new IllegalArgumentException("unknown object in factory");
41     }
42
43     public DigestInfo(
44         AlgorithmIdentifier  algId,
45         byte[]               digest)
46     {
47         this.digest = digest;
48         this.algId = algId;
49     }
50
51     public DigestInfo(
52         ASN1Sequence  obj)
53     {
54         Enumeration             e = obj.getObjects();
55
56         algId = AlgorithmIdentifier.getInstance(e.nextElement());
57         digest = ((ASN1OctetString)e.nextElement()).getOctets();
58     }
59
60     public AlgorithmIdentifier getAlgorithmId()
61     {
62         return algId;
63     }
64
65     public byte[] getDigest()
66     {
67         return digest;
68     }
69
70     public DERObject getDERObject()
71     {
72         DEREncodableVector  v = new DEREncodableVector();
73
74         v.add(algId);
75         v.add(new DEROctetString(digest));
76
77         return new DERSequence(v);
78     }
79 }