Tencent/matrix · error · RuntimeException

Initialize SHA256-DIGEST failed.

Error message

Initialize SHA256-DIGEST failed.

What it means

MatrixUtil's SHA-256 ThreadLocal throws this when MessageDigest.getInstance("SHA-256") raises NoSuchAlgorithmException in initialValue(). SHA-256 is part of the standard JCA algorithm set on Android, so the throw signals a non-compliant or stripped crypto provider environment.

Solutions

  1. Register a provider that supports SHA-256 (e.g. BouncyCastle) via Security.addProvider()
  2. Audit Security.getProviders()/Provider services to confirm SHA-256 availability
  3. Catch the RuntimeException and fall back to a manually configured digest
  4. Fix java.security registration defaults in the build/runtime image

Example fix

// before
byte[] digest = MatrixUtil.getSHA256String(input); // may throw

// after
static {
    if (digestUnavailable()) {
        Security.addProvider(new BouncyCastleProvider());
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

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

Try / catch

try {
    byte[] digest = MatrixUtil.getSHA256String(input);
} catch (RuntimeException e) {
    if (e.getCause() instanceof NoSuchAlgorithmException) {
        Security.addProvider(new BouncyCastleProvider());
    } else { throw e; }
}

Prevention

When it happens

Trigger: First access of the SHA256_DIGEST ThreadLocal on a thread (via getSHA/getSHA256String) where no provider supplies SHA-256.

Common situations: Removed or overridden security providers; custom java.security policy files; hardened/embedded runtime images lacking standard message-digest algorithms.

Related errors


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

Appendix: source

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

        }
    };

    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");
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Initialize SHA256-DIGEST failed.", e);
            }
        }
    };

    private static byte[] getSHA(String input) throws NoSuchAlgorithmException {
        // Static getInstance method is called with hashing SHA
        MessageDigest md = SHA256_DIGEST.get();

        // digest() method called
        // to calculate message digest of an input
        // and return array of byte
        return md.digest(input.getBytes(StandardCharsets.UTF_8));
    }

    private static String toHexString(byte[] hash) {
        // Convert byte array into signum representation
        BigInteger number = new BigInteger(1, hash);

View on GitHub (pinned to 3b8293bd65)