package org.yi.happy.archive;

import java.security.MessageDigest;

import org.yi.happy.archive.key.UnknownAlgorithmException;

/**
 * Utility methods for blocks of data
 */
public class BlockUtil {
    /**
     * expand some data into an encryption key using the same algorithm as
     * freenet does for key expansion. the first size bytes of ( digest('\0' +
     * data) + digest("\0\0" + data) + ... ).
     * 
     * @param digest
     *            the digest to use for the expansion
     * @param data
     *            the seed data
     * @param size
     *            the target key size
     * @return the key bytes
     */
    public static byte[] expandKey(MessageDigest digest, byte[] data, int size) {
	byte[] out = new byte[size];
	int o = 0;
	int n = 1;
	while (o < out.length) {
	    for (int j = 0; j < n; j++) {
		digest.update((byte) 0);
	    }
	    digest.update(data);
	    byte[] bit = digest.digest();
	    for (int j = 0; o < out.length && j < bit.length; j++) {
		out[o++] = bit[j];
	    }
	    n++;
	}
	return out;
    }

    public static byte[] hashBytes(String digest, byte[] bytes)
	    throws UnknownDigestException {
	try {
	    MessageDigest d = DigestFactory.create(digest);
	    d.update(bytes);
	    return d.digest();
	} catch (UnknownAlgorithmException e) {
	    throw new UnknownDigestException(digest, e);
	}
    }
}

@@ -65,68 +65,8 @@
 t;%0A%0A
-import org.yi.happy.archive.key.UnknownAlgorithmException;%0A%0A
 /**%0A
@@ -985,315 +985,7 @@
    %7D
-%0A%0A    public static byte%5B%5D hashBytes(String digest, byte%5B%5D bytes)%0A%09    throws UnknownDigestException %7B%0A%09try %7B%0A%09    MessageDigest d = DigestFactory.create(digest);%0A%09    d.update(bytes);%0A%09    return d.digest();%0A%09%7D catch (UnknownAlgorithmException e) %7B%0A%09    throw new UnknownDigestException(digest, e);%0A%09%7D%0A    %7D
 %0A%7D%0A

drop unused BlockUtil.hashBytes(...).