Google Authenticator implementation in Python
點選(此處)摺疊或開啟
-
#!/usr/bin/env python
-
# coding= utf-8
-
'''
-
Created on 2017年9月29日
-
-
@author: babaoqi
-
'''
-
import string, random
-
import hmac, base64, struct, hashlib, time
-
-
-
def random_str(size=10):
-
#all_char = string.letters + string.digits + string.punctuation
-
all_char = string.letters + string.digits
-
rchars = [random.choice(all_char) for i in range(size)]
-
rstr = "".join(rchars)
-
return rstr
-
-
def get_secret():
-
rstr = random_str()
-
# random_str 長度必須是5的倍數, encode後最後才不含'='號
-
# 長度=10,encode返回長度為16
-
secret = base64.b32encode(rstr)
-
return secret.upper()
-
-
# 基於次數
-
def get_hotp_token(secret, intervals_no):
-
key = base64.b32decode(secret, True)
-
msg = struct.pack(">Q", intervals_no)
-
googleCode = hmac.new(key, msg, hashlib.sha1).digest()
-
o = ord(googleCode[19]) & 15
-
googleCode = (struct.unpack(">I", googleCode[o:o + 4])[0] & 0x7fffffff) % 1000000
-
# 如果驗證碼的第一位是0,則不會顯示。此處判斷若是5位碼,則在第一位補上0
-
# 前面補0
-
sl = len(str(googleCode))
-
if sl < 6:
-
googleCode = "%s%s" % ('0' * (6 - sl), googleCode)
-
return googleCode
-
-
# 基於時間
-
def get_totp_token(secret):
-
period_seconds = 30
-
intervals_no = int(time.time()) // period_seconds
-
return get_hotp_token(secret, intervals_no)
-
-
if __name__ == '__main__':
-
secret = get_secret()
-
-
for i in xrange(0,30):
-
print secret, get_totp_token(secret), get_hotp_token(secret, i)
-
time.sleep(4)
-
- exit(0)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2145604/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- time-based基於google key生成6位驗證碼(google authenticator)Go
- _Authenticator
- Android中Google Authenticator驗證系統工作原理分析AndroidGo
- Laravel 使用 Laravel-google-authenticator 擴充通過 Google 身份驗證器為你的網站打造一個動態手機令牌LaravelGo網站
- Laravel 使用 Laravel-google-authenticator 擴充透過 Google 身份驗證器為你的網站打造一個動態手機令牌LaravelGo網站
- 【譯】TCP Implementation in LinuxTCPLinux
- Linux下部署SSH登入時的二次身份驗證環境記錄(利用Google Authenticator)LinuxGo
- The Design and Implementation of Hyperupcalls 翻譯PCA
- SAP KANBAN and Implementation ConsiderationsIDE
- SAP S/4 HANA On-Premise implementationREM
- How to change the implementation (detour) of an externally declared functionFunction
- react元件內報implementation()方法錯誤!React元件
- Observer Pattern - An Enterprise JavaBean Implementation (轉)ServerJavaBean
- Android Gradle 依賴配置:implementation & apiAndroidGradleAPI
- SAP Commerce的路由實現(Route Implementation)路由
- Google 面試題 | 132模式 Python 版本Go面試題模式Python
- python抓取google搜尋結果PythonGo
- Google App Engine正式支援Python 2.7GoAPPPython
- Design and implementation of database anomaly monitoring system based on AI algorithmDatabaseAIGo
- Five SAP HANA implementation tips CIOs should knowiOS
- 設計模式(五)橋接模式Bridge == Pointer To Implementation設計模式橋接
- Google Python 程式設計風格指南GoPython程式設計
- 高德地圖No implementation found for long com.autonavi.amap.mapcore.MapCore地圖
- 巧用Google Fire簡化Python命令列程式GoPython命令列
- Python之父從Google離職,加入DropboxPythonGo
- teams 更改密碼後無法登入,提示使用 Microsoft Authenticator 驗證問題密碼ROS
- 如何用 Python 指令碼批量下載 Google 影象?Python指令碼Go
- Grumpy:Google開源神器 Go上執行PythonGoPython
- The app.Configuration 'compile' is obsolete and has been replaced with 'implementation'APPCompile
- Implementation Notes: Runtime Environment Map Filtering for Image Based LightingFilter
- Python一鍵轉Java?“Google翻譯”你別鬧PythonJavaGo
- 谷歌正式推出Python版Google API客戶端庫谷歌PythonGoAPI客戶端
- 帶讀 |《Redis 設計與實現》(英文名:The Design and Implementation of Redis)Redis
- Android中gradle檔案中implementation和compile的異同AndroidGradleCompile
- 重磅!Google推出了Python最牛X的編輯器......GoPython
- Google的Python程式碼格式化工具YAPF詳解GoPython
- Using Python to Shorten a URL Using Google's Shortening ServicePythonGo
- C++ Templates (2.1 類别範本Stack的實現 Implementation of Class Template Stack)C++