package aalg.crypto;

import java.util.Arrays;


/**
 * Simple non-secure hash function for testing purposes.
 * @author Ing. Petr Ležák
 */
public class TestHashFunction extends HashFunctionImpl {
	
	private final byte[] hash;
	private long messageLength;
	
	public TestHashFunction (final int hashLength) {
		this.hash = new byte[hashLength];
	}

	@Override
	public void initialize() {
		Arrays.fill(hash, (byte) 0);
		messageLength = 0;
	}

	@Override
	public void update(final byte[] data, final int offset, final int size) {
		for (int i = offset; i < offset + size; i++) {
			final int hashIndex = (int) (messageLength % hash.length);
			hash[hashIndex] ^= data[i];
			
			messageLength++;
		}
	}

	@Override
	public byte[] finish() {
		return Arrays.copyOf(hash, hash.length);
	}

	@Override
	public int getOutputByteCount() {
		return hash.length;
	}
	
	
}
