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