R
R
random2014-10-25 09:40:09
Java
random, 2014-10-25 09:40:09

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.

  1. User to specify file URL;
  2. The program prompts the user for a save location. The file download starts;
  3. At the end of the download, the program should create the file "PRESENT_NAME.check" and place it next to the downloaded file: o The file must contain the checksum or hash of the downloaded file.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
protven, 2014-10-25
@protven

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. ..

R
random, 2014-10-25
@demon123

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;
    }
}

Here is the hashing algorithm, but how to create a file with the .check extension? on my example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question