{"id":298589,"date":"2024-05-20T16:56:28","date_gmt":"2024-05-20T08:56:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/298589.html"},"modified":"2024-05-20T16:56:34","modified_gmt":"2024-05-20T08:56:34","slug":"php-%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-md5-%e5%ae%9e%e7%8e%b0%e5%8a%a0%e5%af%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/298589.html","title":{"rendered":"PHP \u7a0b\u5e8f\u5982\u4f55\u4f7f\u7528 md5 \u5b9e\u73b0\u52a0\u5bc6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27150547\/526d33e7-b06d-4dd1-9c25-5a357288034c.webp\" alt=\"PHP \u7a0b\u5e8f\u5982\u4f55\u4f7f\u7528 md5 \u5b9e\u73b0\u52a0\u5bc6\" \/><\/p>\n<p><p>MD5\uff08Message Digest Algorithm 5\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u52a0\u5bc6\u6563\u5217\u7b97\u6cd5\uff0c\u7528\u4e8e\u786e\u4fdd\u4fe1\u606f\u5b8c\u6574\u6027\u3002\u5728PHP\u7a0b\u5e8f\u4e2d\uff0c<strong>\u4f7f\u7528<code>md5()<\/code>\u51fd\u6570\u5b9e\u73b0\u52a0\u5bc6\u3001\u786e\u4fdd\u5bc6\u7801\u6216\u654f\u611f\u6570\u636e\u7684\u5b89\u5168\u6027\u3001\u751f\u6210\u72ec\u7279\u7684\u6587\u4ef6\u540d\u6216\u4ee4\u724c<\/strong>\u3002\u867d\u7136MD5\u56e0\u6613\u53d7\u5f69\u8679\u8868\u653b\u51fb\u548c\u78b0\u649e\u653b\u51fb\u800c\u4e0d\u518d\u88ab\u8ba4\u4e3a\u662f\u7edd\u5bf9\u5b89\u5168\u7684\u52a0\u5bc6\u5f62\u5f0f\uff0c\u4f46\u5b83\u5728\u8bb8\u591a\u975e\u9ad8\u5b89\u5168\u73af\u5883\u4e0b\u4f9d\u7136\u662f\u4e00\u79cd\u5feb\u901f\u4e14\u7b80\u6613\u7684\u52a0\u5bc6\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>MD5 \u5c24\u5176\u9002\u7528\u4e8e\u751f\u6210\u72ec\u7279\u7684\u6587\u4ef6\u540d\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u56e0\u4e3a\u5b83\u80fd\u5feb\u901f\u4ea7\u751f\u56fa\u5b9a\u957f\u5ea6\u7684\u6563\u5217\u503c\u3002\u4f8b\u5982\uff0c<strong>\u5728\u50a8\u5b58\u7528\u6237\u5bc6\u7801\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7MD5\u5c06\u660e\u6587\u5bc6\u7801\u8f6c\u6362\u6210\u6563\u5217\u503c\u50a8\u5b58\uff0c\u8fd9\u6837\u5373\u4f7f\u6570\u636e\u5e93\u88ab\u6cc4\u9732\uff0c\u653b\u51fb\u8005\u4e5f\u65e0\u6cd5\u76f4\u63a5\u77e5\u6653\u539f\u59cb\u5bc6\u7801<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MD5\u52a0\u5bc6\u7684\u539f\u7406<\/h3>\n<\/p>\n<p><h4>\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>MD5\u52a0\u5bc6\u662f\u5c06\u6570\u636e\uff08\u5982\u5b57\u7b26\u4e32\uff09\u901a\u8fc7\u7279\u5b9a\u7b97\u6cd5\u8f6c\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u8be5\u6563\u5217\u8fc7\u7a0b\u662f\u5355\u5411\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4eceMD5\u6563\u5217\u503c\u51e0\u4e4e\u4e0d\u53ef\u80fd\u53cd\u63a8\u51fa\u539f\u59cb\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u52a0\u5bc6\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\uff0cMD5\u7b97\u6cd5\u5904\u7406\u8f93\u5165\u7684\u6570\u636e\u6d41\uff0c\u7ecf\u8fc7\u4e00\u7cfb\u5217\u590d\u6742\u53d8\u6362\u6700\u7ec8\u751f\u6210\u4e00\u4e2a128\u4f4d\u7684\u56fa\u5b9a\u957f\u5ea6\u6563\u5217\u503c\uff0c\u901a\u5e38\u4ee532\u5b57\u7b26\u7684\u5341\u516d\u8fdb\u5236\u6570\u5c55\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528md5()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728PHP\u4e2d\u8c03\u7528MD5\u52a0\u5bc6\u975e\u5e38\u7b80\u5355\uff0c\u4ec5\u9700\u4f7f\u7528md5()\u51fd\u6570\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">string md5 ( string $str [, bool $raw_output = FALSE ] )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff0c\u663e\u793a\u4e86\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528MD5\u52a0\u5bc6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntext = &quot;SecretPassword&quot;;<\/p>\n<p>$hash = md5($plaintext);<\/p>\n<p>echo $hash;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001MD5\u52a0\u5bc6\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u5bc6\u7801\u50a8\u5b58<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u5bc6\u7801\u5b58\u50a8\u65b9\u9762\uff0cMD5\u901a\u5e38\u7528\u6765\u5c06\u7528\u6237\u521b\u5efa\u7684\u660e\u6587\u5bc6\u7801\u8f6c\u6362\u6210\u6563\u5217\u503c\uff0c\u5b58\u5165\u6570\u636e\u5e93\u3002<strong>\u7136\u800c\uff0c\u5efa\u8bae\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u5982bcrypt\u6216\u7ed3\u5408\u76d0\u503c\uff08salt\uff09\u548c\u8fed\u4ee3\u6765\u589e\u5f3aMD5\u7684\u5b89\u5168\u6027<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u6587\u4ef6\u6821\u9a8c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528MD5\u53ef\u4ee5\u751f\u6210\u6587\u4ef6\u7684\u6563\u5217\u503c\uff0c<strong>\u4ee5\u6b64\u6765\u68c0\u9a8c\u6587\u4ef6\u5728\u4f20\u8f93\u6216\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u662f\u5426\u88ab\u7be1\u6539<\/strong>\u3002\u5982\u679c\u6587\u4ef6\u7684MD5\u503c\u53d1\u751f\u53d8\u5316\uff0c\u5219\u6587\u4ef6\u5185\u5bb9\u4e5f\u968f\u4e4b\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u9ad8MD5\u5b89\u5168\u6027\u7684\u7b56\u7565<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528\u76d0\u503c\uff08Salt\uff09<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u589e\u5f3aMD5\u52a0\u5bc6\u7684\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u5728\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u76d0\u503c\u3002\u76d0\u503c\u7684\u4f7f\u7528\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u62b5\u6297\u5f69\u8679\u8868\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$user_password = &quot;SecretPassword&quot;;<\/p>\n<p>\/\/ \u521b\u5efa\u76d0\u503c<\/p>\n<p>$salt = &quot;randomString&quot;;<\/p>\n<p>\/\/ \u5c06\u76d0\u503c\u4e0e\u5bc6\u7801\u7ed3\u5408<\/p>\n<p>$saltedPW = $salt . $user_password;<\/p>\n<p>$hash = md5($saltedPW);<\/p>\n<p>echo $hash;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u54c8\u5e0c\u7684\u54c8\u5e0c<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u63d0\u9ad8\u5b89\u5168\u6027\u7684\u505a\u6cd5\u662f\u5bf9\u6563\u5217\u503c\u518d\u6b21\u8fdb\u884c\u6563\u5217\u3002\u901a\u8fc7\u591a\u6b21\u6563\u5217\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u51cf\u5c11\u88ab\u7834\u89e3\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001MD5\u7684\u5c40\u9650\u6027\u4e0e\u66ff\u4ee3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>MD5\u7684\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>MD5\u867d\u7136\u65b9\u4fbf\u548c\u5feb\u901f\uff0c\u4f46\u56e0\u4e3a\u56fa\u6709\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u5b83\u4e0d\u9002\u5408\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u5408\uff0c\u5982\u91d1\u878d\u4fe1\u606f\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><h4>\u66ff\u4ee3\u65b9\u6848<\/h4>\n<\/p>\n<p><p>\u73b0\u4ee3\u66f4\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5982SHA-256\u548cbcrypt\uff0c\u63d0\u4f9b\u4e86\u66f4\u590d\u6742\u548c\u5b89\u5168\u7684\u52a0\u5bc6\u65b9\u5f0f\uff0c<strong>\u5e94\u4f18\u5148\u8003\u8651\u5728\u654f\u611f\u4fe1\u606f\u52a0\u5bc6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u66ff\u4ee3\u65b9\u6848<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1MD5\u5728PHP\u4e2d\u7684\u4f7f\u7528\u6781\u4e3a\u7b80\u5355\u65b9\u4fbf\uff0c\u7531\u4e8e\u5176\u5df2\u77e5\u5b89\u5168\u5f31\u70b9\uff0c\u5f00\u53d1\u8005\u5e94\u5728\u5b9e\u8df5\u4e2d\u8003\u8651\u66f4\u73b0\u4ee3\u548c\u5b89\u5168\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u6216\u81f3\u5c11\u589e\u52a0\u590d\u6742\u5ea6\uff0c\u6bd4\u5982\u8fd0\u7528\u76d0\u503c\u548c\u591a\u91cd\u6563\u5217\u8fc7\u7a0b\uff0c\u4ee5\u589e\u5f3a\u5b89\u5168\u6027\u3002\u5728\u5bf9\u6027\u80fd\u8981\u6c42\u4e0d\u9ad8\u7684\u60c5\u51b5\u4e0b\uff0c\u66f4\u63a8\u8350\u4f7f\u7528\u4f8b\u5982SHA-256\u3001bcrypt\u6216Argon2\u8fd9\u7c7b\u7b97\u6cd5\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b89\u5168\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 md5 \u5728 PHP \u7a0b\u5e8f\u4e2d\u8fdb\u884c\u5bc6\u7801\u52a0\u5bc6\uff1f<\/strong><br \/>\n\u5728 PHP \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 md5 \u51fd\u6570\u6765\u5b9e\u73b0\u5bc6\u7801\u52a0\u5bc6\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ecf\u8fc7 md5 \u7b97\u6cd5\u5904\u7406\u540e\u7684\u54c8\u5e0c\u503c\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u4f7f\u7528 md5 \u51fd\u6570\u8fdb\u884c\u52a0\u5bc6\u3002\u7136\u540e\uff0c\u5c06\u52a0\u5bc6\u540e\u7684\u5bc6\u7801\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u6216\u5176\u4ed6\u5730\u65b9\uff0c\u7528\u4e8e\u540e\u7eed\u767b\u5f55\u9a8c\u8bc1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1 md5 \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u52a0\u5bc6\u65b9\u5f0f\uff0c\u4f46\u4e0d\u63a8\u8350\u5c06\u5176\u7528\u4e8e\u5b58\u50a8\u654f\u611f\u6570\u636e\uff0c\u56e0\u4e3a\u5176\u76f8\u5bf9\u8f83\u5f31\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>2. md5 \u5728 PHP \u4e2d\u6709\u54ea\u4e9b\u5b89\u5168\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\n\u5c3d\u7ba1 md5 \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5bc6\u7801\u52a0\u5bc6\u65b9\u6cd5\uff0c\u4f46\u5b83\u5df2\u7ecf\u4e0d\u518d\u88ab\u89c6\u4e3a\u5b89\u5168\u7684\u52a0\u5bc6\u65b9\u5f0f\u3002\u4e3b\u8981\u539f\u56e0\u6709\u4e24\u70b9\uff1a\u9996\u5148\uff0c\u7531\u4e8e md5 \u51fd\u6570\u662f\u5355\u5411\u6563\u5217\u51fd\u6570\uff0c\u65e0\u6cd5\u5c06\u54c8\u5e0c\u503c\u8fd8\u539f\u56de\u539f\u59cb\u5bc6\u7801\u3002\u8fd9\u610f\u5473\u7740\u5728\u6bd4\u8f83\u5bc6\u7801\u65f6\uff0c\u65e0\u6cd5\u76f4\u63a5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u4e0e\u5b58\u50a8\u7684\u52a0\u5bc6\u5bc6\u7801\u8fdb\u884c\u5bf9\u6bd4\uff1b\u5176\u6b21\uff0c\u7531\u4e8e md5 \u7684\u7b97\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u5bb9\u6613\u53d7\u5230\u66b4\u529b\u7834\u89e3\u6216\u5f69\u8679\u8868\u653b\u51fb\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u654f\u611f\u6570\u636e\u6216\u7528\u6237\u5bc6\u7801\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u52a0\u5bc6\u65b9\u6cd5\uff0c\u5982 bcrypt \u6216 Argon2\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u66f4\u5b89\u5168\u7684\u5bc6\u7801\u52a0\u5bc6\u65b9\u5f0f\u53ef\u4ee5\u66ff\u4ee3 md5\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728 PHP \u7a0b\u5e8f\u4e2d\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u5bc6\u7801\u52a0\u5bc6\u65b9\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528 bcrypt \u6216 Argon2\u3002\u8fd9\u4e9b\u7b97\u6cd5\u57fa\u4e8e Blowfish \u548c Argon2i \u7b97\u6cd5\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u522b\u7684\u5bc6\u7801\u6563\u5217\u4fdd\u62a4\u3002\u5728\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u65f6\uff0c\u4f60\u9700\u8981\u5f15\u5165 bcrypt \u6216 Argon2i \u7684\u76f8\u5173\u51fd\u6570\u5e93\uff0c\u5e76\u6309\u7167\u5176\u6307\u5bfc\u8fdb\u884c\u5bc6\u7801\u52a0\u5bc6\u3001\u5b58\u50a8\u548c\u9a8c\u8bc1\u3002\u4e0e md5 \u76f8\u6bd4\uff0cbcrypt \u548c Argon2 \u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u548c\u62b5\u6297\u66b4\u529b\u7834\u89e3\u653b\u51fb\u7684\u80fd\u529b\u3002\u5f53\u7136\u4e86\uff0c\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5bc6\u7801\u52a0\u5bc6\u65b9\u5f0f\uff0c\u90fd\u4e0d\u80fd\u4fdd\u8bc1\u7edd\u5bf9\u7684\u5b89\u5168\uff0c\u56e0\u6b64\u5408\u7406\u7684\u5bc6\u7801\u7b56\u7565\u548c\u5176\u4ed6\u5b89\u5168\u63aa\u65bd\u4e5f\u540c\u6837\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"MD5\uff08Message Digest Algorithm 5\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u52a0\u5bc6\u6563\u5217\u7b97\u6cd5\uff0c\u7528\u4e8e\u786e\u4fdd\u4fe1\u606f\u5b8c\u6574\u6027 [&hellip;]","protected":false},"author":3,"featured_media":298593,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/298589"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=298589"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/298589\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/298593"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=298589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=298589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=298589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}