{"record":{"id":"8e15ad5e961da732","repo":"paragonie/random_compat","slug":"could-not-gather-sufficient-random-data-bytes-mcrypt","errorCode":null,"errorMessage":"Could not gather sufficient random data","messagePattern":"Could not gather sufficient random data","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"lib/random_bytes_mcrypt.php","lineNumber":75,"sourceCode":"        }\n\n        /** @var string|bool $buf */\n        $buf = @mcrypt_create_iv((int) $bytes, (int) MCRYPT_DEV_URANDOM);\n        if (\n            is_string($buf)\n                &&\n            RandomCompat_strlen($buf) === $bytes\n        ) {\n            /**\n             * Return our random entropy buffer here:\n             */\n            return $buf;\n        }\n\n        /**\n         * If we reach here, PHP has failed us.\n         */\n        throw new Exception(\n            'Could not gather sufficient random data'\n        );\n    }\n}\n","sourceCodeStart":57,"sourceCodeEnd":80,"githubUrl":"https://github.com/paragonie/random_compat/blob/b5d188cc9d5e02f94d2c41da23093f1ef557c5b1/lib/random_bytes_mcrypt.php#L57-L80","documentation":"The mcrypt-backed random_bytes() throws this Exception when mcrypt_create_iv(MCRYPT_DEV_URANDOM) returns false or otherwise fails to deliver random bytes, meaning even the urandom-backed mcrypt path is unusable. The polyfill fails closed rather than return weak randomness.","triggerScenarios":"random_bytes() called on a PHP 5 system where mcrypt_create_iv() fails — mcrypt unavailable, /dev/urandom unreadable, or the function suppressed by disable_functions — reaching the throw at lib/random_bytes_mcrypt.php:75.","commonSituations":"Legacy PHP 5.x builds without mcrypt compiled in, containers missing /dev/urandom, chroot jails lacking device nodes, or hosts that disable mcrypt functions via disable_functions.","solutions":["Verify the mcrypt extension is installed and enabled (php -m | grep mcrypt).","Ensure /dev/urandom exists and is readable by the PHP process.","Upgrade to PHP 7.0+ where random_bytes() is native and mcrypt is not needed.","Remove mcrypt from disable_functions / relax open_basedir if it blocks urandom.","Fail and alert rather than falling back to rand()/mt_rand(), which are not cryptographically secure."],"exampleFix":"// before\n$token = bin2hex(random_bytes(32));\n// after\ntry {\n    $token = bin2hex(random_bytes(32));\n} catch (Exception $e) {\n    throw new RuntimeException('Secure RNG unavailable; cannot mint token', 0, $e);\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check backend availability:\n// if (!extension_loaded('mcrypt') && PHP_VERSION_ID < 70000) failHard('No RNG backend');","typeGuard":"null","tryCatchPattern":"try {\n    $token = random_bytes(32);\n} catch (Exception $e) {\n    // Do NOT degrade to rand()/mt_rand()/uniqid()\n    throw new RuntimeException('CSPRNG unavailable; token generation aborted', 0, $e);\n}","preventionTips":["Ensure mcrypt is compiled in on PHP 5 hosts, or migrate to PHP 7+.","Confirm /dev/urandom is present and readable in every deployment image.","Keep mcrypt functions out of disable_functions.","Alert on this error: it indicates a host-level environment problem, not bad input."],"tags":["php","cryptography","randomness","mcrypt","environment"],"backgroundTag":"unsupported-platform","analyzedSha":"b5d188cc9d5e02f94d2c41da23093f1ef557c5b1","analyzedAt":"2026-09-13T16:12:09.755Z","contentChangedAt":"2026-09-13T16:12:09.755Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}