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

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

import com.google.crypto.tink.util.Bytes;
import com.google.crypto.tink.mac.ChunkedMacVerification;
import com.google.crypto.tink.mac.ChunkedMacComputation;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.mac.HmacKey;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.mac.ChunkedMac;

@Immutable
public final class ChunkedHmacImpl implements ChunkedMac
{
    private static final TinkFipsUtil.AlgorithmFipsCompatibility FIPS;
    private final HmacKey key;
    
    public ChunkedHmacImpl(final HmacKey key) throws GeneralSecurityException {
        if (!ChunkedHmacImpl.FIPS.isCompatible()) {
            throw new GeneralSecurityException("Can not use HMAC in FIPS-mode, as BoringCrypto module is not available.");
        }
        this.key = key;
    }
    
    @Override
    public ChunkedMacComputation createComputation() throws GeneralSecurityException {
        return new ChunkedHmacComputation(this.key);
    }
    
    @Override
    public ChunkedMacVerification createVerification(final byte[] tag) throws GeneralSecurityException {
        if (tag.length < this.key.getOutputPrefix().size()) {
            throw new GeneralSecurityException("Tag too short");
        }
        if (!this.key.getOutputPrefix().equals(Bytes.copyFrom(tag, 0, this.key.getOutputPrefix().size()))) {
            throw new GeneralSecurityException("Wrong tag prefix");
        }
        return ChunkedMacVerificationFromComputation.create(new ChunkedHmacComputation(this.key), tag);
    }
    
    static {
        FIPS = TinkFipsUtil.AlgorithmFipsCompatibility.ALGORITHM_REQUIRES_BORINGCRYPTO;
    }
}
