{"absolute_url": "/jbmckee/random-password/", "blog_post": false, "code": "$pwd = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));\n\nOR\n\n// usage: $newpassword = generatePassword(12); // for a 12-char password, upper/lower/numbers.\n// functions that use rand() or mt_rand() are not secure according to the PHP manual.\nfunction getRandomBytes($nbBytes = 32)\n{\n    $bytes = openssl_random_pseudo_bytes($nbBytes, $strong);\n    if (false !== $bytes && true === $strong) {\n        return $bytes;\n    }\n    else {\n        throw new \\Exception(\"Unable to generate secure token from OpenSSL.\");\n    }\n}\nfunction generatePassword($length){\n    return substr(preg_replace(\"/[^a-zA-Z0-9]/\", \"\", base64_encode(getRandomBytes($length+1))),0,$length);\n}\n", "created": "2018-04-13T17:27:30.408715", "description": "", "description_rendered": "", "embed_url": "https://snipt.net/embed/2412c32037a574e182a9dcd04fde0d3e/", "full_absolute_url": "/jbmckee/random-password/", "id": 55, "lexer": "text", "line_count": 20, "log_entries": [{"code": "$pwd = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));\n\nOR\n\n// usage: $newpassword = generatePassword(12); // for a 12-char password, upper/lower/numbers.\n// functions that use rand() or mt_rand() are not secure according to the PHP manual.\nfunction getRandomBytes($nbBytes = 32)\n{\n    $bytes = openssl_random_pseudo_bytes($nbBytes, $strong);\n    if (false !== $bytes && true === $strong) {\n        return $bytes;\n    }\n    else {\n        throw new \\Exception(\"Unable to generate secure token from OpenSSL.\");\n    }\n}\nfunction generatePassword($length){\n    return substr(preg_replace(\"/[^a-zA-Z0-9]/\", \"\", base64_encode(getRandomBytes($length+1))),0,$length);\n}\n", "created": "2018-04-13T17:27:30.686671", "diff": "--- \n+++ \n@@ -0,0 +1,19 @@\n+$pwd = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));\n+\n+OR\n+\n+// usage: $newpassword = generatePassword(12); // for a 12-char password, upper/lower/numbers.\n+// functions that use rand() or mt_rand() are not secure according to the PHP manual.\n+function getRandomBytes($nbBytes = 32)\n+{\n+    $bytes = openssl_random_pseudo_bytes($nbBytes, $strong);\n+    if (false !== $bytes && true === $strong) {\n+        return $bytes;\n+    }\n+    else {\n+        throw new \\Exception(\"Unable to generate secure token from OpenSSL.\");\n+    }\n+}\n+function generatePassword($length){\n+    return substr(preg_replace(\"/[^a-zA-Z0-9]/\", \"\", base64_encode(getRandomBytes($length+1))),0,$length);\n+}\n", "user": "jbmckee"}], "meta": "", "modified": "2026-04-27T10:40:33.372957", "publish_date": "2018-04-13T17:27:30.365732", "raw_url": "/raw/2412c32037a574e182a9dcd04fde0d3e/", "resource_uri": "/api/public/snipt/55/", "slug": "random-password", "stylized": "<table class=\"highlighttable\"><tr><td class=\"linenos\"><div class=\"linenodiv\"><pre><a href=\"#L-1\"> 1</a>\n<a href=\"#L-2\"> 2</a>\n<a href=\"#L-3\"> 3</a>\n<a href=\"#L-4\"> 4</a>\n<a href=\"#L-5\"> 5</a>\n<a href=\"#L-6\"> 6</a>\n<a href=\"#L-7\"> 7</a>\n<a href=\"#L-8\"> 8</a>\n<a href=\"#L-9\"> 9</a>\n<a href=\"#L-10\">10</a>\n<a href=\"#L-11\">11</a>\n<a href=\"#L-12\">12</a>\n<a href=\"#L-13\">13</a>\n<a href=\"#L-14\">14</a>\n<a href=\"#L-15\">15</a>\n<a href=\"#L-16\">16</a>\n<a href=\"#L-17\">17</a>\n<a href=\"#L-18\">18</a>\n<a href=\"#L-19\">19</a></pre></div></td><td class=\"code\"><div class=\"highlight\"><pre><span></span><span id=\"L-1\"><a name=\"L-1\"></a>$pwd = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));\n</span><span id=\"L-2\"><a name=\"L-2\"></a>\n</span><span id=\"L-3\"><a name=\"L-3\"></a>OR\n</span><span id=\"L-4\"><a name=\"L-4\"></a>\n</span><span id=\"L-5\"><a name=\"L-5\"></a>// usage: $newpassword = generatePassword(12); // for a 12-char password, upper/lower/numbers.\n</span><span id=\"L-6\"><a name=\"L-6\"></a>// functions that use rand() or mt_rand() are not secure according to the PHP manual.\n</span><span id=\"L-7\"><a name=\"L-7\"></a>function getRandomBytes($nbBytes = 32)\n</span><span id=\"L-8\"><a name=\"L-8\"></a>{\n</span><span id=\"L-9\"><a name=\"L-9\"></a>    $bytes = openssl_random_pseudo_bytes($nbBytes, $strong);\n</span><span id=\"L-10\"><a name=\"L-10\"></a>    if (false !== $bytes &amp;&amp; true === $strong) {\n</span><span id=\"L-11\"><a name=\"L-11\"></a>        return $bytes;\n</span><span id=\"L-12\"><a name=\"L-12\"></a>    }\n</span><span id=\"L-13\"><a name=\"L-13\"></a>    else {\n</span><span id=\"L-14\"><a name=\"L-14\"></a>        throw new \\Exception(&quot;Unable to generate secure token from OpenSSL.&quot;);\n</span><span id=\"L-15\"><a name=\"L-15\"></a>    }\n</span><span id=\"L-16\"><a name=\"L-16\"></a>}\n</span><span id=\"L-17\"><a name=\"L-17\"></a>function generatePassword($length){\n</span><span id=\"L-18\"><a name=\"L-18\"></a>    return substr(preg_replace(&quot;/[^a-zA-Z0-9]/&quot;, &quot;&quot;, base64_encode(getRandomBytes($length+1))),0,$length);\n</span><span id=\"L-19\"><a name=\"L-19\"></a>}\n</span></pre></div>\n</td></tr></table>", "tags": [], "title": "random password", "user": {"absolute_url": "/jbmckee/", "email_md5": "f4da96308b6db1b0b19738a7c105d3da", "id": 1, "resource_uri": "/api/public/user/1/", "snipts": "/api/public/snipt/?user=1", "snipts_count": 72, "username": "jbmckee"}}