// 
// 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.AccessesPartialKey;
import com.google.errorprone.annotations.RestrictedApi;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.internal.OutputPrefixUtil;
import javax.annotation.Nullable;
import com.google.crypto.tink.util.Bytes;
import com.google.crypto.tink.util.SecretBytes;
import com.google.errorprone.annotations.Immutable;

@Immutable
public final class XChaCha20Poly1305Key extends AeadKey
{
    private final XChaCha20Poly1305Parameters parameters;
    private final SecretBytes keyBytes;
    private final Bytes outputPrefix;
    @Nullable
    private final Integer idRequirement;
    
    private XChaCha20Poly1305Key(final XChaCha20Poly1305Parameters parameters, final SecretBytes keyBytes, final Bytes outputPrefix, @Nullable final Integer idRequirement) {
        this.parameters = parameters;
        this.keyBytes = keyBytes;
        this.outputPrefix = outputPrefix;
        this.idRequirement = idRequirement;
    }
    
    private static Bytes getOutputPrefix(final XChaCha20Poly1305Parameters parameters, @Nullable final Integer idRequirement) {
        if (parameters.getVariant() == XChaCha20Poly1305Parameters.Variant.NO_PREFIX) {
            return OutputPrefixUtil.EMPTY_PREFIX;
        }
        if (parameters.getVariant() == XChaCha20Poly1305Parameters.Variant.CRUNCHY) {
            return OutputPrefixUtil.getLegacyOutputPrefix(idRequirement);
        }
        if (parameters.getVariant() == XChaCha20Poly1305Parameters.Variant.TINK) {
            return OutputPrefixUtil.getTinkOutputPrefix(idRequirement);
        }
        throw new IllegalStateException("Unknown Variant: " + parameters.getVariant());
    }
    
    @Override
    public Bytes getOutputPrefix() {
        return this.outputPrefix;
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    @AccessesPartialKey
    public static XChaCha20Poly1305Key create(final SecretBytes secretBytes) throws GeneralSecurityException {
        return create(XChaCha20Poly1305Parameters.Variant.NO_PREFIX, secretBytes, null);
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    public static XChaCha20Poly1305Key create(final XChaCha20Poly1305Parameters.Variant variant, final SecretBytes secretBytes, @Nullable final Integer idRequirement) throws GeneralSecurityException {
        if (variant != XChaCha20Poly1305Parameters.Variant.NO_PREFIX && idRequirement == null) {
            throw new GeneralSecurityException("For given Variant " + variant + " the value of idRequirement must be non-null");
        }
        if (variant == XChaCha20Poly1305Parameters.Variant.NO_PREFIX && idRequirement != null) {
            throw new GeneralSecurityException("For given Variant NO_PREFIX the value of idRequirement must be null");
        }
        if (secretBytes.size() != 32) {
            throw new GeneralSecurityException("XChaCha20Poly1305 key must be constructed with key of length 32 bytes, not " + secretBytes.size());
        }
        final XChaCha20Poly1305Parameters parameters = XChaCha20Poly1305Parameters.create(variant);
        return new XChaCha20Poly1305Key(parameters, secretBytes, getOutputPrefix(parameters, idRequirement), idRequirement);
    }
    
    @RestrictedApi(explanation = "Accessing parts of keys can produce unexpected incompatibilities, annotate the function with @AccessesPartialKey", link = "https://developers.google.com/tink/design/access_control#accessing_partial_keys", allowedOnPath = ".*Test\\.java", allowlistAnnotations = { AccessesPartialKey.class })
    public SecretBytes getKeyBytes() {
        return this.keyBytes;
    }
    
    @Override
    public XChaCha20Poly1305Parameters getParameters() {
        return this.parameters;
    }
    
    @Nullable
    @Override
    public Integer getIdRequirementOrNull() {
        return this.idRequirement;
    }
    
    @Override
    public boolean equalsKey(final Key o) {
        if (!(o instanceof XChaCha20Poly1305Key)) {
            return false;
        }
        final XChaCha20Poly1305Key that = (XChaCha20Poly1305Key)o;
        return that.parameters.equals(this.parameters) && that.keyBytes.equalsSecretBytes(this.keyBytes) && Objects.equals(that.idRequirement, this.idRequirement);
    }
}
