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

package com.google.crypto.tink.signature;

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

public final class RsaSsaPkcs1Parameters extends SignatureParameters
{
    public static final BigInteger F4;
    private final int modulusSizeBits;
    private final BigInteger publicExponent;
    private final Variant variant;
    private final HashType hashType;
    
    private RsaSsaPkcs1Parameters(final int modulusSizeBits, final BigInteger publicExponent, final Variant variant, final HashType hashType) {
        this.modulusSizeBits = modulusSizeBits;
        this.publicExponent = publicExponent;
        this.variant = variant;
        this.hashType = hashType;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public int getModulusSizeBits() {
        return this.modulusSizeBits;
    }
    
    public BigInteger getPublicExponent() {
        return this.publicExponent;
    }
    
    public Variant getVariant() {
        return this.variant;
    }
    
    public HashType getHashType() {
        return this.hashType;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof RsaSsaPkcs1Parameters)) {
            return false;
        }
        final RsaSsaPkcs1Parameters that = (RsaSsaPkcs1Parameters)o;
        return that.getModulusSizeBits() == this.getModulusSizeBits() && Objects.equals(that.getPublicExponent(), this.getPublicExponent()) && that.getVariant() == this.getVariant() && that.getHashType() == this.getHashType();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(RsaSsaPkcs1Parameters.class, this.modulusSizeBits, this.publicExponent, this.variant, this.hashType);
    }
    
    @Override
    public boolean hasIdRequirement() {
        return this.variant != Variant.NO_PREFIX;
    }
    
    @Override
    public String toString() {
        return "RSA SSA PKCS1 Parameters (variant: " + this.variant + ", hashType: " + this.hashType + ", publicExponent: " + this.publicExponent + ", and " + this.modulusSizeBits + "-bit modulus)";
    }
    
    static {
        F4 = BigInteger.valueOf(65537L);
    }
    
    @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 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 {
            SHA256 = new HashType("SHA256");
            SHA384 = new HashType("SHA384");
            SHA512 = new HashType("SHA512");
        }
    }
    
    public static final class Builder
    {
        @Nullable
        private Integer modulusSizeBits;
        @Nullable
        private BigInteger publicExponent;
        @Nullable
        private HashType hashType;
        private Variant variant;
        private static final BigInteger TWO;
        private static final BigInteger PUBLIC_EXPONENT_UPPER_BOUND;
        
        private Builder() {
            this.modulusSizeBits = null;
            this.publicExponent = RsaSsaPkcs1Parameters.F4;
            this.hashType = null;
            this.variant = Variant.NO_PREFIX;
        }
        
        @CanIgnoreReturnValue
        public Builder setModulusSizeBits(final int modulusSizeBits) {
            this.modulusSizeBits = modulusSizeBits;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setPublicExponent(final BigInteger e) {
            this.publicExponent = e;
            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 void validatePublicExponent(final BigInteger publicExponent) throws InvalidAlgorithmParameterException {
            final int c = publicExponent.compareTo(RsaSsaPkcs1Parameters.F4);
            if (c == 0) {
                return;
            }
            if (c < 0) {
                throw new InvalidAlgorithmParameterException("Public exponent must be at least 65537.");
            }
            if (publicExponent.mod(Builder.TWO).equals(BigInteger.ZERO)) {
                throw new InvalidAlgorithmParameterException("Invalid public exponent");
            }
            if (publicExponent.compareTo(Builder.PUBLIC_EXPONENT_UPPER_BOUND) > 0) {
                throw new InvalidAlgorithmParameterException("Public exponent cannot be larger than 2^256.");
            }
        }
        
        public RsaSsaPkcs1Parameters build() throws GeneralSecurityException {
            if (this.modulusSizeBits == null) {
                throw new GeneralSecurityException("key size is not set");
            }
            if (this.publicExponent == null) {
                throw new GeneralSecurityException("publicExponent 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.modulusSizeBits < 2048) {
                throw new InvalidAlgorithmParameterException(String.format("Invalid key size in bytes %d; must be at least 2048 bits", this.modulusSizeBits));
            }
            this.validatePublicExponent(this.publicExponent);
            return new RsaSsaPkcs1Parameters(this.modulusSizeBits, this.publicExponent, this.variant, this.hashType, null);
        }
        
        static {
            TWO = BigInteger.valueOf(2L);
            PUBLIC_EXPONENT_UPPER_BOUND = Builder.TWO.pow(256);
        }
    }
}
