分享好友 最新动态首页 最新动态分类 切换频道
google authenticator python_在Python谷歌身份验证实施
2024-12-26 20:27

I am trying to use one-time passwords that can be generated using Google Authenticator application.

google authenticator python_在Python谷歌身份验证实施

What Google Authenticator does

Basically, Google Authenticator implements two types of passwords:

HOTP - HMAC-based One-Time Password, which means the password is changed with each call, in compliance to RFC4226, and

TOTP - Time-based One-Time Password, which changes for every 30-seconds period (as far as I know).

Google Authenticator is also available as Open Source here: code.google.com/p/google-authenticator

Current code

I was looking for existing solutions to generate HOTP and TOTP passwords, but did not find much. The code I have is the following snippet responsible for generating HOTP:

import hmac, base64, struct, hashlib, time

def get_token(secret, digest_mode=hashlib.sha1, intervals_no=None):

if intervals_no == None:

intervals_no = int(time.time()) // 30

key = base64.b32decode(secret)

msg = struct.pack(">Q", intervals_no)

h = hmac.new(key, msg, digest_mode).digest()

o = ord(h[19]) & 15

h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000

return h

The problem I am facing is that the password I generate using the above code is not the same as generated using Google Authenticator app for Android. Even though I tried multiple intervals_no values (exactly first 10000, beginning with intervals_no = 0), with secret being equal to key provided within the GA app.

Questions I have

My questions are:

What am I doing wrong?

How can I generate HOTP and/or TOTP in Python?

Are there any existing Python libraries for this?

To sum up: please give me any clues that will help me implement Google Authenticator authentication within my Python code.

解决方案

I wanted to set a bounty on my question, but I have succeeded in creating solution. My problem seemed to be connected with incorrect value of secret key (it must be correct parameter for base64.b32decode() function).

Below I post full working solution with explanation on how to use it.

Code

