{"id":187744,"date":"2024-11-11T16:49:22","date_gmt":"2024-11-11T08:49:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/187744\/"},"modified":"2024-11-11T16:49:23","modified_gmt":"2024-11-11T08:49:23","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2%e6%96%b9%e6%b3%95%e8%88%87%e5%ae%89%e5%85%a8%e6%80%a7-%e5%af%86%e7%a2%bc%e5%ad%98%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/187744\/","title":{"rendered":"\u6578\u64da\u5eab\u4e2d\u7684\u5bc6\u78bc\u5b58\u5132\u65b9\u6cd5\u8207\u5b89\u5168\u6027 (\u5bc6\u78bc\u5b58\u6578\u64da\u5eab\u4e2d)"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2%e6%96%b9%e6%b3%95%e8%88%87%e5%ae%89%e5%85%a8%e6%80%a7-cSejBXrfAT\">\u6578\u64da\u5eab\u4e2d\u7684\u5bc6\u78bc\u5b58\u5132\u65b9\u6cd5\u8207\u5b89\u5168\u6027<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u5b57\u5316\u7684\u6642\u4ee3\uff0c\u6578\u64da\u5b89\u5168\u6027\u6210\u70ba\u4e86\u4f01\u696d\u548c\u500b\u4eba\u6700\u95dc\u6ce8\u7684\u554f\u984c\u4e4b\u4e00\u3002\u5c24\u5176\u662f\u5bc6\u78bc\u7684\u5b58\u5132\uff0c\u56e0\u70ba\u5b83\u5011\u662f\u4fdd\u8b77\u7528\u6236\u6578\u64da\u548c\u96b1\u79c1\u7684\u7b2c\u4e00\u9053\u9632\u7dda\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6578\u64da\u5eab\u4e2d\u5bc6\u78bc\u7684\u5b58\u5132\u65b9\u6cd5\u53ca\u5176\u5b89\u5168\u6027\uff0c\u5e6b\u52a9\u8b80\u8005\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u4fdd\u8b77\u5bc6\u78bc\u3002<\/p>\n<h2 id=\"%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%8e%9f%e5%89%87-cSejBXrfAT\">\u5bc6\u78bc\u5b58\u5132\u7684\u57fa\u672c\u539f\u5247<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\u5b58\u5132\u5bc6\u78bc\u6642\uff0c\u6700\u91cd\u8981\u7684\u539f\u5247\u662f\u6c38\u9060\u4e0d\u8981\u4ee5\u660e\u6587\u5f62\u5f0f\u5b58\u5132\u5bc6\u78bc\u3002\u9019\u610f\u5473\u8457\uff0c\u5373\u4f7f\u6578\u64da\u5eab\u88ab\u653b\u64ca\u8005\u5165\u4fb5\uff0c\u653b\u64ca\u8005\u4e5f\u7121\u6cd5\u76f4\u63a5\u7372\u53d6\u7528\u6236\u7684\u5bc6\u78bc\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u5bc6\u78bc\u5b58\u5132\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e5%93%88%e5%b8%8c%ef%bc%88hashing%ef%bc%89-cSejBXrfAT\">1. \u54c8\u5e0c\uff08Hashing\uff09<\/h3>\n<p>\u54c8\u5e0c\u662f\u4e00\u7a2e\u5c07\u4efb\u610f\u9577\u5ea6\u7684\u8f38\u5165\u6578\u64da\u8f49\u63db\u70ba\u56fa\u5b9a\u9577\u5ea6\u7684\u5b57\u7b26\u4e32\u7684\u904e\u7a0b\u3002\u5e38\u898b\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ecSHA-256\u3001bcrypt\u548cArgon2\u7b49\u3002\u9019\u4e9b\u7b97\u6cd5\u7684\u7279\u9ede\u662f\u4e0d\u53ef\u9006\uff0c\u5373\u7121\u6cd5\u5f9e\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u539f\u59cb\u5bc6\u78bc\u3002<\/p>\n<pre><code>const crypto = require('crypto');\n\nfunction hashPassword(password) {\n    return crypto.createHash('sha256').update(password).digest('hex');\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528SHA-256\u7b97\u6cd5\u5c0d\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\u3002\u5118\u7ba1SHA-256\u662f\u4e00\u7a2e\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f46\u5b83\u4e26\u4e0d\u9069\u5408\u7528\u65bc\u5bc6\u78bc\u5b58\u5132\uff0c\u56e0\u70ba\u5b83\u7684\u8a08\u7b97\u901f\u5ea6\u8f03\u5feb\uff0c\u5bb9\u6613\u53d7\u5230\u66b4\u529b\u7834\u89e3\u653b\u64ca\u3002<\/p>\n<h3 id=\"2-%e5%8a%a0%e9%b9%bd%ef%bc%88salting%ef%bc%89-cSejBXrfAT\">2. \u52a0\u9e7d\uff08Salting\uff09<\/h3>\n<p>\u70ba\u4e86\u589e\u5f37\u54c8\u5e0c\u7684\u5b89\u5168\u6027\uff0c\u901a\u5e38\u6703\u5728\u5bc6\u78bc\u54c8\u5e0c\u4e4b\u524d\u6dfb\u52a0\u4e00\u500b\u96a8\u6a5f\u751f\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u7a31\u70ba\u201c\u9e7d\u201d\u3002\u9019\u6a23\uff0c\u5373\u4f7f\u5169\u500b\u7528\u6236\u7684\u5bc6\u78bc\u76f8\u540c\uff0c\u6700\u7d42\u7684\u54c8\u5e0c\u503c\u4e5f\u6703\u4e0d\u540c\u3002<\/p>\n<pre><code>const crypto = require('crypto');\n\nfunction hashPasswordWithSalt(password, salt) {\n    return crypto.createHash('sha256').update(password + salt).digest('hex');\n}\n\nconst salt = crypto.randomBytes(16).toString('hex');\nconst hashedPassword = hashPasswordWithSalt('myPassword', salt);\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u751f\u6210\u4e86\u4e00\u500b\u96a8\u6a5f\u7684\u9e7d\u4e26\u5c07\u5176\u8207\u5bc6\u78bc\u7d50\u5408\u5f8c\u9032\u884c\u54c8\u5e0c\u8655\u7406\u3002\u9019\u6a23\uff0c\u5373\u4f7f\u653b\u64ca\u8005\u7372\u5f97\u4e86\u54c8\u5e0c\u503c\uff0c\u4e5f\u7121\u6cd5\u8f15\u6613\u5730\u9032\u884c\u7834\u89e3\u3002<\/p>\n<h3 id=\"3-%e5%af%86%e7%a2%bc%e5%93%88%e5%b8%8c%e5%87%bd%e6%95%b8%ef%bc%88password-hashing-functions%ef%bc%89-cSejBXrfAT\">3. \u5bc6\u78bc\u54c8\u5e0c\u51fd\u6578\uff08Password Hashing Functions\uff09<\/h3>\n<p>\u5c08\u9580\u70ba\u5bc6\u78bc\u5b58\u5132\u8a2d\u8a08\u7684\u54c8\u5e0c\u51fd\u6578\uff0c\u5982bcrypt\u3001scrypt\u548cArgon2\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3002\u9019\u4e9b\u51fd\u6578\u4e0d\u50c5\u6703\u52a0\u9e7d\uff0c\u9084\u6703\u9032\u884c\u591a\u6b21\u8fed\u4ee3\uff0c\u589e\u52a0\u8a08\u7b97\u7684\u8907\u96dc\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u62b5\u6297\u66b4\u529b\u7834\u89e3\u7684\u80fd\u529b\u3002<\/p>\n<pre><code>const bcrypt = require('bcrypt');\n\nasync function hashPassword(password) {\n    const saltRounds = 10;\n    return await bcrypt.hash(password, saltRounds);\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u4f7f\u7528bcrypt\u5eab\u5c0d\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\uff0c\u4e26\u6307\u5b9a\u4e86\u9e7d\u7684\u8f2a\u6578\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u5bc6\u78bc\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2%e7%9a%84%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-cSejBXrfAT\">\u5bc6\u78bc\u5b58\u5132\u7684\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u9664\u4e86\u9078\u64c7\u5408\u9069\u7684\u54c8\u5e0c\u7b97\u6cd5\u548c\u52a0\u9e7d\u6280\u8853\u5916\uff0c\u9084\u6709\u5176\u4ed6\u4e00\u4e9b\u5b89\u5168\u6027\u8003\u91cf\uff1a<\/p>\n<ul>\n<li><strong>\u5b9a\u671f\u66f4\u65b0\u5b89\u5168\u7b56\u7565\uff1a<\/strong>\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u65b0\u7684\u653b\u64ca\u624b\u6bb5\u4e0d\u65b7\u51fa\u73fe\uff0c\u56e0\u6b64\u5b9a\u671f\u6aa2\u67e5\u548c\u66f4\u65b0\u5bc6\u78bc\u5b58\u5132\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u4f7f\u7528HTTPS\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u662f\u52a0\u5bc6\u7684\uff0c\u9632\u6b62\u4e2d\u9593\u4eba\u653b\u64ca\u3002<\/li>\n<li><strong>\u9650\u5236\u767b\u9304\u5617\u8a66\uff1a<\/strong>\u901a\u904e\u9650\u5236\u767b\u9304\u5617\u8a66\u6b21\u6578\u4f86\u9632\u6b62\u66b4\u529b\u7834\u89e3\u653b\u64ca\u3002<\/li>\n<li><strong>\u76e3\u63a7\u7570\u5e38\u884c\u70ba\uff1a<\/strong>\u53ca\u6642\u6aa2\u6e2c\u548c\u97ff\u61c9\u53ef\u7591\u7684\u767b\u9304\u6d3b\u52d5\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cSejBXrfAT\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\u5b89\u5168\u5730\u5b58\u5132\u5bc6\u78bc\u662f\u4fdd\u8b77\u7528\u6236\u6578\u64da\u7684\u91cd\u8981\u63aa\u65bd\u3002\u901a\u904e\u4f7f\u7528\u54c8\u5e0c\u3001\u52a0\u9e7d\u548c\u5c08\u9580\u7684\u5bc6\u78bc\u54c8\u5e0c\u51fd\u6578\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u5bc6\u78bc\u7684\u5b89\u5168\u6027\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u66f4\u65b0\u5b89\u5168\u7b56\u7565\u548c\u76e3\u63a7\u7570\u5e38\u884c\u70ba\u4e5f\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u548c\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u5c07\u6709\u52a9\u65bc\u9032\u4e00\u6b65\u52a0\u5f37\u5b89\u5168\u6027\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u6578\u64da\u5eab\u4e2d\u5bc6\u78bc\u5b58\u5132\u65b9\u6cd5\u7684\u5b89\u5168\u6027\uff0c\u4e86\u89e3\u52a0\u5bc6\u6280\u8853\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u4fdd\u8b77\u7528\u6236\u8cc7\u6599\u514d\u53d7\u672a\u6388\u6b0a\u8a2a\u554f\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-187744","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=187744"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187744\/revisions"}],"predecessor-version":[{"id":187745,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187744\/revisions\/187745"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=187744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=187744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=187744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}