{"id":1478,"date":"2014-06-08T23:47:00","date_gmt":"2014-06-08T15:47:00","guid":{"rendered":"http:\/\/leegorous.net\/blog\/?p=1478"},"modified":"2014-06-09T10:18:01","modified_gmt":"2014-06-09T02:18:01","slug":"how-to-generate-unpredictable-unique-id","status":"publish","type":"post","link":"http:\/\/leegorous.net\/blog\/2014\/06\/08\/how-to-generate-unpredictable-unique-id\/","title":{"rendered":"\u5982\u4f55\u751f\u6210\u552f\u4e00\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684 ID"},"content":{"rendered":"<p>\u901a\u5e38\u6570\u636e\u5e93\u53ef\u4ee5\u751f\u6210\u552f\u4e00\u7684 ID\uff0c\u6700\u591a\u7684\u5c31\u662f\u6570\u5b57\u5e8f\u5217\uff0c\u4e5f\u6709\u50cf MongoDB \u8fd9\u6837\u4ea7\u751f\u7ec4\u5408\u5e8f\u5217\u7684\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u5f62\u5f0f\u7684 ID \u7531\u4e8e\u662f\u5e8f\u5217\uff0c\u662f\u53ef\u4ee5\u9884\u6d4b\u7684\u3002\u5982\u679c\u60f3\u5f97\u5230\u4e0d\u53ef\u9884\u6d4b\u4e14\u552f\u4e00\u7684 ID\uff0c\u65b9\u6cd5\u8fd8\u662f\u6709\u7684\u3002<\/p>\n<p>\u4e0b\u9762\u4e3b\u8981\u4ee5 Node.js \u7684\u73af\u5883\u4e3a\u4f8b\u3002<\/p>\n<h2>Node-uuid<\/h2>\n<p>Github \u4e0a\u6709\u4e2a <a title=\"node-uuid\" href=\"https:\/\/github.com\/broofa\/node-uuid\" target=\"_blank\">node-uuid<\/a> \u9879\u76ee\uff0c\u5b83\u53ef\u4ee5\u5feb\u901f\u5730\u751f\u6210\u7b26\u5408 <a title=\"RFC4122\" href=\"http:\/\/www.ietf.org\/rfc\/rfc4122.txt\" target=\"_blank\">RFC4122<\/a> \u89c4\u8303 version 1 \u6216\u8005 version 4 \u7684 UUID\u3002<\/p>\n<p>\u5b89\u88c5\uff0c\u65e2\u53ef\u4ee5\u901a\u8fc7 npm install node-uuid \uff0c\u4e5f\u53ef\u4ee5\u5728 package.json \u4e2d\u5b9a\u4e49\u3002\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<pre class=\"brush: javascript;\">var uuid = require(&#039;node-uuid&#039;);\r\n\r\n\/\/ \u4ea7\u751f\u4e00\u4e2a v1 (\u57fa\u4e8e\u65f6\u95f4\u7684) id\r\nuuid.v1(); \/\/ -&gt; &#039;6c84fb90-12c4-11e1-840d-7b25c5ee775a&#039;\r\n\r\n\/\/ \u4ea7\u751f\u4e00\u4e2a v4 (\u968f\u673a) id\r\nuuid.v4(); \/\/ -&gt; &#039;110ec58a-a0f2-4ac4-8393-c866d813b8d1\u2019\r\n<\/pre>\n<p>\u542c\u8d77\u6765\u5f88\u6709\u4fdd\u8bc1\uff0c\u53ea\u662f\u2026\u2026\u6709\u70b9\u592a\u957f\u4e86\u3002<\/p>\n<p>\u574a\u95f4\u4e5f\u6709\u4e00\u4e9b\u5728 UUID \u57fa\u7840\u4e0a\u968f\u673a\u622a\u53d6\u51e0\u4f4d\u7684\u529e\u6cd5\u6765\u7ec4\u6210\u4e00\u4e2a\u65b0\u7684\u77ed\u4e00\u70b9\u7684\u5b57\u7b26\u4e32\uff0c\u53ea\u4e0d\u8fc7\u552f\u4e00\u6027\u5c31\u53c8\u91cd\u65b0\u6210\u4e3a\u4e00\u4e2a\u95ee\u53f7\u4e86\u3002<\/p>\n<h2>Hashids<\/h2>\n<p>\u53e6\u5916\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6765\u81ea <a title=\"hashids.org\" href=\"http:\/\/www.hashids.org\/node-js\/\" target=\"_blank\">hashids<\/a>\uff0c\u5b83\u7684\u539f\u7406\u5c31\u662f\u4ece\u6570\u5b57\u7ecf\u8fc7\u4e00\u4e2a\u52a0\u76d0(salted)\u7b97\u6cd5\u4ea7\u751f\u4e00\u4e2a\u54c8\u5e0c(hash)\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u7b97\u6cd5\u5c31\u662f\u901a\u8fc7\u6df7\u6dc6\u4f7f\u7ed3\u679c\u5177\u6709\u4e0d\u53ef\u9884\u6d4b\u6027\uff0c\u800c\u552f\u4e00\u6027\u4f9d\u7136\u7531\u6570\u5b57\u672c\u8eab\u6765\u8fbe\u6210\uff0c\u4ece\u800c\u5f97\u5230(\u7c7b\u4f3c youtube \u91cc\u7684)\u8db3\u591f\u77ed\uff0c\u4e0d\u53ef\u9884\u6d4b\u4e14\u552f\u4e00\u7684 ID\u3002<\/p>\n<p>\u5b89\u88c5\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u53ea\u662f hashids \u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7684\u7248\u672c\u3002\u6bd4\u5982\u5728 package.json \u4e2d<\/p>\n<pre class=\"brush: javascript;\">&quot;dependencies&quot;: {\r\n  &quot;hashids&quot;: &quot;0.3.3&quot;\r\n}\r\n<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\u3002\u5148\u8bbe\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a salt\u3002<\/p>\n<pre class=\"brush: javascript;\">var Hashids = require(&quot;hashids&quot;),\r\n    hashids = new Hashids(&quot;this is my salt\u201d);\r\n<\/pre>\n<p>\u7136\u540e\u5bf9\u6570\u5b57(\u51c6\u786e\u6765\u8bf4\u662f\u6570\u5b57\u6570\u7ec4)\u8fdb\u884c\u7f16\u7801<\/p>\n<pre class=\"brush: javascript;\">var hash = hashids.encrypt(12345);\r\n\/\/ Nkk9\r\n\r\nvar hash = hashids.encrypt(683, 94108, 123, 5);\r\n\/\/ aBMswoO2UB3Sj\r\n<\/pre>\n<p>\u5bf9\u6570\u7ec4\u7f16\u7801\u53ef\u4ee5\u6709\u5f88\u6709\u8da3\u7684\u7528\u6cd5\uff0c\u6bd4\u5982\u9700\u8981\u5728\u4e00\u4e2a\u5206\u5e03\u5f0f\u7684\u73af\u5883\u91cc\u4f7f\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u7f16\u53f7 + \u5e8f\u5217\u6570\u5b57\u7ec4\u5408\u6570\u7ec4\u7136\u540e\u518d\u7f16\u7801\u3002<\/p>\n<p>\u6709\u7f16\u7801\u5c31\u6709\u89e3\u7801<\/p>\n<pre class=\"brush: javascript;\">var numbers = hashids.decrypt(&quot;NkK9\u201d);\r\n\/\/ [12345]\r\n<\/pre>\n<p>\u7f16\u7801\u7684\u8f93\u51fa\u4f1a\u968f\u7740\u6570\u5b57\u7684\u589e\u5927\u800c\u53d8\u957f\uff0c\u4e3a\u4e86\u8fbe\u5230\u8db3\u591f\u6df7\u6dc6\uff0c\u5b83\u53ef\u4ee5\u6307\u5b9a\u6700\u5c11\u957f\u5ea6\u3002<\/p>\n<pre class=\"brush: javascript;\">hashids = new Hashids(&quot;this is my salt&quot;, 8);\r\n\r\nvar hash = hashids.encrypt(1);\r\n\/\/ gB0NV05e\r\n<\/pre>\n<p>\u8fd8\u6709\u5176\u5b83\u9009\u9879\uff0c\u6bd4\u5982\u63a7\u5236\u8f93\u51fa\u6240\u4f7f\u7528\u7684\u5b57\u7b26\uff0c\u7f16\u7801 MongoDB \u7684 ObjectId \u7b49\u7b49\u3002<\/p>\n<p>\u4e0a\u9762\u4ecb\u7ecd\u7684\u4e24\u4e2a\u9879\u76ee\uff0c\u5404\u6709\u5404\u4f18\u7f3a\u70b9\uff0c\u4e0d\u8fc7\u76ee\u7684\u662f\u4e00\u81f4\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u7528\u3002\u4e24\u4e2a\u9879\u76ee\u5728\u5176\u5b83\u8bed\u8a00\u4e0a\u4e5f\u6709\u7c7b\u4f3c\u7684\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u5e38\u6570\u636e\u5e93\u53ef\u4ee5\u751f\u6210\u552f\u4e00\u7684 ID\uff0c\u6700\u591a\u7684\u5c31\u662f\u6570\u5b57\u5e8f\u5217\uff0c\u4e5f\u6709\u50cf MongoDB \u8fd9\u6837\u4ea7\u751f\u7ec4\u5408\u5e8f\u5217\u7684\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u5f62\u5f0f\u7684 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/leegorous.net\/blog\/2014\/06\/08\/how-to-generate-unpredictable-unique-id\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5982\u4f55\u751f\u6210\u552f\u4e00\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684 ID\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[102,35],"tags":[95,93,94],"_links":{"self":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1478"}],"collection":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/comments?post=1478"}],"version-history":[{"count":6,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1478\/revisions"}],"predecessor-version":[{"id":1486,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1478\/revisions\/1486"}],"wp:attachment":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/media?parent=1478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/categories?post=1478"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/tags?post=1478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}