01: package org.bouncycastle.asn1.x509;
02:
03: import java.util.Enumeration;
04:
05: import org.bouncycastle.asn1.ASN1Encodable;
06: import org.bouncycastle.asn1.ASN1EncodableVector;
07: import org.bouncycastle.asn1.ASN1OctetString;
08: import org.bouncycastle.asn1.ASN1Sequence;
09: import org.bouncycastle.asn1.ASN1TaggedObject;
10: import org.bouncycastle.asn1.DERObject;
11: import org.bouncycastle.asn1.DEROctetString;
12: import org.bouncycastle.asn1.DERSequence;
13:
14: /**
15: * The DigestInfo object.
16: * <pre>
17: * DigestInfo::=SEQUENCE{
18: * digestAlgorithm AlgorithmIdentifier,
19: * digest OCTET STRING }
20: * </pre>
21: */
22: public class DigestInfo extends ASN1Encodable {
23: private byte[] digest;
24: private AlgorithmIdentifier algId;
25:
26: public static DigestInfo getInstance(ASN1TaggedObject obj,
27: boolean explicit) {
28: return getInstance(ASN1Sequence.getInstance(obj, explicit));
29: }
30:
31: public static DigestInfo getInstance(Object obj) {
32: if (obj instanceof DigestInfo) {
33: return (DigestInfo) obj;
34: } else if (obj instanceof ASN1Sequence) {
35: return new DigestInfo((ASN1Sequence) obj);
36: }
37:
38: throw new IllegalArgumentException("unknown object in factory");
39: }
40:
41: public DigestInfo(AlgorithmIdentifier algId, byte[] digest) {
42: this .digest = digest;
43: this .algId = algId;
44: }
45:
46: public DigestInfo(ASN1Sequence obj) {
47: Enumeration e = obj.getObjects();
48:
49: algId = AlgorithmIdentifier.getInstance(e.nextElement());
50: digest = ASN1OctetString.getInstance(e.nextElement())
51: .getOctets();
52: }
53:
54: public AlgorithmIdentifier getAlgorithmId() {
55: return algId;
56: }
57:
58: public byte[] getDigest() {
59: return digest;
60: }
61:
62: public DERObject toASN1Object() {
63: ASN1EncodableVector v = new ASN1EncodableVector();
64:
65: v.add(algId);
66: v.add(new DEROctetString(digest));
67:
68: return new DERSequence(v);
69: }
70: }
|