package utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class HashUtils {
	
	public static String hashString(String input)  {
		
		try {
		
			byte[] bytesOfMessage = input.getBytes("UTF-8");	
			
			return DigestUtils.md5Hex(bytesOfMessage);
			
		}
		catch(Exception e) {
			
			return "";
		}
	}

	
	public static String hashFile(File file)  {
		
		try {
			
			MessageDigest md = MessageDigest.getInstance("MD5");
			
			FileInputStream fis = new FileInputStream(file);
			
			DigestInputStream dis = new DigestInputStream(fis, md);
			
			
			// hash the size
			dis.read(ByteBuffer.allocate(8).putLong(file.length()).array());
			
			
			// hash first 1000 bytes
			int i = 0;
			while (dis.read() != -1 && i < 1000) {
				i++;
			};
			
			// hash  5000 bytes starting in the middle or the remainder of the file if under 10000
			if(file.length() > 10000) {
				dis.skip(file.length() / 2);
				
				i = 0;
				while (dis.read() != -1 && i < 5000) {
					i++;
				};
			}
			else {
				while (dis.read() != -1) {
				};
			}
				
			dis.close();
			
			return new String(Hex.encodeHex(md.digest()));
			
			
		}
		catch(Exception e) {
			
			return "";
		}
	}

}

@@ -100,24 +100,64 @@
 DigestUtils;
+%0Aimport org.apache.commons.io.FileUtils;
 %0A%0Aimport jav
@@ -624,753 +624,50 @@
 %0A%09%09%09
-MessageDigest md = MessageDigest.getInstance(%22MD5%22);%0A%09%09%09%0A%09%09%09FileInputStream fis = new FileInputStream(file);%0A%09%09%09%0A%09%09%09DigestInputStream dis = new DigestInputStream(fis, md);%0A%09%09%09%0A%09%09%09%0A%09%09%09// hash the size%0A%09%09%09dis.read(ByteBuffer.allocate(8).putLong(file.length()).array());%0A%09%09%09%0A%09%09%09%0A%09%09%09// hash first 1000 bytes%0A%09%09%09int i = 0;%0A%09%09%09while (dis.read() != -1 && i %3C 1000) %7B%0A%09%09%09%09i++;%0A%09%09%09%7D;%0A%09%09%09%0A%09%09%09// hash  5000 bytes starting in the middle or the remainder of the file if under 10000%0A%09%09%09if(file.length() %3E 10000) %7B%0A%09%09%09%09dis.skip(file.length() / 2);%0A%09%09%09%09%0A%09%09%09%09i = 0;%0A%09%09%09%09while (dis.read() != -1 && i %3C 5000) %7B%0A%09%09%09%09%09i++;%0A%09%09%09%09%7D;%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09while (dis.read() != -1) %7B%0A%09%09%09%09%7D;%0A%09%09%09%7D%0A%09%09%09%09%0A%09%09%09dis.close();%0A%09%09%09%0A%09%09%09return new String(Hex.encodeHex(md.digest()));%0A%09%09%09
+return %22%22 + FileUtils.checksumCRC32(file);
 %0A%09%09%09
@@ -720,10 +720,11 @@
 %09%09%7D%0A%09%7D%0A%0A
-
 %7D%0A
+%0A

Use CRC32 as hash