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

package com.google.crypto.tink.aead;

import com.google.crypto.tink.Parameters;
import java.util.Objects;
import com.google.crypto.tink.Key;
import com.google.crypto.tink.internal.OutputPrefixUtil;
import java.security.GeneralSecurityException;
import javax.annotation.Nullable;
import com.google.crypto.tink.util.Bytes;

public final class LegacyKmsEnvelopeAeadKey extends AeadKey
{
    private final LegacyKmsEnvelopeAeadParameters parameters;
    private final Bytes outputPrefix;
    @Nullable
    private final Integer idRequirement;
    
    private LegacyKmsEnvelopeAeadKey(final LegacyKmsEnvelopeAeadParameters parameters, final Bytes outputPrefix, @Nullable final Integer idRequirement) {
        this.parameters = parameters;
        this.outputPrefix = outputPrefix;
        this.idRequirement = idRequirement;
    }
    
    public static LegacyKmsEnvelopeAeadKey create(final LegacyKmsEnvelopeAeadParameters parameters, @Nullable final Integer idRequirement) throws GeneralSecurityException {
        Bytes outputPrefix;
        if (parameters.getVariant() == LegacyKmsEnvelopeAeadParameters.Variant.NO_PREFIX) {
            if (idRequirement != null) {
                throw new GeneralSecurityException("For given Variant NO_PREFIX the value of idRequirement must be null");
            }
            outputPrefix = OutputPrefixUtil.EMPTY_PREFIX;
        }
        else {
            if (parameters.getVariant() != LegacyKmsEnvelopeAeadParameters.Variant.TINK) {
                throw new GeneralSecurityException("Unknown Variant: " + parameters.getVariant());
            }
            if (idRequirement == null) {
                throw new GeneralSecurityException("For given Variant TINK the value of idRequirement must be non-null");
            }
            outputPrefix = OutputPrefixUtil.getTinkOutputPrefix(idRequirement);
        }
        return new LegacyKmsEnvelopeAeadKey(parameters, outputPrefix, idRequirement);
    }
    
    public static LegacyKmsEnvelopeAeadKey create(final LegacyKmsEnvelopeAeadParameters parameters) throws GeneralSecurityException {
        return create(parameters, null);
    }
    
    @Override
    public Bytes getOutputPrefix() {
        return this.outputPrefix;
    }
    
    @Override
    public LegacyKmsEnvelopeAeadParameters getParameters() {
        return this.parameters;
    }
    
    @Override
    public Integer getIdRequirementOrNull() {
        return this.idRequirement;
    }
    
    @Override
    public boolean equalsKey(final Key o) {
        if (!(o instanceof LegacyKmsEnvelopeAeadKey)) {
            return false;
        }
        final LegacyKmsEnvelopeAeadKey that = (LegacyKmsEnvelopeAeadKey)o;
        return that.parameters.equals(this.parameters) && Objects.equals(that.idRequirement, this.idRequirement);
    }
}
