001package org.apache.commons.ssl.org.bouncycastle.asn1.esf;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Choice;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1OctetString;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.DEROctetString;
008import org.apache.commons.ssl.org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
009import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
010
011/**
012 * <pre>
013 * OtherHash ::= CHOICE {
014 *    sha1Hash  OtherHashValue, -- This contains a SHA-1 hash
015 *   otherHash  OtherHashAlgAndValue
016 *  }
017 * </pre>
018 */
019public class OtherHash
020    extends ASN1Object
021    implements ASN1Choice
022{
023
024    private ASN1OctetString sha1Hash;
025    private OtherHashAlgAndValue otherHash;
026
027    public static OtherHash getInstance(Object obj)
028    {
029        if (obj instanceof OtherHash)
030        {
031            return (OtherHash)obj;
032        }
033        if (obj instanceof ASN1OctetString)
034        {
035            return new OtherHash((ASN1OctetString)obj);
036        }
037        return new OtherHash(OtherHashAlgAndValue.getInstance(obj));
038    }
039
040    private OtherHash(ASN1OctetString sha1Hash)
041    {
042        this.sha1Hash = sha1Hash;
043    }
044
045    public OtherHash(OtherHashAlgAndValue otherHash)
046    {
047        this.otherHash = otherHash;
048    }
049
050    public OtherHash(byte[] sha1Hash)
051    {
052        this.sha1Hash = new DEROctetString(sha1Hash);
053    }
054
055    public AlgorithmIdentifier getHashAlgorithm()
056    {
057        if (null == this.otherHash)
058        {
059            return new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1);
060        }
061        return this.otherHash.getHashAlgorithm();
062    }
063
064    public byte[] getHashValue()
065    {
066        if (null == this.otherHash)
067        {
068            return this.sha1Hash.getOctets();
069        }
070        return this.otherHash.getHashValue().getOctets();
071    }
072
073    public ASN1Primitive toASN1Primitive()
074    {
075        if (null == this.otherHash)
076        {
077            return this.sha1Hash;
078        }
079        return this.otherHash.toASN1Primitive();
080    }
081}