{"id":2919,"date":"2025-03-18T00:38:53","date_gmt":"2025-03-17T16:38:53","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2919"},"modified":"2025-03-18T00:38:53","modified_gmt":"2025-03-17T16:38:53","slug":"python-hashlib-%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/18\/python-hashlib-%e6%a8%a1%e5%9d%97\/","title":{"rendered":"Python hashlib \u6a21\u5757"},"content":{"rendered":"\n<p><code>hashlib<\/code> \u662f Python \u7684\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u5bf9\u591a\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-256 \u7b49\u3002\u54c8\u5e0c\u7b97\u6cd5\u5e38\u7528\u4e8e\u6570\u636e\u7684\u5b8c\u6574\u6027\u6821\u9a8c\u3001\u5bc6\u7801\u5b58\u50a8\u7b49\u573a\u666f\uff0c\u901a\u8fc7\u5c06\u6570\u636e\u6620\u5c04\u4e3a\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\uff0c\u5e2e\u52a9\u6211\u4eec\u5224\u65ad\u6570\u636e\u662f\u5426\u53d1\u751f\u53d8\u5316\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u4ec0\u4e48\u662f\u54c8\u5e0c\u51fd\u6570\uff1f<\/strong><\/h2>\n\n\n\n<p>\u54c8\u5e0c\u51fd\u6570\u662f\u4e00\u79cd\u5c06\u4efb\u610f\u5927\u5c0f\u7684\u8f93\u5165\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\uff09\u8f6c\u6362\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u8f93\u51fa\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u6570\u5b57\u6216\u5b57\u8282\u4e32\uff09\u7684\u51fd\u6570\u3002\u54c8\u5e0c\u503c\uff08\u6216\u6458\u8981\uff09\u662f\u6570\u636e\u7684\u201c\u6307\u7eb9\u201d\uff0c\u7528\u4e8e\u5feb\u901f\u6bd4\u8f83\u4e0d\u540c\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong><code>hashlib<\/code> \u6a21\u5757\u6982\u8ff0<\/strong><\/h2>\n\n\n\n<p><code>hashlib<\/code> \u6a21\u5757\u4e3a Python \u63d0\u4f9b\u4e86\u8bbf\u95ee\u591a\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u63a5\u53e3\u3002\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MD5<\/strong>\uff1a\u8f93\u51fa128\u4f4d\u54c8\u5e0c\u503c\uff0c\u901a\u5e38\u8868\u793a\u4e3a32\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u3002<\/li>\n\n\n\n<li><strong>SHA-1<\/strong>\uff1a\u8f93\u51fa160\u4f4d\u54c8\u5e0c\u503c\uff0c\u8868\u793a\u4e3a40\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u3002<\/li>\n\n\n\n<li><strong>SHA-224<\/strong>\u3001<strong>SHA-256<\/strong>\u3001<strong>SHA-384<\/strong>\u3001<strong>SHA-512<\/strong>\uff1a\u8fd9\u4e9b\u7b97\u6cd5\u5c5e\u4e8e SHA \u7cfb\u5217\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u957f\u5ea6\u5206\u522b\u4e3a224\u3001256\u3001384\u3001512\u4f4d\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u5e38\u7528\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b9e\u73b0<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 <strong>MD5 \u54c8\u5e0c<\/strong><\/h3>\n\n\n\n<p>MD5 \u662f\u4e00\u79cd\u5e38\u7528\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u8f93\u51fa128\u4f4d\u7684\u54c8\u5e0c\u503c\u3002\u867d\u7136\u5b83\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u56e0\u4e3a\u5b58\u5728\u78b0\u649e\u6f0f\u6d1e\uff08\u5373\u4e0d\u540c\u8f93\u5165\u53ef\u80fd\u4ea7\u751f\u76f8\u540c\u54c8\u5e0c\u503c\uff09\uff0c\u5728\u73b0\u4ee3\u5e94\u7528\u4e2d\u5df2\u7ecf\u4e0d\u518d\u63a8\u8350\u7528\u4e8e\u5b89\u5168\u76f8\u5173\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\n# \u521b\u5efa MD5 \u54c8\u5e0c\u5bf9\u8c61\nmd5_hash = hashlib.md5()\n\n# \u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\uff0c\u4f20\u5165\u9700\u8981\u8ba1\u7b97\u54c8\u5e0c\u7684\u6570\u636e\uff08\u5b57\u8282\u7c7b\u578b\uff09\nmd5_hash.update(b\"hello world\")\n\n# \u83b7\u53d6 MD5 \u54c8\u5e0c\u503c\uff0c\u8fd4\u56de\u5341\u516d\u8fdb\u5236\u8868\u793a\nprint(md5_hash.hexdigest())  # \u8f93\u51fa\uff1ab94d27b9934d3e08a52e52d7da7dabfa<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 <strong>SHA-1 \u54c8\u5e0c<\/strong><\/h3>\n\n\n\n<p>SHA-1 \u8f93\u51fa160\u4f4d\u7684\u54c8\u5e0c\u503c\uff0c\u8868\u793a\u4e3a40\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u3002\u867d\u7136 SHA-1 \u6bd4 MD5 \u66f4\u5b89\u5168\uff0c\u4f46\u4e5f\u5b58\u5728\u78b0\u649e\u6f0f\u6d1e\uff0c\u56e0\u6b64\u5728\u8bb8\u591a\u5b89\u5168\u5e94\u7528\u4e2d\u4e5f\u9010\u6e10\u88ab\u5f03\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\n# \u521b\u5efa SHA-1 \u54c8\u5e0c\u5bf9\u8c61\nsha1_hash = hashlib.sha1()\n\n# \u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\nsha1_hash.update(b\"hello world\")\n\n# \u83b7\u53d6 SHA-1 \u54c8\u5e0c\u503c\nprint(sha1_hash.hexdigest())  # \u8f93\u51fa\uff1a2ef7bde608ce5404e97d5f042f95f89f1c232871<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 <strong>SHA-256 \u54c8\u5e0c<\/strong><\/h3>\n\n\n\n<p>SHA-256 \u8f93\u51fa256\u4f4d\u7684\u54c8\u5e0c\u503c\uff0c\u901a\u5e38\u8868\u793a\u4e3a64\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u3002\u5b83\u6bd4 MD5 \u548c SHA-1 \u66f4\u52a0\u5b89\u5168\uff0c\u76ee\u524d\u88ab\u5e7f\u6cdb\u7528\u4e8e\u5bc6\u7801\u4fdd\u62a4\u548c\u6570\u636e\u5b8c\u6574\u6027\u6821\u9a8c\u7b49\u9886\u57df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\n# \u521b\u5efa SHA-256 \u54c8\u5e0c\u5bf9\u8c61\nsha256_hash = hashlib.sha256()\n\n# \u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\nsha256_hash.update(b\"hello world\")\n\n# \u83b7\u53d6 SHA-256 \u54c8\u5e0c\u503c\nprint(sha256_hash.hexdigest())  # \u8f93\u51fa\uff1aa591a6d40bf420404a011733cfb7b190d62c65bf0bcda115a3e8c6b64c79f47b<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 <strong>SHA-512 \u54c8\u5e0c<\/strong><\/h3>\n\n\n\n<p>SHA-512 \u8f93\u51fa512\u4f4d\u7684\u54c8\u5e0c\u503c\uff0c\u8868\u793a\u4e3a128\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u3002\u5b83\u7684\u8ba1\u7b97\u901f\u5ea6\u6bd4 SHA-256 \u6162\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u957f\u7684\u54c8\u5e0c\u503c\uff0c\u9002\u7528\u4e8e\u5bf9\u5b89\u5168\u6027\u8981\u6c42\u66f4\u9ad8\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\n# \u521b\u5efa SHA-512 \u54c8\u5e0c\u5bf9\u8c61\nsha512_hash = hashlib.sha512()\n\n# \u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\nsha512_hash.update(b\"hello world\")\n\n# \u83b7\u53d6 SHA-512 \u54c8\u5e0c\u503c\nprint(sha512_hash.hexdigest())  # \u8f93\u51fa\uff1a861844d6704e8573fec34d967e20bcfe57b18d68249756d7d47fece184121a24e6a3a2bbdb7554ac7b3c538e8f4d9a8b957049d8cf503788ff5709c8a22e0b8<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u9010\u6b65\u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61<\/strong><\/h2>\n\n\n\n<p>\u54c8\u5e0c\u5bf9\u8c61\u5728\u66f4\u65b0\u65f6\uff0c\u53ef\u4ee5\u9010\u6b65\u5c06\u6570\u636e\u4f20\u5165\uff0c\u9002\u7528\u4e8e\u8f83\u5927\u6570\u636e\u7684\u54c8\u5e0c\u8ba1\u7b97\u3002\u6bcf\u6b21\u66f4\u65b0\u65f6\uff0c\u6570\u636e\u4f1a\u88ab\u8ffd\u52a0\u5230\u54c8\u5e0c\u5bf9\u8c61\u4e2d\uff0c\u76f4\u5230\u6700\u7ec8\u8ba1\u7b97\u51fa\u54c8\u5e0c\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\n# \u521b\u5efa\u4e00\u4e2a SHA-256 \u54c8\u5e0c\u5bf9\u8c61\nsha256_hash = hashlib.sha256()\n\n# \u9010\u6b65\u66f4\u65b0\u54c8\u5e0c\u503c\nsha256_hash.update(b\"hello\")\nsha256_hash.update(b\" world\")\n\n# \u83b7\u53d6\u6700\u7ec8\u7684\u54c8\u5e0c\u503c\nprint(sha256_hash.hexdigest())  # \u8f93\u51fa\uff1aa591a6d40bf420404a011733cfb7b190d62c65bf0bcda115a3e8c6b64c79f47b<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u54c8\u5e0c\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\u6216\u9700\u8981\u5904\u7406\u7684\u6587\u4ef6\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>hashlib<\/code> \u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8ba1\u7b97\u54c8\u5e0c\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\n\ndef hash_file(filename):\n    sha256_hash = hashlib.sha256()\n\n    with open(filename, \"rb\") as f:\n        # \u5206\u5757\u8bfb\u53d6\u6587\u4ef6\uff0c\u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\n        for byte_block in iter(lambda: f.read(4096), b\"\"):\n            sha256_hash.update(byte_block)\n\n    return sha256_hash.hexdigest()\n\n# \u8ba1\u7b97\u67d0\u4e2a\u6587\u4ef6\u7684 SHA-256 \u54c8\u5e0c\u503c\nprint(hash_file(\"large_file.txt\"))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong><code>hashlib<\/code> \u6a21\u5757\u4e2d\u7684\u5e38\u89c1\u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>hashlib.new(name, data=None)<\/code><\/strong>\uff1a\u6839\u636e\u7ed9\u5b9a\u7684\u54c8\u5e0c\u7b97\u6cd5\u540d\u79f0\u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c<code>hashlib.new('sha256')<\/code> \u4f1a\u8fd4\u56de\u4e00\u4e2a SHA-256 \u54c8\u5e0c\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong><code>hexdigest()<\/code><\/strong>\uff1a\u8fd4\u56de\u54c8\u5e0c\u503c\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong><code>digest()<\/code><\/strong>\uff1a\u8fd4\u56de\u54c8\u5e0c\u503c\u7684\u5b57\u8282\u4e32\uff08\u6bd4 <code>hexdigest()<\/code> \u66f4\u7d27\u51d1\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u54c8\u5e0c\u78b0\u649e<\/strong>\uff1a\u5373\u4f7f\u4e0d\u540c\u7684\u6570\u636e\u53ef\u80fd\u4f1a\u5f97\u5230\u76f8\u540c\u7684\u54c8\u5e0c\u503c\uff08\u8fd9\u88ab\u79f0\u4e3a\u78b0\u649e\uff09\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u907f\u514d\u4f7f\u7528\u5982 MD5 \u548c SHA-1 \u7b49\u4e0d\u518d\u88ab\u63a8\u8350\u7684\u7b97\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u9006\u6027<\/strong>\uff1a\u54c8\u5e0c\u662f\u4e0d\u53ef\u9006\u7684\uff0c\u5373\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u6062\u590d\u539f\u59cb\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p><code>hashlib<\/code> \u6a21\u5757\u4e3a Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u5bc6\u7801\u5b66\u3001\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\u7b49\u9886\u57df\u3002\u4f60\u53ef\u4ee5\u7075\u6d3b\u9009\u62e9\u5408\u9002\u7684\u54c8\u5e0c\u7b97\u6cd5\uff08\u5982 SHA-256\uff09\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u5b89\u5168\u9700\u6c42\u3002\u5bf9\u4e8e\u9ad8\u6548\u548c\u5b89\u5168\u7684\u54c8\u5e0c\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528 SHA \u7cfb\u5217\u7b97\u6cd5\uff0c\u7279\u522b\u662f SHA-256 \u6216 SHA-512\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>hashlib \u662f Python \u7684\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u5bf9\u591a\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u652f\u6301\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2920,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2919"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2919\/revisions"}],"predecessor-version":[{"id":2921,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2919\/revisions\/2921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2920"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}