Answer the question
In order to leave comments, you need to log in
How to create a file that contains a checksum?
There are 3 points, I implemented only 1 and 2. Question: How to implement point 3, if possible, then with an example.
Answer the question
In order to leave comments, you need to log in
What is the hashing algorithm?
Here is an option for md5
stackoverflow.com/questions/304268/getting-a-files...
javarevisited.blogspot.ru/2013/06/how-to-generate-...
In general, there is an opinion that you mixed up the toster and the Google window. ..
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question