{"id":22708,"date":"2024-11-21T12:02:24","date_gmt":"2024-11-21T04:02:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22708\/"},"modified":"2024-11-21T12:02:24","modified_gmt":"2024-11-21T04:02:24","slug":"thinkphp%e5%8a%a0%e5%af%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22708\/","title":{"rendered":"thinkphp\u52a0\u5bc6\u65b9\u6cd5"},"content":{"rendered":"<p>\u6700\u8fd1\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u7f51\u7ad9\u5f00\u59cb\u4f7f\u7528thinkphp\u6846\u67b6\u6765\u5f00\u53d1\u548c\u7ef4\u62a4\u81ea\u5df1\u7684\u7f51\u7ad9\u3002\u8fd9\u662f\u56e0\u4e3athinkphp\u6846\u67b6\u5177\u6709\u5f88\u591a\u4f18\u70b9\uff0c\u6bd4\u5982\u6613\u4e8e\u5b66\u4e60\u3001\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\u3001\u5b89\u5168\u6027\u9ad8\u7b49\u7b49\u3002\u7136\u800c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u5b89\u5168\u7684\u95ee\u9898\uff0c\u5176\u4e2d\u4e00\u9879\u91cd\u8981\u7684\u4efb\u52a1\u5c31\u662f\u52a0\u5bc6\u6570\u636e\u3002\u672c\u6587\u5c06\u4f1a\u63a2\u8ba8thinkphp\u4e2d\u52a0\u5bc6\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570<\/li>\n<\/ol>\n<p>thinkphp\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570\u4e3athinkhelperHash::make($str)\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a0\u5bc6\u5b57\u7b26\u4e32\uff08$str\uff09\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>$str = 'Hello World';\n$encrypt_str = thinkhelperHash::make($str);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32$encrypt_str\uff0c\u5b83\u662f\u4e00\u4e2a40\u4f4d\u7684\u968f\u673a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u662f\u4e00\u4e2a\u57fa\u4e8e\u54c8\u5e0c\u7b97\u6cd5\u7684\u7b80\u5355\u52a0\u5bc6\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\u662f\u8db3\u591f\u7684\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/li>\n<\/ol>\n<p>\u9664\u4e86\u7cfb\u7edf\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570\u4e4b\u5916\uff0cthinkphp\u8fd8\u652f\u6301\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u52a0\u5bc6\u3002\u6bd4\u5982\u53ef\u4ee5\u4f7f\u7528PHP\u52a0\u5bc6\u6269\u5c55\u5e93sodium\u6765\u8fdb\u884c\u6570\u636e\u52a0\u5bc6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>\u9996\u5148\u9700\u8981\u5b89\u88c5sodium\u6269\u5c55\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4\uff1a<\/p>\n<pre>pecl install libsodium<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5728php.ini\u6587\u4ef6\u4e2d\u52a0\u5165\u4e00\u884c\uff1a<\/p>\n<pre>extension=sodium.so<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u52a0\u5bc6\u51fd\u6570\u8fdb\u884c\u52a0\u5bc6\uff0c\u5728sodium\u5e93\u4e2d\uff0c\u52a0\u5bc6\u51fd\u6570\u4e3asodium_crypto_secretbox($str, $nonce, $secretKey)\u3002<\/p>\n<p>\u5176\u4e2d\uff0c$str\u4e3a\u9700\u8981\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32\uff0c$nonce\u4e3a\u552f\u4e00\u7684\u5b57\u7b26\u4e32\uff0c$secretKey\u4e3a\u5bc6\u94a5\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>$str = 'Hello World';\n$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);\n$secretKey = sodium_crypto_secretbox_keygen();\n$encrypt_str = sodium_crypto_secretbox($str, $nonce, $secretKey);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u52a0\u5bc6\u540e\u7684$encrypt_str\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u548cnonce\u6765\u8fdb\u884c\u89e3\u5bc6\u3002<\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49\u52a0\u5bc6\u65b9\u6cd5<\/li>\n<\/ol>\n<p>\u5982\u679c\u60f3\u8981\u66f4\u52a0\u5b89\u5168\u7684\u52a0\u5bc6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u52a0\u5bc6\u51fd\u6570\u6216\u8005\u8c03\u7528\u7b2c\u4e09\u65b9\u7684\u5e93\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<p>\u4f7f\u7528\u81ea\u5b9a\u4e49\u52a0\u5bc6\u51fd\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>function my_encrypt($data, $key){\n    $iv = mcrypt_create_iv(\n        mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),\n        MCRYPT_DEV_URANDOM\n    );\n\n    $encrypted = base64_encode(\n        $iv .\n        mcrypt_encrypt(\n            MCRYPT_RIJNDAEL_128,\n            hash('sha256', $key, true),\n            $data,\n            MCRYPT_MODE_CBC,\n            $iv\n        )\n    );\n    return $encrypted;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u57fa\u4e8eaes-256-ctr\u7b97\u6cd5\u7684\u52a0\u5bc6\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5bc6\u94a5\u548c\u52a0\u5bc6\u6570\u636e\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cmcrypt\u5df2\u7ecf\u5728PHP 7.2\u7248\u672c\u4e2d\u88ab\u5e9f\u5f03\uff0c\u56e0\u6b64\u9700\u8981\u81ea\u884c\u5bfb\u627e\u66ff\u4ee3\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u6570\u636e\u5b89\u5168\u662f\u4e00\u4e2a\u7f51\u7ad9\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5fc5\u987b\u8981\u8003\u8651\u7684\u95ee\u9898\u3002\u5728thinkphp\u6846\u67b6\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570\u3001\u7b2c\u4e09\u65b9\u5e93\u6216\u8005\u81ea\u5b9a\u4e49\u52a0\u5bc6\u51fd\u6570\uff0c\u6765\u4fdd\u969c\u6570\u636e\u7684\u5b89\u5168\u6027\u3002\u4f46\u662f\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u52a0\u5bc6\u65b9\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6548\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u52a0\u5bc6\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u7f51\u7ad9\u5f00\u59cb\u4f7f\u7528thinkphp\u6846\u67b6\u6765\u5f00\u53d1\u548c\u7ef4\u62a4\u81ea\u5df1\u7684\u7f51\u7ad9\u3002\u8fd9\u662f\u56e0\u4e3athinkphp\u6846\u67b6\u5177\u6709\u5f88\u591a\u4f18\u70b9\uff0c\u6bd4\u5982\u6613\u4e8e\u5b66\u4e60\u3001\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\u3001\u5b89\u5168\u6027\u9ad8\u7b49\u7b49\u3002\u7136\u800c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u5b89\u5168\u7684\u95ee\u9898\uff0c\u5176\u4e2d\u4e00\u9879\u91cd\u8981\u7684\u4efb\u52a1\u5c31\u662f\u52a0\u5bc6\u6570\u636e\u3002\u672c\u6587\u5c06\u4f1a\u63a2\u8ba8thinkphp\u4e2d\u52a0\u5bc6\u6570\u636e\u7684\u65b9\u6cd5\u3002 \u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570 thinkphp\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570\u4e3athinkhelperHash::make($str)\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a0\u5bc6\u5b57\u7b26\u4e32\uff08$str\uff09\u3002 \u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a $str = &#8216;Hello World&#8217;; $encrypt_str = thinkhelperHash::make($str); \u767b\u5f55\u540e\u590d\u5236 \u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32$encrypt_str\uff0c\u5b83\u662f\u4e00\u4e2a40\u4f4d\u7684\u968f\u673a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u662f\u4e00\u4e2a\u57fa\u4e8e\u54c8\u5e0c\u7b97\u6cd5\u7684\u7b80\u5355\u52a0\u5bc6\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\u662f\u8db3\u591f\u7684\u3002 \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 \u9664\u4e86\u7cfb\u7edf\u81ea\u5e26\u7684\u52a0\u5bc6\u51fd\u6570\u4e4b\u5916\uff0cthinkphp\u8fd8\u652f\u6301\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u52a0\u5bc6\u3002\u6bd4\u5982\u53ef\u4ee5\u4f7f\u7528PHP\u52a0\u5bc6\u6269\u5c55\u5e93sodium\u6765\u8fdb\u884c\u6570\u636e\u52a0\u5bc6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a \u9996\u5148\u9700\u8981\u5b89\u88c5sodium\u6269\u5c55\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4\uff1a pecl install libsodium \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5728php.ini\u6587\u4ef6\u4e2d\u52a0\u5165\u4e00\u884c\uff1a extension=sodium.so \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u52a0\u5bc6\u51fd\u6570\u8fdb\u884c\u52a0\u5bc6\uff0c\u5728sodium\u5e93\u4e2d\uff0c\u52a0\u5bc6\u51fd\u6570\u4e3asodium_crypto_secretbox($str, $nonce, $secretKey)\u3002 \u5176\u4e2d\uff0c$str\u4e3a\u9700\u8981\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32\uff0c$nonce\u4e3a\u552f\u4e00\u7684\u5b57\u7b26\u4e32\uff0c$secretKey\u4e3a\u5bc6\u94a5\u3002 \u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a $str = &#8216;Hello World&#8217;; $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $secretKey = sodium_crypto_secretbox_keygen(); $encrypt_str = sodium_crypto_secretbox($str, $nonce, $secretKey); \u767b\u5f55\u540e\u590d\u5236 \u52a0\u5bc6\u540e\u7684$encrypt_str\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u548cnonce\u6765\u8fdb\u884c\u89e3\u5bc6\u3002 \u81ea\u5b9a\u4e49\u52a0\u5bc6\u65b9\u6cd5 \u5982\u679c\u60f3\u8981\u66f4\u52a0\u5b89\u5168\u7684\u52a0\u5bc6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u52a0\u5bc6\u51fd\u6570\u6216\u8005\u8c03\u7528\u7b2c\u4e09\u65b9\u7684\u5e93\u8fdb\u884c\u52a0\u5bc6\u3002 \u4f7f\u7528\u81ea\u5b9a\u4e49\u52a0\u5bc6\u51fd\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a function my_encrypt($data, $key){ $iv = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22708","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22708"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22708\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}