{"id":1200,"date":"2019-03-16T04:52:29","date_gmt":"2019-03-16T04:52:29","guid":{"rendered":"http:\/\/www.ipcpu.com\/?p=1200"},"modified":"2019-05-12T05:04:52","modified_gmt":"2019-05-12T05:04:52","slug":"maf-hotp-totp","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2019\/03\/maf-hotp-totp\/","title":{"rendered":"\u591a\u56e0\u5b50\u8ba4\u8bc1\u4e4bHOTP\u548cTOTP\u7684\u539f\u7406\u548c\u5b9e\u73b0(Python)"},"content":{"rendered":"

\u4e00\u3001\u6982\u8ff0<\/h2>\n

\u53cc\u56e0\u5b50\u8ba4\u8bc1\uff08Two-factor authentication\uff0c\u4e5f\u53eb2FA\uff09\uff0c\u662f\u4e00\u79cd\u901a\u8fc7\u7ec4\u5408\u4e24\u79cd\u4e0d\u540c\u7684\u9a8c\u8bc1\u65b9\u5f0f\u8fdb\u884c\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u7684\u673a\u5236\u3002Google\u57282011\u5e743\u6708\u4efd\uff0c\u5ba3\u5e03\u5728\u7ebf\u4e0a\u4f7f\u7528\u53cc\u56e0\u5b50\u8ba4\u8bc1\uff0cMSN\u548cYahoo\u7d27\u968f\u5176\u540e\u3002<\/p>\n

\u53cc\u56e0\u5b50\u8ba4\u8bc1\uff0c\u9664\u4e86\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u540d\u5bc6\u7801\u5916\uff0c\u8fd8\u8981\u7ed3\u5408\u53e6\u5916\u4e00\u79cd\u5b9e\u7269\u8bbe\u5907\uff0c\u5982Rsa\u4ee4\u724c\uff0c\u6216\u8005\u624b\u673a\u3002
\n\u53cc\u56e0\u5b50\u8ba4\u8bc1\u7684\u4ea7\u54c1\u5927\u81f4\u53ef\u4ee5\u5206\u6210\u4e24\u7c7b\uff1a<\/p>\n

\n

\u53ef\u4ee5\u4ea7\u751ftoken\u7684\u786c\u4ef6\u8bbe\u5907
\n \u667a\u80fd\u624b\u673a\u7684app<\/p>\n<\/blockquote>\n

\u624b\u673a\u77ed\u4fe1\u9a8c\u8bc1\u7801\uff0c\u767b\u5f55\u5fae\u4fe1\u516c\u4f17\u53f7\u65f6\u7684\u626b\u7801\u786e\u8ba4\u90fd\u53ef\u4ee5\u79f0\u4e3a\u53cc\u56e0\u5b50\u8ba4\u8bc1\u3002\u672c\u6587\u53ea\u4ecb\u7ecdOTP\u4e00\u6b21\u6027\u5bc6\u7801\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3atoken\u3002<\/p>\n

\u4e8c\u3001\u76f8\u5173\u672f\u8bed<\/h2>\n

OTP \u662f One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u4e00\u6b21\u6027\u5bc6\u7801\u3002\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd<\/strong><\/p>\n

\n

HOTP \u662fHMAC-based One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u57fa\u4e8eHMAC\u7b97\u6cd5\u52a0\u5bc6\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u3002<\/p>\n<\/blockquote>\n

\u3000\u3000HOTP \u662f\u4e8b\u4ef6\u540c\u6b65\uff0c\u901a\u8fc7\u67d0\u4e00\u7279\u5b9a\u7684\u4e8b\u4ef6\u6b21\u5e8f\u53ca\u76f8\u540c\u7684\u79cd\u5b50\u503c\u4f5c\u4e3a\u8f93\u5165\uff0c\u901a\u8fc7HASH\u7b97\u6cd5\u8fd0\u7b97\u51fa\u4e00\u81f4\u7684\u5bc6\u7801\u3002RFC4226\u3002<\/p>\n

\n

TOTP \u662fTime-based One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u57fa\u4e8e\u65f6\u95f4\u6233\u7b97\u6cd5\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u3002 <\/p>\n<\/blockquote>\n

\u3000\u3000TOTP \u662f\u65f6\u95f4\u540c\u6b65\uff0c\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u52a8\u6001\u53e3\u4ee4\u548c\u52a8\u6001\u53e3\u4ee4\u9a8c\u8bc1\u670d\u52a1\u5668\u7684\u65f6\u95f4\u6bd4\u5bf9\uff0c\u4e00\u822c\u6bcf60\u79d2\u4ea7\u751f\u4e00\u4e2a\u65b0\u53e3\u4ee4\uff0c\u8981\u6c42\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u80fd\u591f\u5341\u5206\u7cbe\u786e\u7684\u4fdd\u6301\u6b63\u786e\u7684\u65f6\u949f\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u57fa\u4e8e\u65f6\u95f4\u8ba1\u7b97\u7684\u52a8\u6001\u53e3\u4ee4\u624d\u80fd\u4e00\u81f4\u3002\u3000RFC6238\u3002\u3000<\/p>\n

\u4e09\u3001OTP\u5b9e\u73b0\u539f\u7406<\/h2>\n


\n\u901a\u8fc7\u4e0a\u56fe\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u8f93\u5165\u7b97\u6cd5\u7684\u4e3b\u8981\u6709\u4e24\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u662f\u5171\u4eab\u5bc6\u94a5(\u4e5f\u88ab\u79f0\u4f5c\u79cd\u5b50)\uff0c\u53e6\u5916\u4e00\u4e2a\u662f\u8ba1\u6570(\u6216\u8005\u65f6\u95f4\u56e0\u5b50)\uff0c\u7ecf\u8fc7\u7279\u5b9a\u7684\u7b97\u6cd5\u8ba1\u7b97\u51fa\u7ed3\u679c\u3002\u5982\u679c\u8fd9\u4e24\u4e2a\u5143\u7d20\u5168\u90fd\u4e00\u81f4\uff0c\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u4f1a\u8ba1\u7b97\u51fa\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u5b9e\u73b0\u8ba4\u8bc1\u529f\u80fd\u3002<\/p>\n

\u56db\u3001\u4f7f\u7528Python\u5b9e\u73b0OTP\u529f\u80fd<\/h2>\n

\u76ee\u524dPython\u4e0a\u6709\u4ee5\u4e0b\u4e24\u5927\u6a21\u5757\u6bd4\u8f83\u6d41\u884c\uff0c\u6211\u4eec\u4ee5\u7b2c\u4e00\u4e2apyotp\u4e3a\u4f8b\u4ecb\u7ecd\u6bd4\u8f83\u5e38\u7528\u7684TOTP\u3002
\nhttps:\/\/github.com\/pyauth\/pyotp<\/a>
\n
https:\/\/github.com\/tadeck\/onetimepass\/<\/a><\/p>\n

pyotp\u5c31\u662f\u4e00\u4e2apython\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528pip\u5b89\u88c5\u3002<\/p>\n

\n
[root@ipcpu-apollo ~]# python\r\nPython 3.6.5 (default, Jul 18 2018, 16:07:41) \r\n[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n>>> \r\n>>> import base64\r\n>>> string='this is s string'\r\n>>> secretKey = base64.b32encode(string.encode(encoding=\"utf-8\"))\r\n>>> print(secretKey)\r\nb'ORUGS4ZANFZSA4ZAON2HE2LOM4======'\r\n>>> import pyotp\r\n>>> totp = pyotp.TOTP(secretKey)\r\n>>> totp.now()\r\n'863055'\r\n>>> totp.verify(863055)\r\nTrue<\/code><\/pre>\n<\/div>\n

\u4e94\u3001pyotp\u548cGoogle Authenticator\u7684\u517c\u5bb9<\/h2>\n

pyotp\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u4eae\u70b9\u5c31\u662f\u5185\u7f6e\u4e86\u548cGoogle Authenticator\u7684\u517c\u5bb9\uff0c\u5982\u4e0b\u5c31\u53ef\u4ee5\u4ea7\u751fGoogle Authenticator\u53ef\u4ee5\u8bc6\u522b\u7684\u5b57\u7b26\u4e32<\/p>\n

\n
>>> pyotp.totp.TOTP('JBSWY3DPEHPK3PXP').provisioning_uri(\"alice@google.com\", issuer_name=\"Secure App\")\r\n'otpauth:\/\/totp\/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP&issuer=Secure%20App'<\/code><\/pre>\n<\/div>\n

\u628a\u8fd9\u4e00\u4e32\u5b57\u7b26\u751f\u6210QR\u4e8c\u7ef4\u7801\uff0c\u7136\u540e\u7528Google Authenticator\u626b\u63cf\u5c31\u53ef\u4ee5\u7528\u3002
\n\u670d\u52a1\u5668\u7aef\u53ef\u4ee5\u7528\u5982\u4e0b\u65b9\u5f0f\u9a8c\u8bc1<\/p>\n

\n
>>> import pyotp\r\n>>> totp = pyotp.TOTP(\"JBSWY3DPEHPK3PXP\")\r\n>>> print(\"Current OTP:\", totp.now())\r\nCurrent OTP: 267343<\/code><\/pre>\n<\/div>\n

\u6211\u4eec\u603b\u7ed3\u4e0b\uff0cGoogle Authenticator\u5b9e\u9645\u4e0a\u53ea\u5b58\u50a8\u4e86\u5171\u4eab\u5bc6\u94a5\uff0c\u5e76\u901a\u8fc7\u5171\u4eab\u5bc6\u94a5\u6765\u4ea7\u751fOTP\u5bc6\u7801\uff0c\u5176\u4ed6\u7684\u90ae\u7bb1\u3001\u7b7e\u53d1\u4eba\u4ec0\u4e48\u7684\u90fd\u662f\u4e3a\u4e86\u6807\u793a\u5171\u4eab\u5bc6\u94a5\u7528\u6765\u533a\u5206\u7684\u3002
\n<\/p>\n

\u53c2\u8003\u8d44\u6599<\/h2>\n

https:\/\/www.jianshu.com\/p\/a7b900e8e50a<\/a>
\n
https:\/\/pyotp.readthedocs.io\/en\/latest\/<\/a>
\n
https:\/\/www.cnblogs.com\/voipman\/p\/6216328.html<\/a><\/p>\n

<\/p>\n

<\/div>\n
<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n

<\/wiz_tmp_tag><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » \u591a\u56e0\u5b50\u8ba4\u8bc1\u4e4bHOTP\u548cTOTP\u7684\u539f\u7406\u548c\u5b9e\u73b0(Python)<\/a><\/p>","protected":false},"excerpt":{"rendered":"

\u4e00\u3001\u6982\u8ff0 \u53cc\u56e0\u5b50\u8ba4\u8bc1\uff08Two-factor authentication\uff0c\u4e5f\u53eb2FA\uff09\uff0c\u662f\u4e00\u79cd\u901a\u8fc7\u7ec4\u5408\u4e24\u79cd\u4e0d\u540c\u7684\u9a8c\u8bc1\u65b9\u5f0f\u8fdb\u884c\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u7684\u673a\u5236\u3002Google\u57282011\u5e743\u6708\u4efd\uff0c\u5ba3\u5e03\u5728\u7ebf\u4e0a\u4f7f\u7528\u53cc\u56e0\u5b50\u8ba4\u8bc1\uff0cMSN\u548cYahoo\u7d27\u968f\u5176\u540e\u3002 \u53cc\u56e0\u5b50\u8ba4\u8bc1\uff0c\u9664\u4e86\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u540d\u5bc6\u7801\u5916\uff0c\u8fd8\u8981\u7ed3\u5408\u53e6\u5916\u4e00\u79cd\u5b9e\u7269\u8bbe\u5907\uff0c\u5982Rsa\u4ee4\u724c\uff0c\u6216\u8005\u624b\u673a\u3002 \u53cc\u56e0\u5b50\u8ba4\u8bc1\u7684\u4ea7\u54c1\u5927\u81f4\u53ef\u4ee5\u5206\u6210\u4e24\u7c7b\uff1a \u53ef\u4ee5\u4ea7\u751ftoken\u7684\u786c\u4ef6\u8bbe\u5907 \u667a\u80fd\u624b\u673a\u7684app \u624b\u673a\u77ed\u4fe1\u9a8c\u8bc1\u7801\uff0c\u767b\u5f55\u5fae\u4fe1\u516c\u4f17\u53f7\u65f6\u7684\u626b\u7801\u786e\u8ba4\u90fd\u53ef\u4ee5\u79f0\u4e3a\u53cc\u56e0\u5b50\u8ba4\u8bc1\u3002\u672c\u6587\u53ea\u4ecb\u7ecdOTP\u4e00\u6b21\u6027\u5bc6\u7801\uff0c\u4e5f\u7ecf\u5e38\u88ab\u79f0\u4e3atoken\u3002 \u4e8c\u3001\u76f8\u5173\u672f\u8bed OTP \u662f One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u4e00\u6b21\u6027\u5bc6\u7801\u3002\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd HOTP \u662fHMAC-based One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u57fa\u4e8eHMAC\u7b97\u6cd5\u52a0\u5bc6\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u3002 \u3000\u3000HOTP \u662f\u4e8b\u4ef6\u540c\u6b65\uff0c\u901a\u8fc7\u67d0\u4e00\u7279\u5b9a\u7684\u4e8b\u4ef6\u6b21\u5e8f\u53ca\u76f8\u540c\u7684\u79cd\u5b50\u503c\u4f5c\u4e3a\u8f93\u5165\uff0c\u901a\u8fc7HASH\u7b97\u6cd5\u8fd0\u7b97\u51fa\u4e00\u81f4\u7684\u5bc6\u7801\u3002RFC4226\u3002 TOTP \u662fTime-based One-Time Password\u7684\u7b80\u5199\uff0c\u8868\u793a\u57fa\u4e8e\u65f6\u95f4\u6233\u7b97\u6cd5\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u3002 \u3000\u3000TOTP \u662f\u65f6\u95f4\u540c\u6b65\uff0c\u57fa\u4e8e\u5ba2\u6237\u7aef\u7684\u52a8\u6001\u53e3\u4ee4\u548c\u52a8\u6001\u53e3\u4ee4\u9a8c\u8bc1\u670d\u52a1\u5668\u7684\u65f6\u95f4\u6bd4\u5bf9\uff0c\u4e00\u822c\u6bcf60\u79d2\u4ea7\u751f\u4e00\u4e2a\u65b0\u53e3\u4ee4\uff0c\u8981\u6c42\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u80fd\u591f\u5341\u5206\u7cbe\u786e\u7684\u4fdd\u6301\u6b63\u786e\u7684\u65f6\u949f\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u57fa\u4e8e\u65f6\u95f4\u8ba1\u7b97\u7684\u52a8\u6001\u53e3\u4ee4\u624d\u80fd\u4e00\u81f4\u3002\u3000RFC6238\u3002\u3000 \u4e09\u3001OTP\u5b9e\u73b0\u539f\u7406 \u901a\u8fc7\u4e0a\u56fe\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u8f93\u5165\u7b97\u6cd5\u7684\u4e3b\u8981\u6709\u4e24\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u662f\u5171\u4eab\u5bc6\u94a5(\u4e5f\u88ab\u79f0\u4f5c\u79cd\u5b50)\uff0c\u53e6\u5916\u4e00\u4e2a\u662f\u8ba1\u6570(\u6216\u8005\u65f6\u95f4\u56e0\u5b50)\uff0c\u7ecf\u8fc7\u7279\u5b9a\u7684\u7b97\u6cd5\u8ba1\u7b97\u51fa\u7ed3\u679c\u3002\u5982\u679c\u8fd9\u4e24\u4e2a\u5143\u7d20\u5168\u90fd\u4e00\u81f4\uff0c\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u4f1a\u8ba1\u7b97\u51fa\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u5b9e\u73b0\u8ba4\u8bc1\u529f\u80fd\u3002 \u56db\u3001\u4f7f\u7528Python\u5b9e\u73b0OTP\u529f\u80fd \u76ee\u524dPython\u4e0a\u6709\u4ee5\u4e0b\u4e24\u5927\u6a21\u5757\u6bd4\u8f83\u6d41\u884c\uff0c\u6211\u4eec\u4ee5\u7b2c\u4e00\u4e2apyotp\u4e3a\u4f8b\u4ecb\u7ecd\u6bd4\u8f83\u5e38\u7528\u7684TOTP\u3002 https:\/\/github.com\/pyauth\/pyotp https:\/\/github.com\/tadeck\/onetimepass\/ pyotp\u5c31\u662f\u4e00\u4e2apython\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528pip\u5b89\u88c5\u3002 [root@ipcpu-apollo ~]# python Python 3.6.5 (default, Jul 18 2018, 16:07:41) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type “help”, “copyright”, “credits” or […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[17,193,192,59,15],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1200"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1200"}],"version-history":[{"count":2,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1200\/revisions"}],"predecessor-version":[{"id":1215,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1200\/revisions\/1215"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}