import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class ChecksumHandler {
public static byte[] createChecksum(String filename, HashTypes hash) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = null;
if(hash == HashTypes.MD5) complete = MessageDigest.getInstance("MD5");
else if(hash == HashTypes.SHA1) complete = MessageDigest.getInstance("SHA1");
else if(hash == HashTypes.SHA256) complete = MessageDigest.getInstance("SHA-256");
else if(hash == HashTypes.SHA512) complete = MessageDigest.getInstance("SHA-512");
else {
fis.close();
return null;
}
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
public static String getChecksum(String filename, HashTypes hash) throws Exception {
byte[] b = createChecksum(filename, hash);
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
}
Вот алгоритм хэширование, а как создать файл с расширением .check? на моем примере.