// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.asn1.bc;

import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1BitString;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.ASN1Object;

public class ExternalValue extends ASN1Object
{
    private final GeneralNames location;
    private final AlgorithmIdentifier hashAlg;
    private final byte[] hashValue;
    
    public ExternalValue(final GeneralName generalName, final AlgorithmIdentifier hashAlg, final byte[] array) {
        this.location = new GeneralNames(generalName);
        this.hashAlg = hashAlg;
        this.hashValue = Arrays.clone(array);
    }
    
    private ExternalValue(final ASN1Sequence asn1Sequence) {
        if (asn1Sequence.size() == 3) {
            this.location = GeneralNames.getInstance(asn1Sequence.getObjectAt(0));
            this.hashAlg = AlgorithmIdentifier.getInstance(asn1Sequence.getObjectAt(1));
            if (asn1Sequence.getObjectAt(2) instanceof ASN1BitString) {
                this.hashValue = ASN1BitString.getInstance(asn1Sequence.getObjectAt(2)).getOctets();
            }
            else {
                this.hashValue = ASN1OctetString.getInstance(asn1Sequence.getObjectAt(2)).getOctets();
            }
            return;
        }
        throw new IllegalArgumentException("unknown sequence");
    }
    
    public static ExternalValue getInstance(final Object o) {
        if (o instanceof ExternalValue) {
            return (ExternalValue)o;
        }
        if (o != null) {
            return new ExternalValue(ASN1Sequence.getInstance(o));
        }
        return null;
    }
    
    public GeneralName getLocation() {
        return this.location.getNames()[0];
    }
    
    public GeneralName[] getLocations() {
        return this.location.getNames();
    }
    
    public AlgorithmIdentifier getHashAlg() {
        return this.hashAlg;
    }
    
    public byte[] getHashValue() {
        return Arrays.clone(this.hashValue);
    }
    
    @Deprecated
    public ASN1BitString getHashVal() {
        return new DERBitString(this.hashValue);
    }
    
    @Override
    public ASN1Primitive toASN1Primitive() {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        asn1EncodableVector.add(this.location);
        asn1EncodableVector.add(this.hashAlg);
        asn1EncodableVector.add(new DEROctetString(this.hashValue));
        return new DERSequence(asn1EncodableVector);
    }
}
