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

package com.google.crypto.tink.aead;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import javax.annotation.Nullable;
import com.google.errorprone.annotations.Immutable;
import java.util.Objects;

public final class AesGcmParameters extends AeadParameters
{
    private final int keySizeBytes;
    private final int ivSizeBytes;
    private final int tagSizeBytes;
    private final Variant variant;
    
    private AesGcmParameters(final int keySizeBytes, final int ivSizeBytes, final int tagSizeBytes, final Variant variant) {
        this.keySizeBytes = keySizeBytes;
        this.ivSizeBytes = ivSizeBytes;
        this.tagSizeBytes = tagSizeBytes;
        this.variant = variant;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public int getKeySizeBytes() {
        return this.keySizeBytes;
    }
    
    public int getIvSizeBytes() {
        return this.ivSizeBytes;
    }
    
    public int getTagSizeBytes() {
        return this.tagSizeBytes;
    }
    
    public Variant getVariant() {
        return this.variant;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof AesGcmParameters)) {
            return false;
        }
        final AesGcmParameters that = (AesGcmParameters)o;
        return that.getKeySizeBytes() == this.getKeySizeBytes() && that.getIvSizeBytes() == this.getIvSizeBytes() && that.getTagSizeBytes() == this.getTagSizeBytes() && that.getVariant() == this.getVariant();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(AesGcmParameters.class, this.keySizeBytes, this.ivSizeBytes, this.tagSizeBytes, this.variant);
    }
    
    @Override
    public boolean hasIdRequirement() {
        return this.variant != Variant.NO_PREFIX;
    }
    
    @Override
    public String toString() {
        return "AesGcm Parameters (variant: " + this.variant + ", " + this.ivSizeBytes + "-byte IV, " + this.tagSizeBytes + "-byte tag, and " + this.keySizeBytes + "-byte key)";
    }
    
    @Immutable
    public static final class Variant
    {
        public static final Variant TINK;
        public static final Variant CRUNCHY;
        public static final Variant NO_PREFIX;
        private final String name;
        
        private Variant(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            TINK = new Variant("TINK");
            CRUNCHY = new Variant("CRUNCHY");
            NO_PREFIX = new Variant("NO_PREFIX");
        }
    }
    
    public static final class Builder
    {
        @Nullable
        private Integer keySizeBytes;
        @Nullable
        private Integer ivSizeBytes;
        @Nullable
        private Integer tagSizeBytes;
        private Variant variant;
        
        private Builder() {
            this.keySizeBytes = null;
            this.ivSizeBytes = null;
            this.tagSizeBytes = null;
            this.variant = Variant.NO_PREFIX;
        }
        
        @CanIgnoreReturnValue
        public Builder setKeySizeBytes(final int keySizeBytes) throws GeneralSecurityException {
            if (keySizeBytes != 16 && keySizeBytes != 24 && keySizeBytes != 32) {
                throw new InvalidAlgorithmParameterException(String.format("Invalid key size %d; only 16-byte, 24-byte and 32-byte AES keys are supported", keySizeBytes));
            }
            this.keySizeBytes = keySizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setIvSizeBytes(final int ivSizeBytes) throws GeneralSecurityException {
            if (ivSizeBytes <= 0) {
                throw new GeneralSecurityException(String.format("Invalid IV size in bytes %d; IV size must be positive", ivSizeBytes));
            }
            this.ivSizeBytes = ivSizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setTagSizeBytes(final int tagSizeBytes) throws GeneralSecurityException {
            if (tagSizeBytes < 12 || tagSizeBytes > 16) {
                throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; value must be between 12 and 16 bytes", tagSizeBytes));
            }
            this.tagSizeBytes = tagSizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setVariant(final Variant variant) {
            this.variant = variant;
            return this;
        }
        
        public AesGcmParameters build() throws GeneralSecurityException {
            if (this.keySizeBytes == null) {
                throw new GeneralSecurityException("Key size is not set");
            }
            if (this.variant == null) {
                throw new GeneralSecurityException("Variant is not set");
            }
            if (this.ivSizeBytes == null) {
                throw new GeneralSecurityException("IV size is not set");
            }
            if (this.tagSizeBytes == null) {
                throw new GeneralSecurityException("Tag size is not set");
            }
            return new AesGcmParameters(this.keySizeBytes, this.ivSizeBytes, this.tagSizeBytes, this.variant, null);
        }
    }
}
