001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1.x509;
020    
021    import java.util.Enumeration;
022    
023    import org.apache.geronimo.util.asn1.ASN1Encodable;
024    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
025    import org.apache.geronimo.util.asn1.ASN1OctetString;
026    import org.apache.geronimo.util.asn1.ASN1Sequence;
027    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
028    import org.apache.geronimo.util.asn1.DERObject;
029    import org.apache.geronimo.util.asn1.DEROctetString;
030    import org.apache.geronimo.util.asn1.DERSequence;
031    
032    /**
033     * The DigestInfo object.
034     * <pre>
035     * DigestInfo::=SEQUENCE{
036     *          digestAlgorithm  AlgorithmIdentifier,
037     *          digest OCTET STRING }
038     * </pre>
039     */
040    public class DigestInfo
041        extends ASN1Encodable
042    {
043        private byte[]                  digest;
044        private AlgorithmIdentifier     algId;
045    
046        public static DigestInfo getInstance(
047            ASN1TaggedObject obj,
048            boolean          explicit)
049        {
050            return getInstance(ASN1Sequence.getInstance(obj, explicit));
051        }
052    
053        public static DigestInfo getInstance(
054            Object  obj)
055        {
056            if (obj instanceof DigestInfo)
057            {
058                return (DigestInfo)obj;
059            }
060            else if (obj instanceof ASN1Sequence)
061            {
062                return new DigestInfo((ASN1Sequence)obj);
063            }
064    
065            throw new IllegalArgumentException("unknown object in factory");
066        }
067    
068        public DigestInfo(
069            AlgorithmIdentifier  algId,
070            byte[]               digest)
071        {
072            this.digest = digest;
073            this.algId = algId;
074        }
075    
076        public DigestInfo(
077            ASN1Sequence  obj)
078        {
079            Enumeration             e = obj.getObjects();
080    
081            algId = AlgorithmIdentifier.getInstance(e.nextElement());
082            digest = ((ASN1OctetString)e.nextElement()).getOctets();
083        }
084    
085        public AlgorithmIdentifier getAlgorithmId()
086        {
087            return algId;
088        }
089    
090        public byte[] getDigest()
091        {
092            return digest;
093        }
094    
095        public DERObject toASN1Object()
096        {
097            ASN1EncodableVector  v = new ASN1EncodableVector();
098    
099            v.add(algId);
100            v.add(new DEROctetString(digest));
101    
102            return new DERSequence(v);
103        }
104    }