{"record":{"id":"040649b853351cb6","repo":"Tencent/matrix","slug":"initialize-sha256-digest-failed","errorCode":null,"errorMessage":"Initialize SHA256-DIGEST failed.","messagePattern":"Initialize SHA256-DIGEST failed\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/MatrixUtil.java","lineNumber":244,"sourceCode":"        }\n    };\n\n    public static String getMD5String(String s) {\n        return getMD5String(s.getBytes());\n    }\n\n    public static String getMD5String(byte[] bytes) {\n        MessageDigest digest = MD5_DIGEST.get();\n        return bufferToHex(digest.digest(bytes));\n    }\n\n    private final static ThreadLocal<MessageDigest> SHA256_DIGEST = new ThreadLocal<MessageDigest>() {\n        @Override\n        protected MessageDigest initialValue() {\n            try {\n                return MessageDigest.getInstance(\"SHA-256\");\n            } catch (NoSuchAlgorithmException e) {\n                throw new RuntimeException(\"Initialize SHA256-DIGEST failed.\", e);\n            }\n        }\n    };\n\n    private static byte[] getSHA(String input) throws NoSuchAlgorithmException {\n        // Static getInstance method is called with hashing SHA\n        MessageDigest md = SHA256_DIGEST.get();\n\n        // digest() method called\n        // to calculate message digest of an input\n        // and return array of byte\n        return md.digest(input.getBytes(StandardCharsets.UTF_8));\n    }\n\n    private static String toHexString(byte[] hash) {\n        // Convert byte array into signum representation\n        BigInteger number = new BigInteger(1, hash);\n","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/MatrixUtil.java#L226-L262","documentation":"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.","triggerScenarios":"First access of the SHA256_DIGEST ThreadLocal on a thread (via getSHA/getSHA256String) where no provider supplies SHA-256.","commonSituations":"Removed or overridden security providers; custom java.security policy files; hardened/embedded runtime images lacking standard message-digest algorithms.","solutions":["Register a provider that supports SHA-256 (e.g. BouncyCastle) via Security.addProvider()","Audit Security.getProviders()/Provider services to confirm SHA-256 availability","Catch the RuntimeException and fall back to a manually configured digest","Fix java.security registration defaults in the build/runtime image"],"exampleFix":"// before\nbyte[] digest = MatrixUtil.getSHA256String(input); // may throw\n\n// after\nstatic {\n    if (digestUnavailable()) {\n        Security.addProvider(new BouncyCastleProvider());\n    }\n}","handlingStrategy":"try-catch","validationCode":"try {\n    MessageDigest.getInstance(\"SHA-256\");\n} catch (NoSuchAlgorithmException e) {\n    Security.addProvider(new BouncyCastleProvider());\n}","typeGuard":null,"tryCatchPattern":"try {\n    byte[] digest = MatrixUtil.getSHA256String(input);\n} catch (RuntimeException e) {\n    if (e.getCause() instanceof NoSuchAlgorithmException) {\n        Security.addProvider(new BouncyCastleProvider());\n    } else { throw e; }\n}","preventionTips":["Confirm SHA-256 availability at startup with a cheap MessageDigest.getInstance probe","Avoid removing default JCA providers in custom runtime images","Register BouncyCastle as a fallback provider"],"tags":["android","crypto","sha256","threadlocal"],"backgroundTag":"module-init-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}