Tencent/matrix · error · RuntimeException

Initialize MD5 failed.

Error message

Initialize MD5 failed.

What it means

MatrixUtil's MD5 ThreadLocal wraps MessageDigest.getInstance("MD5") and throws a RuntimeException when the JCA provider reports NoSuchAlgorithmException. MD5 is mandated by every standard Java/Android platform, so this only fires on broken or non-compliant crypto provider environments.

Solutions

  1. Restore/verify the default security Providers (e.g. Security.addProvider(new BouncyCastleProvider()))
  2. Check java.security config files and registered providers for missing MD5 support
  3. Catch the RuntimeException and use an alternative hashing path or fail gracefully
  4. Log Security.getProviders() output at startup to diagnose provider stripping

Example fix

// before
String md5 = MatrixUtil.getMD5String(data); // may throw

// after
try {
    String md5 = MatrixUtil.getMD5String(data);
} catch (RuntimeException e) {
    if (e.getCause() instanceof NoSuchAlgorithmException) {
        Security.addProvider(new BouncyCastleProvider());
    }
    throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

try {
    MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
    Security.addProvider(new BouncyCastleProvider());
}

Try / catch

try {
    String md5 = MatrixUtil.getMD5String(data);
} catch (RuntimeException e) {
    if (e.getCause() instanceof NoSuchAlgorithmException) {
        Security.addProvider(new BouncyCastleProvider());
        // retry once or fall back to another hash
    } else { throw e; }
}

Prevention

When it happens

Trigger: MessageDigest.getInstance("MD5") throwing NoSuchAlgorithmException inside the ThreadLocal's initialValue() when getMD5String first runs on a thread.

Common situations: Heavily stripped JRE/Android builds with removed crypto providers; custom Provider configurations that unregister default algorithms; exotic JVMs or security policy setups (e.g. some embedded or hardened environments).

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/69cda99f1ad7a8cb. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/MatrixUtil.java:224

     */
    public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to close resource", e);
        }
    }

    private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private final static ThreadLocal<MessageDigest> MD5_DIGEST = new ThreadLocal<MessageDigest>() {
        @Override
        protected MessageDigest initialValue() {
            try {
                return MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Initialize MD5 failed.", e);
            }
        }
    };

    public static String getMD5String(String s) {
        return getMD5String(s.getBytes());
    }

    public static String getMD5String(byte[] bytes) {
        MessageDigest digest = MD5_DIGEST.get();
        return bufferToHex(digest.digest(bytes));
    }

    private final static ThreadLocal<MessageDigest> SHA256_DIGEST = new ThreadLocal<MessageDigest>() {
        @Override
        protected MessageDigest initialValue() {
            try {
                return MessageDigest.getInstance("SHA-256");

View on GitHub (pinned to 3b8293bd65)