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

package com.google.crypto.tink.signature;

import com.google.crypto.tink.signature.internal.LegacyFullSign;
import com.google.crypto.tink.internal.PrimitiveRegistry;
import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.internal.MonitoringClient;
import com.google.crypto.tink.internal.MonitoringUtil;
import com.google.crypto.tink.internal.MutableMonitoringRegistry;
import com.google.crypto.tink.internal.MonitoringAnnotations;
import com.google.crypto.tink.internal.KeysetHandleInterface;
import com.google.crypto.tink.internal.LegacyProtoKey;
import com.google.crypto.tink.internal.PrimitiveConstructor;
import com.google.crypto.tink.PublicKeySign;
import com.google.crypto.tink.internal.PrimitiveWrapper;

public class PublicKeySignWrapper implements PrimitiveWrapper<PublicKeySign, PublicKeySign>
{
    private static final PublicKeySignWrapper WRAPPER;
    private static final PrimitiveConstructor<LegacyProtoKey, PublicKeySign> LEGACY_PRIMITIVE_CONSTRUCTOR;
    
    PublicKeySignWrapper() {
    }
    
    @Override
    public PublicKeySign wrap(final KeysetHandleInterface keysetHandle, final MonitoringAnnotations annotations, final PrimitiveFactory<PublicKeySign> factory) throws GeneralSecurityException {
        MonitoringClient.Logger logger;
        if (!annotations.isEmpty()) {
            final MonitoringClient client = MutableMonitoringRegistry.globalInstance().getMonitoringClient();
            logger = client.createLogger(keysetHandle, annotations, "public_key_sign", "sign");
        }
        else {
            logger = MonitoringUtil.DO_NOTHING_LOGGER;
        }
        return new WrappedPublicKeySign(new PublicKeySignWithId(factory.create(keysetHandle.getPrimary()), keysetHandle.getPrimary().getId()), logger);
    }
    
    @Override
    public Class<PublicKeySign> getPrimitiveClass() {
        return PublicKeySign.class;
    }
    
    @Override
    public Class<PublicKeySign> getInputPrimitiveClass() {
        return PublicKeySign.class;
    }
    
    public static void register() throws GeneralSecurityException {
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)PublicKeySignWrapper.WRAPPER);
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveConstructor(PublicKeySignWrapper.LEGACY_PRIMITIVE_CONSTRUCTOR);
    }
    
    public static void registerToInternalPrimitiveRegistry(final PrimitiveRegistry.Builder primitiveRegistryBuilder) throws GeneralSecurityException {
        primitiveRegistryBuilder.registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)PublicKeySignWrapper.WRAPPER);
    }
    
    static {
        WRAPPER = new PublicKeySignWrapper();
        LEGACY_PRIMITIVE_CONSTRUCTOR = PrimitiveConstructor.create(LegacyFullSign::create, LegacyProtoKey.class, PublicKeySign.class);
    }
    
    private static class PublicKeySignWithId
    {
        public final PublicKeySign publicKeySign;
        public final int id;
        
        public PublicKeySignWithId(final PublicKeySign publicKeySign, final int id) {
            this.publicKeySign = publicKeySign;
            this.id = id;
        }
    }
    
    private static class WrappedPublicKeySign implements PublicKeySign
    {
        private final PublicKeySignWithId primary;
        private final MonitoringClient.Logger logger;
        
        public WrappedPublicKeySign(final PublicKeySignWithId primary, final MonitoringClient.Logger logger) {
            this.primary = primary;
            this.logger = logger;
        }
        
        @Override
        public byte[] sign(final byte[] data) throws GeneralSecurityException {
            try {
                final byte[] output = this.primary.publicKeySign.sign(data);
                this.logger.log(this.primary.id, data.length);
                return output;
            }
            catch (final GeneralSecurityException e) {
                this.logger.logFailure();
                throw e;
            }
        }
    }
}
