Question

Java: Any idea why MessageDigest.getInstance("MD5") would return null? This was working just fine until recently.

Was it helpful?

Solution

I doubt about the question

MessageDigest.getInstance(“MD5”) returning null

If the class MessageDigest you meant is java.security.MessageDigest, that method will NOT return null, even if you gave a not existing algorithm as parameter.

The code from jdk1.7

public static MessageDigest getInstance(String algorithm)
    throws NoSuchAlgorithmException {
        try {
            Object[] objs = Security.getImpl(algorithm, "MessageDigest",
                                             (String)null);
            if (objs[0] instanceof MessageDigest) {
                MessageDigest md = (MessageDigest)objs[0];
                md.provider = (Provider)objs[1];
                return md;
            } else {
                MessageDigest delegate =
                    new Delegate((MessageDigestSpi)objs[0], algorithm);
                delegate.provider = (Provider)objs[1];
                return delegate;
            }
        } catch(NoSuchProviderException e) {
            throw new NoSuchAlgorithmException(algorithm + " not found");
        }
    }

if it returned null, then md or delegate must be null, then NPE happened in this class.

so check and debug your codes step by step, find out which object is null.

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