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

package com.google.crypto.tink.mac;

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

public final class HmacParameters extends MacParameters
{
    private final int keySizeBytes;
    private final int tagSizeBytes;
    private final Variant variant;
    private final HashType hashType;
    
    private HmacParameters(final int keySizeBytes, final int tagSizeBytes, final Variant variant, final HashType hashType) {
        this.keySizeBytes = keySizeBytes;
        this.tagSizeBytes = tagSizeBytes;
        this.variant = variant;
        this.hashType = hashType;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public int getKeySizeBytes() {
        return this.keySizeBytes;
    }
    
    public int getCryptographicTagSizeBytes() {
        return this.tagSizeBytes;
    }
    
    public int getTotalTagSizeBytes() {
        if (this.variant == Variant.NO_PREFIX) {
            return this.getCryptographicTagSizeBytes();
        }
        if (this.variant == Variant.TINK) {
            return this.getCryptographicTagSizeBytes() + 5;
        }
        if (this.variant == Variant.CRUNCHY) {
            return this.getCryptographicTagSizeBytes() + 5;
        }
        if (this.variant == Variant.LEGACY) {
            return this.getCryptographicTagSizeBytes() + 5;
        }
        throw new IllegalStateException("Unknown variant");
    }
    
    public Variant getVariant() {
        return this.variant;
    }
    
    public HashType getHashType() {
        return this.hashType;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof HmacParameters)) {
            return false;
        }
        final HmacParameters that = (HmacParameters)o;
        return that.getKeySizeBytes() == this.getKeySizeBytes() && that.getTotalTagSizeBytes() == this.getTotalTagSizeBytes() && that.getVariant() == this.getVariant() && that.getHashType() == this.getHashType();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(HmacParameters.class, this.keySizeBytes, this.tagSizeBytes, this.variant, this.hashType);
    }
    
    @Override
    public boolean hasIdRequirement() {
        return this.variant != Variant.NO_PREFIX;
    }
    
    @Override
    public String toString() {
        return "HMAC Parameters (variant: " + this.variant + ", hashType: " + this.hashType + ", " + this.tagSizeBytes + "-byte tags, 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 LEGACY;
        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");
            LEGACY = new Variant("LEGACY");
            NO_PREFIX = new Variant("NO_PREFIX");
        }
    }
    
    @Immutable
    public static final class HashType
    {
        public static final HashType SHA1;
        public static final HashType SHA224;
        public static final HashType SHA256;
        public static final HashType SHA384;
        public static final HashType SHA512;
        private final String name;
        
        private HashType(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            SHA1 = new HashType("SHA1");
            SHA224 = new HashType("SHA224");
            SHA256 = new HashType("SHA256");
            SHA384 = new HashType("SHA384");
            SHA512 = new HashType("SHA512");
        }
    }
    
    public static final class Builder
    {
        @Nullable
        private Integer keySizeBytes;
        @Nullable
        private Integer tagSizeBytes;
        private HashType hashType;
        private Variant variant;
        
        private Builder() {
            this.keySizeBytes = null;
            this.tagSizeBytes = null;
            this.hashType = null;
            this.variant = Variant.NO_PREFIX;
        }
        
        @CanIgnoreReturnValue
        public Builder setKeySizeBytes(final int keySizeBytes) throws GeneralSecurityException {
            this.keySizeBytes = keySizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setTagSizeBytes(final int tagSizeBytes) throws GeneralSecurityException {
            this.tagSizeBytes = tagSizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setVariant(final Variant variant) {
            this.variant = variant;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setHashType(final HashType hashType) {
            this.hashType = hashType;
            return this;
        }
        
        private static void validateTagSizeBytes(final int tagSizeBytes, final HashType hashType) throws GeneralSecurityException {
            if (tagSizeBytes < 10) {
                throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; must be at least 10 bytes", tagSizeBytes));
            }
            if (hashType == HashType.SHA1) {
                if (tagSizeBytes > 20) {
                    throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; can be at most 20 bytes for SHA1", tagSizeBytes));
                }
            }
            else if (hashType == HashType.SHA224) {
                if (tagSizeBytes > 28) {
                    throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; can be at most 28 bytes for SHA224", tagSizeBytes));
                }
            }
            else if (hashType == HashType.SHA256) {
                if (tagSizeBytes > 32) {
                    throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; can be at most 32 bytes for SHA256", tagSizeBytes));
                }
            }
            else if (hashType == HashType.SHA384) {
                if (tagSizeBytes > 48) {
                    throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; can be at most 48 bytes for SHA384", tagSizeBytes));
                }
            }
            else {
                if (hashType != HashType.SHA512) {
                    throw new GeneralSecurityException("unknown hash type; must be SHA256, SHA384 or SHA512");
                }
                if (tagSizeBytes > 64) {
                    throw new GeneralSecurityException(String.format("Invalid tag size in bytes %d; can be at most 64 bytes for SHA512", tagSizeBytes));
                }
            }
        }
        
        public HmacParameters build() throws GeneralSecurityException {
            if (this.keySizeBytes == null) {
                throw new GeneralSecurityException("key size is not set");
            }
            if (this.tagSizeBytes == null) {
                throw new GeneralSecurityException("tag size is not set");
            }
            if (this.hashType == null) {
                throw new GeneralSecurityException("hash type is not set");
            }
            if (this.variant == null) {
                throw new GeneralSecurityException("variant is not set");
            }
            if (this.keySizeBytes < 16) {
                throw new InvalidAlgorithmParameterException(String.format("Invalid key size in bytes %d; must be at least 16 bytes", this.keySizeBytes));
            }
            validateTagSizeBytes(this.tagSizeBytes, this.hashType);
            return new HmacParameters(this.keySizeBytes, this.tagSizeBytes, this.variant, this.hashType, null);
        }
    }
}
