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

package com.google.crypto.tink;

import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import com.google.crypto.tink.proto.RegistryConfig;
import com.google.crypto.tink.proto.KeyTypeEntry;

public final class Config
{
    public static KeyTypeEntry getTinkKeyTypeEntry(final String catalogueName, final String primitiveName, final String keyProtoName, final int keyManagerVersion, final boolean newKeyAllowed) {
        return KeyTypeEntry.newBuilder().setPrimitiveName(primitiveName).setTypeUrl("type.googleapis.com/google.crypto.tink." + keyProtoName).setKeyManagerVersion(keyManagerVersion).setNewKeyAllowed(newKeyAllowed).setCatalogueName(catalogueName).build();
    }
    
    public static void register(final RegistryConfig config) throws GeneralSecurityException {
        for (final KeyTypeEntry entry : config.getEntryList()) {
            registerKeyType(entry);
        }
    }
    
    public static void registerKeyType(final KeyTypeEntry entry) throws GeneralSecurityException {
        validate(entry);
        if (entry.getCatalogueName().equals("TinkAead") || entry.getCatalogueName().equals("TinkMac") || entry.getCatalogueName().equals("TinkHybridDecrypt") || entry.getCatalogueName().equals("TinkHybridEncrypt") || entry.getCatalogueName().equals("TinkPublicKeySign") || entry.getCatalogueName().equals("TinkPublicKeyVerify") || entry.getCatalogueName().equals("TinkStreamingAead") || entry.getCatalogueName().equals("TinkDeterministicAead")) {
            return;
        }
        final Catalogue<?> catalogue = Registry.getCatalogue(entry.getCatalogueName());
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveWrapper(catalogue.getPrimitiveWrapper());
        final KeyManager<?> keyManager = catalogue.getKeyManager(entry.getTypeUrl(), entry.getPrimitiveName(), entry.getKeyManagerVersion());
        Registry.registerKeyManager(keyManager, entry.getNewKeyAllowed());
    }
    
    private static void validate(final KeyTypeEntry entry) throws GeneralSecurityException {
        if (entry.getTypeUrl().isEmpty()) {
            throw new GeneralSecurityException("Missing type_url.");
        }
        if (entry.getPrimitiveName().isEmpty()) {
            throw new GeneralSecurityException("Missing primitive_name.");
        }
        if (entry.getCatalogueName().isEmpty()) {
            throw new GeneralSecurityException("Missing catalogue_name.");
        }
    }
    
    private Config() {
    }
}