The following code is enough. I have also uploaded it to GitHub as separate module called onetimepass (available here: https://github.com/tadeck/onetimepass).

import hmac, base64, struct, hashlib, time

def get_hotp_token(secret, intervals_no):

key = base64.b32decode(secret, True)

msg = struct.pack(">Q", intervals_no)

h = hmac.new(key, msg, hashlib.sha1).digest()

o = ord(h[19]) & 15

h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000

return h

def get_totp_token(secret):

return get_hotp_token(secret, intervals_no=int(time.time())//30)

It has two functions:

get_hotp_token() generates one-time token (that should invalidate after single use),

get_totp_token() generates token based on time (changed in 30-second intervals),

Parameters

When it comes to parameters:

secret is a secret value known to server (the above script) and client (Google Authenticator, by providing it as password within application),

intervals_no is the number incremeneted after each generation of the token (this should be probably resolved on the server by checking some finite number of integers after last successful one checked in the past)

How to use it

Generate secret (it must be correct parameter for base64.b32decode()) - preferably 16-char (no = signs), as it surely worked for both script and Google Authenticator.

Use get_hotp_token() if you want one-time passwords invalidated after each use. In Google Authenticator this type of passwords i mentioned as based on the counter. For checking it on the server you will need to check several values of intervals_no (as you have no quarantee that user did not generate the pass between the requests for some reason), but not less than the last working intervals_no value (thus you should probably store it somewhere).

Use get_totp_token(), if you want a token working in 30-second intervals. You have to make sure both systems have correct time set (meaning that they both generate the same Unix timestamp in any given moment in time).

Make sure to protect yourself from brute-force attack. If time-based password is used, then trying 1000000 values in less than 30 seconds gives 100% chance of guessing the password. In case of HMAC-based passowrds (HOTPs) it seems to be even worse.

Example

When using the following code for one-time HMAC-based password:

secret = 'MZXW633PN5XW6MZX'

for i in xrange(1, 10):

print i, get_hotp_token(secret, intervals_no=i)

you will get the following result:

1 448400

2 656122

3 457125

4 35022

5 401553

6 581333

7 16329

8 529359

9 171710

最新文章
金华企业在线曝光新引擎,百度推广优化助力抢占市场制高点
金华百度推广优化助力企业提升在线曝光率,抢占市场先机。通过精准关键词投放、创意广告设计及数据分析优化,实现高效推广,助力企业品牌形象及产品销售。随着互联网的普及,越来越多的企业开始意识到网络营销的重要性,在众多网络营销手段
用“小森林”的方式打开市南 这3家氛围餐馆Get你的冬日“小食光”
生活的方式有千百种,用心感受才会知道自己适合哪一种。——《小森林》想告诉你一些冬天的秘密热气腾腾的食物暖呼呼的被窝窗外绽放的烟花寒冷的天气让我们更加敏感的捕捉到隐藏在每个角落里的小温暖所以“寒冷”也是重要的调味料之一~未知
陕西省财政厅机关2025年陕西省预算管理一体化系统融合模块运维项目(含人员台账、账户管理、门户模块)(二次)竞争性磋商公告
1、落实政府采购政策: 1.1《关于进一步加大政府采购支持中小企业力度的通知》(财库〔2022〕19号)、《政府采购促进中小企业发展管理办法》(财库〔2020〕46号)、《财政部 司法部关于政府采购支持监狱企业发展有关问题的通知》(财库〔20
北京360广告的收费方式,360广告平台投放
360广告的收费方式主要包括按点击付费(CPC)和按展示付费(CPM)两种模式。此外,360广告还支持其他计费方式,如按时间付费(CPT)、按下载付费(CPA)等,以满足不同广告主的需求。在开户方面,通常需要预存一定的广告费用,例如5000元或
请问什么是微信cid广告?怎么投放?
微信CID(Click-ID)广告的投放涉及多个步骤,以下是详细的投放流程:一、前期准备注册微信视频号,并完成企业或个人认证。如果尚未开通视频号小店,需要先进行开通,并进行相关加白操作。开通ADQ广告账号,并配置CID技术。这通常需要1~3天
车企团队协作优化——看板软件篇
在汽车行业的激烈竞争中,极越汽车的经历为我们提供了深刻的启示。作为一家由百度和夏一平共同推动的汽车品牌,极越的起伏不仅反映了市场环境的严峻,更揭示了企业管理与团队协作中的关键问题。本文将探讨如何利用看板软件优化车企的团队协
网站建设五大步骤全解析,打造高效网站攻略
建设网站五大步骤解析:1. 确定网站类型和功能;2. 设计网站架构和界面;3. 编码实现网站功能;4. 测试和优化网站性能;5. 上线运营和后期维护。遵循这些步骤,打造专业、高效、易用的网站。确立网站定位与目标网站规划与设计构思网站开发
香港公司注册微信小程序的流程步骤 - 中英详细版
The process steps for registering an overseas company on WeChat mini program1、注册入口打开微信公众平台官网首页(mp.weixin.qq.com),点击右上角的“立即注册”按钮。Registration entranceOpen the official homepage of WeChat p
香港高校巧思引领“00后”创新,焕新创业理解
香港城大创新学院副院长刘特斌认为,创新创业教育要解决的最大痛点是让学生跳出舒适圈,而不是停留于学校实验室,“如果你真的做得好,你的毕业商业计划书可能会成就你的第一个初创公司。”“开始系统学习创业这门课程后,我才发现原来‘想
百度蜘蛛池效果:蜘蛛池外链在搜索引擎优化中的重要作用与策略解析
百度蜘蛛池在搜索引擎优化中扮演关键角色,通过构建高效的外链网络,帮助网站提升搜索引擎排名。本文解析了蜘蛛池外链策略,包括合理布局、优化内容质量和选择优质平台,以实现网站在搜索引擎中的良好表现。本文目录导读:蜘蛛池外链的定义
相关文章
推荐文章
发表评论
0评