{"id":3534,"date":"2025-06-02T15:15:38","date_gmt":"2025-06-04T07:19:38","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3534"},"modified":"2025-06-04T15:15:38","modified_gmt":"2025-06-04T07:19:38","slug":"%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84text_signature%e5%87%bd%e6%95%b8%ef%bc%9a%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3%e5%ae%83%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84text_signature%e5%87%bd%e6%95%b8%ef%bc%9a%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3%e5%ae%83%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Python \u7684 `text_signature()` \u51fd\u6578\uff1a\u529f\u80fd\u3001\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, text_signature, \u51fd\u6578, \u7a0b\u5f0f\u6559\u5b78, SHA-256\"><\/p>\n<h2>\u4f7f\u7528 Python \u7684 `text_signature()` \u51fd\u6578\u4f86\u7c3d\u7f72\u6587\u5b57<\/h2>\n<p>Python\u662f\u4e00\u500b\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5177\u5099\u8a31\u591a\u5be6\u7528\u7684\u51fd\u6578\uff0c\u80fd\u5920\u7c21\u5316\u958b\u767c\u8005\u7684\u5de5\u4f5c\u6d41\u7a0b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `text_signature()` \u51fd\u6578\u7684\u529f\u80fd\u8207\u61c9\u7528\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>### `text_signature()` \u51fd\u6578\u4ecb\u7d39<\/p>\n<p>`text_signature()` \u51fd\u6578\u7684\u4e3b\u8981\u529f\u80fd\u662f\u70ba\u6587\u5b57\u5275\u5efa\u4e00\u500b\u552f\u4e00\u7684\u6a19\u8b58\u7b26\uff0c\u9019\u500b\u6a19\u8b58\u7b26\u53ef\u4ee5\u7528\u4f86\u9a57\u8b49\u6587\u5b57\u7684\u771f\u5be6\u6027\uff0c\u4e26\u9632\u6b62\u5176\u88ab\u4fee\u6539\u3002\u9019\u5c0d\u65bc\u9700\u8981\u8ffd\u6eaf\u4f86\u6e90\u6216\u6aa2\u67e5\u8cc7\u6599\u5b8c\u6574\u6027\u7684\u61c9\u7528\u5834\u666f\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>### \u4f7f\u7528\u65b9\u6cd5\u8207\u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f `text_signature()` \u51fd\u6578\u7684\u57fa\u672c\u5be6\u73fe\uff0c\u4f7f\u7528\u4e86 SHA-256 \u96dc\u6e4a\u6f14\u7b97\u6cd5\u4f86\u8a08\u7b97\u6587\u5b57\u7684\u552f\u4e00\u6a19\u8b58\u7b26\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport hashlib<\/p>\n<p>def text_signature(text: str) -> str:<br \/>\n    &#8220;&#8221;&#8221;\u4f7f\u7528SHA-256\u6f14\u7b97\u6cd5\u751f\u6210\u6587\u5b57\u7684\u552f\u4e00\u6a19\u8b58\u7b26&#8221;&#8221;&#8221;<br \/>\n    return hashlib.sha256(text.encode(&#8216;utf-8&#8217;)).hexdigest()<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `text_signature` \u7684\u51fd\u6578\uff0c\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u53c3\u6578 `text`\uff0c\u4e26\u8fd4\u56de\u5176 SHA-256 \u7684\u5341\u516d\u9032\u4f4d\u8868\u793a\u3002<\/p>\n<p>#### \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u7c3d\u7f72\u4ee5\u4e0b\u7684\u6587\u5b57\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8216;\u9019\u662f\u4e00\u6bb5\u7bc4\u4f8b\u6587\u5b57\u3002&#8217;<br \/>\nsignature = text_signature(text)<br \/>\nprint(f&#8217;\u6587\u5b57: {text}\\n\u7c3d\u540d: {signature}&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u8ff0\u7a0b\u5f0f\u5f8c\uff0c\u60a8\u5c07\u7372\u5f97\u4e00\u500b\u552f\u4e00\u7684\u6a19\u8b58\u7b26\uff0c\u9019\u500b\u6a19\u8b58\u7b26\u80fd\u5920\u5e6b\u52a9\u60a8\u78ba\u8a8d\u6587\u5b57\u7684\u4f86\u6e90\u53ca\u5176\u5b8c\u6574\u6027\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `text_signature()` \u51fd\u6578\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u500b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **TypeError**: \u78ba\u4fdd\u50b3\u5165\u7684\u53c3\u6578\u70ba\u5b57\u4e32\u985e\u578b\u3002<br \/>\n&#8211; **UnicodeEncodeError**: \u78ba\u4fdd\u6587\u5b57\u80fd\u5920\u6b63\u78ba\u7de8\u78bc\u70ba UTF-8\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`text_signature()` \u51fd\u6578\u53ef\u4ee5\u61c9\u7528\u65bc\u591a\u7a2e\u5834\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u6578\u4f4d\u7c3d\u540d**: \u78ba\u4fdd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u8207\u4f86\u6e90\u3002<br \/>\n&#8211; **\u8cc7\u6599\u5eab\u8a18\u9304**: \u70ba\u91cd\u8981\u7684\u6578\u64da\u689d\u76ee\u5275\u5efa\u7c3d\u540d\uff0c\u4ee5\u4fbf\u65e5\u5f8c\u9a57\u8b49\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc) \u4e86\u89e3\u66f4\u591a\u3002<\/p>\n<p>### \u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/p>\n<p>**Q1: `text_signature()` \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: \u8fd4\u56de\u503c\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u8868\u793a\u8f38\u5165\u6587\u5b57\u7684 SHA-256 \u96dc\u6e4a\u503c\uff0c\u53ef\u4ee5\u7528\u4f86\u552f\u4e00\u6a19\u8b58\u8a72\u6587\u5b57\u3002<\/p>\n<p>**Q2: \u6211\u53ef\u4ee5\u7528\u9019\u500b\u51fd\u6578\u4f86\u7c3d\u7f72\u9577\u6587\u672c\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c`text_signature()` \u51fd\u6578\u53ef\u4ee5\u8655\u7406\u4efb\u610f\u9577\u5ea6\u7684\u6587\u672c\uff0c\u53ea\u8981\u78ba\u4fdd\u4f7f\u7528 UTF-8 \u7de8\u78bc\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u78ba\u4fdd\u751f\u6210\u7684\u7c3d\u540d\u4e0d\u6703\u88ab\u7be1\u6539\uff1f**<br \/>\nA3: \u751f\u6210\u7684\u7c3d\u540d\u662f\u4f9d\u64da\u539f\u59cb\u6587\u5b57\u8a08\u7b97\u7684\uff0c\u53ea\u8981\u539f\u59cb\u6587\u5b57\u4e0d\u8b8a\uff0c\u7c3d\u540d\u5c31\u4e0d\u6703\u6539\u8b8a\uff0c\u9019\u6a23\u53ef\u4ee5\u9a57\u8b49\u6587\u5b57\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aPython\u4e2d\u7684text_signature()\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u64cd\u4f5c\u6587\u672c\uff0c\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39\u5b83\u7684\u529f\u80fd\u8207\u61c9\u7528\uff0c\u8b93\u958b\u767c\u8005\u66f4\u52a0\u4e86\u89e3\u5b83\u7684\u50f9\u503c\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-V0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3534"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3534\/revisions"}],"predecessor-version":[{"id":3535,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3534\/revisions\/3535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}