{"id":222546,"date":"2021-07-24T10:05:18","date_gmt":"2021-07-24T02:05:18","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=222546"},"modified":"2021-07-15T09:06:45","modified_gmt":"2021-07-15T01:06:45","slug":"python-model-getpass","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-model-getpass.html","title":{"rendered":"\u5982\u4f55\u8ba9Python\u4e0d\u56de\u663e\u83b7\u53d6\u5bc6\u7801\u8f93\u5165"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5bc6\u7801\u5bf9\u7a0b\u5e8f\u5458\u6765\u8bf4\u5c24\u5176\u91cd\u8981\u3002\u4f60\u4e0d\u5e94\u8be5\u5728\u4e0d\u52a0\u5bc6\u7684\u60c5\u51b5\u4e0b\u5b58\u50a8\u5b83\u4eec\uff0c\u800c\u4e14\u4f60\u4e5f\u4e0d\u5e94\u8be5\u5728\u7528\u6237\u8f93\u5165\u5bc6\u7801\u7684\u65f6\u5019\u663e\u793a\u51fa\u8f93\u5165\u7684\u5185\u5bb9\u3002\u5f53\u6211\u51b3\u5b9a\u8981\u63d0\u9ad8\u6211\u7684\u7b14\u8bb0\u672c\u7535\u8111\u7684\u5b89\u5168\u6027\u65f6\uff0c\u8fd9\u5bf9\u6211\u6765\u8bf4\u53d8\u5f97\u7279\u522b\u91cd\u8981\u3002\u6211\u5bf9\u6211\u7684\u5bb6\u76ee\u5f55\u8fdb\u884c\u4e86\u52a0\u5bc6\uff0c\u4f46\u5f53\u6211\u767b\u5f55\u540e\uff0c\u4efb\u4f55\u4ee5\u7eaf\u6587\u672c\u5f62\u5f0f\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5bc6\u7801\u90fd\u6709\u53ef\u80fd\u66b4\u9732\u5728\u5077\u7aa5\u8005\u9762\u524d\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u7528 GPG \u548c Python \u7684 getpass \u6a21\u5757\u7ed9\u4f60\u7684\u5bc6\u7801\u591a\u4e00\u5c42\u5b89\u5168\u4fdd\u969c\u3002<\/p>\n

\"\"<\/p>\n

\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a Mutt \u7684\u5e94\u7528\u4f5c\u4e3a\u6211\u7684\u7535\u5b50\u90ae\u4ef6\u5ba2\u6237\u7aef\u3002\u5b83\u53ef\u4ee5\u8ba9\u6211\u5728\u6211\u7684 Linux \u7ec8\u7aef\u4e2d\u9605\u8bfb\u548c\u64b0\u5199\u7535\u5b50\u90ae\u4ef6\uff0c\u4f46\u901a\u5e38\u5b83\u5e0c\u671b\u5728\u5176\u914d\u7f6e\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2a\u5bc6\u7801\u3002\u6211\u9650\u5236\u4e86\u6211\u7684 Mutt \u914d\u7f6e\u6587\u4ef6\u7684\u6743\u9650\uff0c\u4ee5\u4fbf\u53ea\u6709\u6211\u53ef\u4ee5\u770b\u5230\u5b83\uff0c\u6211\u662f\u6211\u7684\u7b14\u8bb0\u672c\u7535\u8111\u7684\u552f\u4e00\u7528\u6237\uff0c\u6240\u4ee5\u6211\u5e76\u4e0d\u771f\u7684\u62c5\u5fc3\u7ecf\u8fc7\u8ba4\u8bc1\u7684\u7528\u6237\u4f1a\u65e0\u610f\u4e2d\u770b\u5230\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u76f8\u53cd\uff0c\u6211\u60f3\u4fdd\u62a4\u81ea\u5df1\uff0c\u65e0\u8bba\u662f\u4e3a\u4e86\u5439\u5618\u8fd8\u662f\u4e3a\u4e86\u7248\u672c\u63a7\u5236\uff0c\u4e0d\u81f3\u4e8e\u5fc3\u4e0d\u5728\u7109\u5730\u628a\u6211\u7684\u914d\u7f6e\u53d1\u5e03\u5230\u7f51\u4e0a\uff0c\u628a\u6211\u7684\u5bc6\u7801\u66b4\u9732\u4e86\u3002\u6b64\u5916\uff0c\u867d\u7136\u6211\u4e0d\u5e0c\u671b\u6211\u7684\u7cfb\u7edf\u4e0a\u6709\u4e0d\u53d7\u6b22\u8fce\u7684\u5ba2\u4eba\uff0c\u4f46\u6211\u786e\u5b9e\u60f3\u786e\u4fdd\u5165\u4fb5\u8005\u4e0d\u80fd\u901a\u8fc7\u5bf9\u6211\u7684\u914d\u7f6e\u4e0a\u8fd0\u884c cat \u5c31\u83b7\u5f97\u6211\u7684\u5bc6\u7801\u3002<\/p>\n

Python GnuPG<\/strong><\/div>\n

Python \u6a21\u5757 python-gnupg \u662f gpg \u5e94\u7528\u7684\u4e00\u4e2a Python \u5c01\u88c5\u3002\u8be5\u6a21\u5757\u7684\u540d\u5b57\u662f python-gnupg\uff0c\u4f60\u4e0d\u8981\u628a\u5b83\u548c\u4e00\u4e2a\u53eb\u505a gnupg \u7684\u6a21\u5757\u6df7\u6dc6\u3002<\/p>\n

GnuPG(GPG) \u662f Linux \u7684\u9ed8\u8ba4\u52a0\u5bc6\u7cfb\u7edf\uff0c\u6211\u4ece 2009 \u5e74\u5de6\u53f3\u5f00\u59cb\u4f7f\u7528\u5b83\u3002\u6211\u5bf9\u5b83\u5f88\u719f\u6089\uff0c\u5bf9\u5b83\u7684\u5b89\u5168\u6027\u6709\u5f88\u9ad8\u7684\u4fe1\u4efb\u3002<\/p>\n

\u6211\u51b3\u5b9a\u5c06\u6211\u7684\u5bc6\u7801\u8f93\u5165 Mutt \u7684\u6700\u597d\u65b9\u6cd5\u662f\u5c06\u6211\u7684\u5bc6\u7801\u5b58\u50a8\u5728\u4e00\u4e2a\u52a0\u5bc6\u7684 GPG \u6587\u4ef6\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u63d0\u793a\u6211\u7684 GPG \u5bc6\u7801\u6765\u89e3\u9501\u8fd9\u4e2a\u52a0\u5bc6\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5bc6\u7801\u4ea4\u7ed9 Mutt(\u5b9e\u9645\u4e0a\u662f\u4ea4\u7ed9 offlineimap \u547d\u4ee4\uff0c\u6211\u7528\u5b83\u6765\u540c\u6b65\u6211\u7684\u7b14\u8bb0\u672c\u548c\u7535\u5b50\u90ae\u4ef6\u670d\u52a1\u5668)\u3002<\/p>\n

\u7528 Python \u83b7\u53d6\u7528\u6237\u8f93\u5165 \u662f\u975e\u5e38\u5bb9\u6613\u7684\u3002\u5bf9 input \u8fdb\u884c\u8c03\u7528\uff0c\u65e0\u8bba\u7528\u6237\u8f93\u5165\u4ec0\u4e48\uff0c\u90fd\u4f1a\u88ab\u5b58\u50a8\u4e3a\u4e00\u4e2a\u53d8\u91cf\uff1a<\/p>\n

print(\"Enter password: \") \r\nmyinput = input() \r\nprint(\"You entered: \", myinput)<\/pre>\n

\u6211\u7684\u95ee\u9898\u662f\uff0c\u5f53\u6211\u6839\u636e\u5bc6\u7801\u63d0\u793a\u5728\u7ec8\u7aef\u4e0a\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u6211\u6240\u8f93\u5165\u7684\u6240\u6709\u5185\u5bb9\u5bf9\u4efb\u4f55\u4ece\u6211\u80a9\u8180\u4e0a\u770b\u8fc7\u53bb\u6216\u6eda\u52a8\u6211\u7684\u7ec8\u7aef\u5386\u53f2\u7684\u4eba\u6765\u8bf4\u90fd\u662f\u53ef\u89c1\u7684\uff1a<\/p>\n

$ .\/test.py \r\nEnter password: my-Complex-Passphrase<\/pre>\n
\u7528 getpass \u8f93\u5165\u4e0d\u53ef\u89c1\u5bc6\u7801<\/strong><\/div>\n

\u6b63\u5982\u901a\u5e38\u7684\u60c5\u51b5\u4e00\u6837\uff0c\u6709\u4e00\u4e2a Python \u6a21\u5757\u5df2\u7ecf\u89e3\u51b3\u4e86\u6211\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u6a21\u5757\u662f getpass4\uff0c\u4ece\u7528\u6237\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5b83\u7684\u884c\u4e3a\u548c input \u5b8c\u5168\u4e00\u6837\uff0c\u53ea\u662f\u4e0d\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n

\u4f60\u53ef\u4ee5\u7528 pip \u5b89\u88c5\u8fd9\u4e24\u4e2a\u6a21\u5757\uff1a<\/p>\n

$ python -m pip install --user python-gnupg getpass4 \r\n<\/pre>\n

\u4e0b\u9762\u662f\u6211\u7684 Python \u811a\u672c\uff0c\u7528\u4e8e\u521b\u5efa\u5bc6\u7801\u63d0\u793a\uff1a<\/p>\n

#!\/usr\/bin\/env python \r\n# by Seth Kenlon \r\n# GPLv3 \r\n# install deps: \r\n# python3 -m pip install --user python-gnupg getpass4 \r\nimport gnupg \r\nimport getpass \r\nfrom pathlib import Path \r\ndef get_api_pass(): \r\n  homedir = str(Path.home()) \r\n  gpg = gnupg.GPG(gnupghome=os.path.join(homedir,\".gnupg\"), use_agent=True) \r\n  passwd = getpass.getpass(prompt=\"Enter your GnuPG password: \", stream=None) \r\n  with open(os.path.join(homedir,'.mutt','pass.gpg'), 'rb') as f: \r\n    apipass = (gpg.decrypt_file(f, passphrase=passwd)) \r\n  f.close() \r\n  return str(apipass) \r\n  \r\nif __name__ == \"__main__\": \r\n  apipass = get_api_pass() \r\n  print(apipass)<\/pre>\n

\u5982\u679c\u4f60\u60f3\u8bd5\u8bd5\uff0c\u628a\u6587\u4ef6\u4fdd\u5b58\u4e3a password_prompt.py\u3002\u5982\u679c\u4f60\u4f7f\u7528 offlineimap \u5e76\u60f3\u5728\u4f60\u81ea\u5df1\u7684\u5bc6\u7801\u8f93\u5165\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6848\uff0c\u90a3\u4e48\u628a\u5b83\u4fdd\u5b58\u5230\u67d0\u4e2a\u4f60\u53ef\u4ee5\u5728 .offlineimaprc \u6587\u4ef6\u4e2d\u6307\u5411 offlineimap \u7684\u4f4d\u7f6e(\u6211\u4f7f\u7528 ~\/.mutt\/password_prompt.py)\u3002<\/p>\n

\u6d4b\u8bd5\u5bc6\u7801\u63d0\u793a<\/strong><\/div>\n

\u8981\u67e5\u770b\u811a\u672c\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u4f60\u9996\u5148\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a\u52a0\u5bc6\u6587\u4ef6(\u6211\u5047\u8bbe\u4f60\u5df2\u7ecf\u8bbe\u7f6e\u4e86 GPG)\uff1a<\/p>\n

$ echo \"hello world\" > pass \r\n$ gpg --encrypt pass \r\n$ mv pass.gpg ~\/.mutt\/pass.gpg \r\n$ rm pass \r\n<\/pre>\n

\u73b0\u5728\u8fd0\u884c Python \u811a\u672c\uff1a<\/p>\n

$ python ~\/.mutt\/password_prompt.py \r\nEnter your GPG password: \r\nhello world<\/pre>\n

\u5f53\u4f60\u8f93\u5165\u65f6\u6ca1\u6709\u4efb\u4f55\u663e\u793a\uff0c\u4f46\u53ea\u8981\u4f60\u6b63\u786e\u8f93\u5165 GPG \u53e3\u4ee4\uff0c\u4f60\u5c31\u4f1a\u770b\u5230\u8be5\u6d4b\u8bd5\u4fe1\u606f\u3002<\/p>\n

\u5c06\u5bc6\u7801\u63d0\u793a\u7b26\u4e0e offlineimap \u6574\u5408\u8d77\u6765<\/strong><\/div>\n

\u6211\u9700\u8981\u5c06\u6211\u7684\u65b0\u63d0\u793a\u4e0e offlineimap \u547d\u4ee4\u7ed3\u5408\u8d77\u6765\u3002\u6211\u4e3a\u8fd9\u4e2a\u811a\u672c\u9009\u62e9\u4e86 Python\uff0c\u56e0\u4e3a\u6211\u77e5\u9053 offlineimap \u53ef\u4ee5\u5bf9 Python \u7a0b\u5e8f\u8fdb\u884c\u8c03\u7528\u3002\u5982\u679c\u4f60\u662f\u4e00\u4e2a offlineimap \u7528\u6237\uff0c\u4f60\u4f1a\u660e\u767d\u552f\u4e00\u9700\u8981\u7684\u201c\u6574\u5408\u201d\u662f\u5728\u4f60\u7684 .offlineimaprc \u6587\u4ef6\u4e2d\u6539\u53d8\u4e24\u884c\u3002<\/p>\n

\u9996\u5148\uff0c\u6dfb\u52a0\u4e00\u884c\u5f15\u7528 Python \u6587\u4ef6\u7684\u5185\u5bb9:<\/p>\n

pythonfile = ~\/.mutt\/password_prompt.py \r\n<\/pre>\n

\u7136\u540e\u5c06 .offlineimaprc\u4e2d\u7684 remotepasseval \u884c\u6539\u4e3a\u8c03\u7528 password_prompt.py\u4e2d\u7684 get_api_pass() \u51fd\u6570:<\/p>\n

remotepasseval = get_api_pass() \r\n<\/pre>\n

\u914d\u7f6e\u6587\u4ef6\u4e2d\u4e0d\u518d\u6709\u5bc6\u7801!<\/p>\n

\u5b89\u5168\u95ee\u9898<\/strong><\/div>\n

\u5728\u4f60\u7684\u4e2a\u4eba\u7535\u8111\u4e0a\u8003\u8651\u5b89\u5168\u95ee\u9898\u6709\u65f6\u4f1a\u8ba9\u4eba\u89c9\u5f97\u5f88\u504f\u6267\u3002\u4f60\u7684 SSH \u914d\u7f6e\u662f\u5426\u771f\u7684\u9700\u8981\u9650\u5236\u4e3a 600?\u9690\u85cf\u5728\u540d\u4e3a .mutt \u7684\u65e0\u5173\u7d27\u8981\u7684\u7535\u5b50\u90ae\u4ef6\u5bc6\u7801\u771f\u7684\u91cd\u8981\u5417?\u4e5f\u8bb8\u4e0d\u91cd\u8981\u3002<\/p>\n

\u7136\u800c\uff0c\u77e5\u9053\u6211\u6ca1\u6709\u628a\u654f\u611f\u6570\u636e\u6084\u6084\u5730\u85cf\u5728\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u91cc\uff0c\u4f7f\u6211\u66f4\u5bb9\u6613\u628a\u6587\u4ef6\u63d0\u4ea4\u5230\u516c\u5171 Git \u4ed3\u5e93\uff0c\u628a\u7247\u6bb5\u590d\u5236\u548c\u7c98\u8d34\u5230\u652f\u6301\u8bba\u575b\uff0c\u5e76\u4ee5\u771f\u5b9e\u597d\u7528\u7684\u914d\u7f6e\u6587\u4ef6\u7684\u5f62\u5f0f\u5206\u4eab\u6211\u7684\u77e5\u8bc6\u3002\u4ec5\u5c31\u8fd9\u4e00\u70b9\u800c\u8a00\uff0c\u5b89\u5168\u6027\u7684\u63d0\u9ad8\u4f7f\u6211\u7684\u751f\u6d3b\u66f4\u52a0\u8f7b\u677e\u3002\u800c\u4e14\u6709\u8fd9\u4e48\u591a\u597d\u7684 Python \u6a21\u5757\u53ef\u4ee5\u63d0\u4f9b\u5e2e\u52a9\uff0c\u8fd9\u5f88\u5bb9\u6613\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7528 GPG \u548c Python \u7684 getpass \u6a21\u5757\u7ed9\u4f60\u7684\u5bc6\u7801\u591a\u4e00\u5c42\u5b89\u5168\u4fdd\u969c\u3002 \u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a M […]<\/p>\n","protected":false},"author":317,"featured_media":222378,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-222546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=222546"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222546\/revisions"}],"predecessor-version":[{"id":222583,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222546\/revisions\/222583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/222378"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=222546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=222546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=222546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}