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

package com.google.crypto.tink.subtle.prf;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.io.InputStream;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.prf.Prf;

@Immutable
public class PrfImpl implements Prf
{
    private final StreamingPrf prfStreamer;
    
    private PrfImpl(final StreamingPrf prfStreamer) {
        this.prfStreamer = prfStreamer;
    }
    
    public static PrfImpl wrap(final StreamingPrf prfStreamer) {
        return new PrfImpl(prfStreamer);
    }
    
    private static byte[] readBytesFromStream(final InputStream stream, final int outputLength) throws GeneralSecurityException {
        try {
            final byte[] output = new byte[outputLength];
            int bytesRead;
            for (int offset = 0; offset < outputLength; offset += bytesRead) {
                bytesRead = stream.read(output, offset, outputLength - offset);
                if (bytesRead <= 0) {
                    throw new GeneralSecurityException("Provided StreamingPrf terminated before providing requested number of bytes.");
                }
            }
            return output;
        }
        catch (final IOException exception) {
            throw new GeneralSecurityException(exception);
        }
    }
    
    @Override
    public byte[] compute(final byte[] input, final int outputLength) throws GeneralSecurityException {
        if (input == null) {
            throw new GeneralSecurityException("Invalid input provided.");
        }
        if (outputLength <= 0) {
            throw new GeneralSecurityException("Invalid outputLength specified.");
        }
        final InputStream prfStream = this.prfStreamer.computePrf(input);
        return readBytesFromStream(prfStream, outputLength);
    }
}
