Question

I have some trouble with my code, heres the method that is bothering me.

public static byte[] createChecksum(byte[] b){
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.update(b);
        byte[] checksum = md.digest();
        return checksum;
    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return null;
}

What happens is the statement md.digest() is executed it steps directly you return null. I dunno what goes wrong, is it a problem inside android?

edit: I wanna note that i am using java.security.MessageDigest and not android.security.MessageDigest

As i can see there is not exceptions from the digest method, however it reacts if there was an error inside the function, i send 186 bytes in a array to the method.

Was it helpful?

Solution

I changed the code a bit, but it seems to work, when i use digest outside the try catch, can't explain why, unless its the update change that fixed it.

public static byte[] createChecksum(byte[] b){
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");


    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }
    md.update(b,0,b.length);
    byte[] checksum = md.digest();
    return checksum;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top