문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top