package honk.tools.anidb;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import jonelo.jacksum.algorithm.Edonkey;

/**
 * Helper class for creating ed2k hashsums
 * @author honk
 */
public class Hasher {
    private Hasher() {
        
    }
    
    /**
     * Creates a ed2k hashsum for the file at the given location.
     * @param filename file
     * @return ed2k hashsum as string
     * @throws IOException if an error occurs reading the file
     */
    public static String hash(String filename) throws IOException {
        return hash(new File(filename));
    }
    
    
    /**
     * Creates a ed2k hashsum for the file at the given location.
     * @param file the file
     * @return ed2k hashsum as string
     * @throws IOException if an error occurs reading the file
     */
    public static String hash(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        Edonkey edonkey = new Edonkey();

        byte[] buffer = new byte[1048576*3];
        int read;
        while ((read = fis.read(buffer)) != -1) {
            edonkey.update(buffer, 0, read);
        }
        
        
        return edonkey.getHexValue();
    }
}
