View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import java.util.Enumeration;
22  
23  import org.apache.geronimo.util.asn1.ASN1Encodable;
24  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25  import org.apache.geronimo.util.asn1.ASN1OctetString;
26  import org.apache.geronimo.util.asn1.ASN1Sequence;
27  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
28  import org.apache.geronimo.util.asn1.DERObject;
29  import org.apache.geronimo.util.asn1.DEROctetString;
30  import org.apache.geronimo.util.asn1.DERSequence;
31  
32  /**
33   * The DigestInfo object.
34   * <pre>
35   * DigestInfo::=SEQUENCE{
36   *          digestAlgorithm  AlgorithmIdentifier,
37   *          digest OCTET STRING }
38   * </pre>
39   */
40  public class DigestInfo
41      extends ASN1Encodable
42  {
43      private byte[]                  digest;
44      private AlgorithmIdentifier     algId;
45  
46      public static DigestInfo getInstance(
47          ASN1TaggedObject obj,
48          boolean          explicit)
49      {
50          return getInstance(ASN1Sequence.getInstance(obj, explicit));
51      }
52  
53      public static DigestInfo getInstance(
54          Object  obj)
55      {
56          if (obj instanceof DigestInfo)
57          {
58              return (DigestInfo)obj;
59          }
60          else if (obj instanceof ASN1Sequence)
61          {
62              return new DigestInfo((ASN1Sequence)obj);
63          }
64  
65          throw new IllegalArgumentException("unknown object in factory");
66      }
67  
68      public DigestInfo(
69          AlgorithmIdentifier  algId,
70          byte[]               digest)
71      {
72          this.digest = digest;
73          this.algId = algId;
74      }
75  
76      public DigestInfo(
77          ASN1Sequence  obj)
78      {
79          Enumeration             e = obj.getObjects();
80  
81          algId = AlgorithmIdentifier.getInstance(e.nextElement());
82          digest = ((ASN1OctetString)e.nextElement()).getOctets();
83      }
84  
85      public AlgorithmIdentifier getAlgorithmId()
86      {
87          return algId;
88      }
89  
90      public byte[] getDigest()
91      {
92          return digest;
93      }
94  
95      public DERObject toASN1Object()
96      {
97          ASN1EncodableVector  v = new ASN1EncodableVector();
98  
99          v.add(algId);
100         v.add(new DEROctetString(digest));
101 
102         return new DERSequence(v);
103     }
104 }