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

package com.google.crypto.tink.signature.internal;

import com.google.crypto.tink.subtle.Bytes;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.KeyManager;
import com.google.crypto.tink.internal.ProtoKeySerialization;
import com.google.crypto.tink.internal.KeyManagerRegistry;
import com.google.crypto.tink.InsecureSecretKeyAccess;
import com.google.crypto.tink.internal.LegacyProtoKey;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.PublicKeySign;

@Immutable
public final class LegacyFullSign implements PublicKeySign
{
    private final PublicKeySign rawSigner;
    private final byte[] outputPrefix;
    private final byte[] messageSuffix;
    
    public static PublicKeySign create(final LegacyProtoKey key) throws GeneralSecurityException {
        final ProtoKeySerialization protoKeySerialization = key.getSerialization(InsecureSecretKeyAccess.get());
        final KeyManager<PublicKeySign> manager = KeyManagerRegistry.globalInstance().getKeyManager(protoKeySerialization.getTypeUrl(), PublicKeySign.class);
        final PublicKeySign rawSigner = manager.getPrimitive(protoKeySerialization.getValue());
        return new LegacyFullSign(rawSigner, LegacyFullVerify.getOutputPrefix(protoKeySerialization), LegacyFullVerify.getMessageSuffix(protoKeySerialization));
    }
    
    private LegacyFullSign(final PublicKeySign rawSigner, final byte[] outputPrefix, final byte[] messageSuffix) {
        this.rawSigner = rawSigner;
        this.outputPrefix = outputPrefix;
        this.messageSuffix = messageSuffix;
    }
    
    @Override
    public byte[] sign(final byte[] data) throws GeneralSecurityException {
        byte[] signature;
        if (this.messageSuffix.length == 0) {
            signature = this.rawSigner.sign(data);
        }
        else {
            signature = this.rawSigner.sign(Bytes.concat(new byte[][] { data, this.messageSuffix }));
        }
        if (this.outputPrefix.length == 0) {
            return signature;
        }
        return Bytes.concat(new byte[][] { this.outputPrefix, signature });
    }
}
