Question

The scenario is the next:

I want to upload image to the server. But before uploading the file I have to send the SHA1 checksum of that file so the server could check if the file is already uploaded so I don't upload it again.

The problem is that for the same file I don't get the same SHA1 checksum in my app and on the server side.

Here is the code in my Android app:

public static String getSHA1FromFileContent(String filename)
        throws NoSuchAlgorithmException, IOException {

    final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");

    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    final byte[] buffer = new byte[1024];

    for (int read = 0; (read = is.read(buffer)) != -1;) {
        messageDigest.update(buffer, 0, read);
    }

    is.close();

    // Convert the byte to hex format
    Formatter formatter = new Formatter();

    for (final byte b : messageDigest.digest()) {
        formatter.format("%02x", b);
    }

    String res = formatter.toString();

    formatter.close();

    return res;
}

And here is the code on the server side:

def hashFile(f):

sha1 = hashlib.sha1()

if hasattr(f, 'multiple_chunks') and f.multiple_chunks():
    for c in f.chunks():
    sha1.update(c)
else:
    try:
    sha1.update(f.read())
    finally:
    f.close()

return sha1.hexdigest()

What is the problem and why do I get different SHA1 checksums?

Was it helpful?

Solution

Turned out there was some server side image editing before generating the sha1 sum that wasn't meant to be done in this scenario. They made changes on the server side and now this is working perfectly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top