KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > DigestInfo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import java.util.Enumeration JavaDoc;
21
22 import org.apache.geronimo.util.asn1.ASN1Encodable;
23 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
24 import org.apache.geronimo.util.asn1.ASN1OctetString;
25 import org.apache.geronimo.util.asn1.ASN1Sequence;
26 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
27 import org.apache.geronimo.util.asn1.DERObject;
28 import org.apache.geronimo.util.asn1.DEROctetString;
29 import org.apache.geronimo.util.asn1.DERSequence;
30
31 /**
32  * The DigestInfo object.
33  * <pre>
34  * DigestInfo::=SEQUENCE{
35  * digestAlgorithm AlgorithmIdentifier,
36  * digest OCTET STRING }
37  * </pre>
38  */

39 public class DigestInfo
40     extends ASN1Encodable
41 {
42     private byte[] digest;
43     private AlgorithmIdentifier algId;
44
45     public static DigestInfo getInstance(
46         ASN1TaggedObject obj,
47         boolean explicit)
48     {
49         return getInstance(ASN1Sequence.getInstance(obj, explicit));
50     }
51
52     public static DigestInfo getInstance(
53         Object JavaDoc obj)
54     {
55         if (obj instanceof DigestInfo)
56         {
57             return (DigestInfo)obj;
58         }
59         else if (obj instanceof ASN1Sequence)
60         {
61             return new DigestInfo((ASN1Sequence)obj);
62         }
63
64         throw new IllegalArgumentException JavaDoc("unknown object in factory");
65     }
66
67     public DigestInfo(
68         AlgorithmIdentifier algId,
69         byte[] digest)
70     {
71         this.digest = digest;
72         this.algId = algId;
73     }
74
75     public DigestInfo(
76         ASN1Sequence obj)
77     {
78         Enumeration JavaDoc e = obj.getObjects();
79
80         algId = AlgorithmIdentifier.getInstance(e.nextElement());
81         digest = ((ASN1OctetString)e.nextElement()).getOctets();
82     }
83
84     public AlgorithmIdentifier getAlgorithmId()
85     {
86         return algId;
87     }
88
89     public byte[] getDigest()
90     {
91         return digest;
92     }
93
94     public DERObject toASN1Object()
95     {
96         ASN1EncodableVector v = new ASN1EncodableVector();
97
98         v.add(algId);
99         v.add(new DEROctetString(digest));
100
101         return new DERSequence(v);
102     }
103 }
104
Popular Tags