分享好友 最新动态首页 最新动态分类 切换频道
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

最新文章
荣耀 Magic4、5 系列和荣耀 100 系列手机灵动胶囊优化版本推送更新
据悉“灵动胶囊”功能是荣耀在 MagicOS 8.0 上推出的类似“灵动岛”的功能,由。IT之家此前报道,荣耀官方于今年 2 月发布了《荣耀 Magic 5Magic 4 系列灵动胶囊特性适配计划》的公告。公告表示前期针对少量 Magic 5 和 Magic 4 系列内测用
茂名爱采购代运营的选择
商家或企业在选择代运营平台时,可以人员的配备,比如是否有设计、文案、产品更新上架等人员考虑;也可以从平台在本地的口碑,你总是能感受到的。成已为成都多家企业进行运营,公司配有专业人才,每家店铺均有专人维护,累计为商家带来多笔
骗人的!《RPGAPP》是诈骗软件,说我操作失误数据乱了要充钱修复漏洞才给提现怎么办
〖→被骗请点击进入帮助平台提现追款〗〖→被骗请点击进入帮助平台提现追款〗希望阅读此文的读者能够及时采取措施以减少损失;请及时与团队联系提供解决方案(文章上方点击添加技术员咨询快速追回)若想追回损失资产,务必仔细阅读以下内容
短剧分销对接剧场系统
将短剧分销与剧场系统对接可以带来一些有趣的机会,以下是一些可能的步骤和考虑因素:了解剧场系统:研究剧场系统的功能和接口,确定如何与分销系统进行集成。数据同步:确保短剧的信息,如剧目、演出时间、票价等,能够在分销系统和剧场系
WordPress 商城付费主题 Modown 9.1 最新免授权开心版
wordpress主题modown主题V8.12开源版亲测可用无线授权WordPress主题模板兔是一个独特的主题,它以可爱的兔子为主题,给人带来了一种轻松愉快的感觉。这个主题模板是由一群热爱WordPress的设计师和开发者共同开发的,他们在设计这个主题模板
mysql优化及原理,这一篇就够了
说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *、不使用NULL字段、合理创建索引、为字段选择合适的数据类型.....你是否真的理解这些优化技巧?是否理解其背后的工作原理?在实际场景下性能真有提升吗?我想未必,因而
一个非常适合用于做外贸B2B网站的高级WordPress企业主题
本主题有建站操作视频教程,如需获取视频教程,请。今天继续分享一个专门用于做外贸B2B网站的高级WordPress企业主题,这个主题和本站置顶的那篇文章《一个简洁易用WordPress企业网站主题1天建外贸B2B网站教程(置顶)》中介绍的主题一样,是
酷漫屋下拉式免费版 v1.6.1 安卓版
酷漫屋App,你想看的漫画都在这里;最新漫画排行榜,为您带来近期火热各类好看的漫画作品,进入更新频道即可查看今日上线内容,还有在分类中可以快速找到自己喜欢的类型漫画哦!1、精选推荐:为你推荐优质的漫画阅读资源,使你享受到更好的
网站导航栏优化与SEO的关系
网站导航栏优化与SEO的关系网站导航栏优化与SEO(搜索引擎优化)有着紧密的关系。导航栏是用户访问网站的重要指引,同时也是搜索引擎蜘蛛抓取网站内容的重要线索。一、导航栏对用户体验与SEO的双重影响 从用户体验角度看,一个清晰、简洁且
赤羽画质盒美化包
赤羽画质盒美化包是一款备受用户信赖的系统工具软件,它不仅提升了游戏画面质量,还确保了游戏运行的流畅性,提供极其清晰的游戏视觉体验,允许用户为各种游戏自定义画质设置,综合考虑了操作过程、软件性能等多方面因素,赋予用户自由改变
相关文章
推荐文章
发表评论
0评