谷歌身份验证器(Google Authenticator)

前言

Google Authenticator,是谷歌推出的一款动态口令工具,解决大家的google账户遭到恶意攻击的问题;许多安全性比较高的网站都会采用这种工具来验证登录或者交易;这个动态口令就是Google身份验证器每隔30s会动态生成一个6位数的数字。它的作用是:对你的账号进行“二步验证”保护,或者说做一个双重身份验证,来达到提升安全级别的目的。

步骤

第一步:
1. 在您的手机上安装双重验证程序: Google Authenticator
2. iPhone手机: 在App Store中搜索 Google Authenticator
3. android手机:在应用市场中搜索“谷歌身份验证器”,或搜索Google Authenticator
第二步:安装完成后,您需要对该应用程序进行如下配置:第二步:安装完成后,您需要对该应用程序进行如下配置:
在“google Authenticator(身份验证器)”应用程序中,点击“添加新账户(iOS下是+号)”,然后选择“扫描条形码”。
第三步:配置完成后,手机上会显示一个6位数字,每隔30秒变化一次。这个数字即为您的双重验证密码。请勿删除此双重验证密码账户,否则会导致您无法进行账户操作。您可将密钥记录下来:如果误删,可通过手动输入密钥来恢复。输入双重验证码,以开启或关闭双重验证功能。

谷歌身份验证生成秘钥的接口

import base64
import hashlib
import hmac
import time
import datetime
import random as _randomdef byte_secret(secret):missing_padding = len(secret) % 8
if missing_padding != 0:secret += '=' * (8 - missing_padding)
return base64.b32decode(secret, casefold=True)def int_to_bytestring(i, padding=8):result = bytearray()while i != 0:result.append(i & 0xFF)i >>= 8return bytes(bytearray(reversed(result)).rjust(padding, b'\0'))#根据约定的密钥计算当前动态密码
def generate_otp(secret):for_time = datetime.datetime.now()i = time.mktime(for_time.timetuple())input = int(i / 30)digest = hashlib.sha1digits = 6if input < 0:raise ValueError('input must be positive integer')hasher = hmac.new(byte_secret(secret), int_to_bytestring(input), digest)hmac_hash = bytearray(hasher.digest())offset = hmac_hash[-1] & 0xfcode = ((hmac_hash[offset] & 0x7f) << 24 |(hmac_hash[offset + 1] & 0xff) << 16 |(hmac_hash[offset + 2] & 0xff) << 8 |(hmac_hash[offset + 3] & 0xff))str_code = str(code % 10 ** digits)while len(str_code) < digits:str_code = '0' + str_codereturn str_code#随机生成一个base32密钥
def random_base32(length=16, random=_random.SystemRandom(),chars=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')):return ''.join(random.choice(chars)for _ in range(length))# 生成二维码地址
def getQRCodeGoogleUrl(name, secret):return "otpauth://totp/" + name + "?secret=%s" % secretif __name__ == '__main__':print(type(random_base32()))print(generate_otp('XPFBV5JAC3FS42FE'))

功能实现(自己随便写的,仅供参考)

class GenerateOfferView(APIView):
"""双重验证提供参数"""
permission_classes = [IsAuthenticated]def get(self, request):try:user = User.objects.get(id=request.user.id)except User.DoesNotExist:user = Noneif not user:return Response({"msg": "请先登录!", "status": status.HTTP_401_UNAUTHORIZED})if user.ga == "":secret = random_base32()    # 生成秘钥redis_conn = get_redis_connection('verify_codes')pl = redis_conn.pipeline()pl.setex("secret_%s" % user.moble, constants.GOOGLE_SECRET_EXPIRES, secret)pl.execute()code_url = getQRCodeGoogleUrl(user.username, secret)data = {"code_url": code_url,"secret": secret}return Response({"data": data,'status': status.HTTP_200_OK,'msg': '请先开启双重验证!'})else:ga_list = user.ga.split("|")secret = ga_list[0]code_url = getQRCodeGoogleUrl(user.username, secret)data = {"code_url": code_url,'ga_code': secret,"go_login": ga_list[1],"go_transfer": ga_list[2]}return Response({"data": data,"status": status.HTTP_200_OK,})class GenerateView(GenericAPIView):
"""双重验证"""
permission_classes = [IsAuthenticated]
serializer_class = GenerateSerializerdef post(self, request):ser = self.get_serializer(data=request.data)ser.is_valid(raise_exception=True)ga_code = ser.validated_data['ga_code']ga_login = ser.validated_data['ga_login']ga_transfer = ser.validated_data['ga_transfer']mold = ser.validated_data['mold']try:user = User.objects.get(id=request.user.id)except User.DoesNotExist:user = Noneif not user:return Response({"msg": "请先登录", 'status': status.HTTP_401_UNAUTHORIZED})if not ga_code:return Response({"msg": "请输入验证码!", "status": status.HTTP_400_BAD_REQUEST})if int(mold) == 0:redis_conn = get_redis_connection('verify_codes')secret = redis_conn.get('secret_%s' % user.moble)if not secret:return Response({'msg': "验证码已经失效,请刷新网页", "status": status.HTTP_400_BAD_REQUEST})if ga_code != generate_otp(secret):return Response({"msg": "验证码输入有误!", "status": status.HTTP_400_BAD_REQUEST})user.ga = secret.decode() + "|" + ga_login + "|" + ga_transferuser.save()code_url = getQRCodeGoogleUrl(user.username, secret)return Response({"data": {"qrCodeUrl": code_url, "secret": secret}, "status": status.HTTP_200_OK})elif int(mold) == 1 or int(mold) == 2:if not user.ga:return Response({'msg': "还未设置谷歌验证码", "status": status.HTTP_400_BAD_REQUEST})if not ga_code:return Response({"msg": "请输入验证码!", "status": status.HTTP_400_BAD_REQUEST})ga_list = user.ga.split("|")secret = ga_list[0]if ga_code == generate_otp(str(secret)):user.ga = ""user.save()secret = random_base32()redis_conn = get_redis_connection('verify_codes')pl = redis_conn.pipeline()pl.setex("secret_%s" % user.moble, constants.GOOGLE_SECRET_EXPIRES, secret)pl.execute()code_url = getQRCodeGoogleUrl(user.username, secret)data = {"code_url": code_url,"secret": secret}return Response({"data": data,'status': status.HTTP_200_OK,'msg': '解除绑定成功!'})else:return Response({"msg": "验证未通过,请重新验证!", "status": status.HTTP_400_BAD_REQUEST})

使用思路

1、第一次请求,未绑定谷歌验证码,生成随机base32的秘钥,二维码,供绑定使用;
2、绑定验证,将手机app生成的验证码输入验证,成功后将秘钥存入数据库;
3、绑定过后每次请求查询数据库的秘钥生成二维码传出;
4、取消绑定,清除数据库的数据。

谷歌身份验证器(Google Authenticator)的使用详情相关推荐

  1. javascript版谷歌身份验证器google authenticator

    https://www.cnblogs.com/huangcong/p/11028910.html    : https://github.com/wuyanxin/totp.js 使用JS计算Goo ...

  2. 如何为SSH登录建立双因子验证机制(谷歌身份验证器)?

    前言 默认情况下,SSH已经在远程机器之间使用安全的数据通信;但是如果你想为自己的SSH连接添加另外某种安全层,可以添加谷歌身份验证器(Google Authenticator)双因子验证模块,该模块 ...

  3. 【SpringBoot】61、SpringBoot中使用谷歌身份验证器(Google Authenticator)实现二步身份验证

    Google 身份验证器 Google Authenticator 是谷歌推出的基于时间的一次性密码 (Time-based One-time Password,简称 TOTP),只需要在手机上安装该 ...

  4. 使用C++实现谷歌身份验证器(Google Authenticator)

    使用C++实现谷歌身份验证器(Google Authenticator) 本机环境: windows10 x64位运行环境 1.进入网站:http://slproweb.com/products/Wi ...

  5. 使用谷歌身份验证器(Google Authenticator)保护你的后台

    为何要使用谷歌身份验证器 普通的网站只使用账号.密码.图形验证码进行后台登录.根据我(作为站长)多年的经验来看,这种方式安全性很低,尤其是使用 http 协议,明文的帐号和密码相当于在网络上裸奔.如果 ...

  6. Google Authenticator windows client 谷歌身份验证器 windows 电脑端

    谷歌身份验证器现在有安卓客户端和ios客户端,本人开发了一个windows客户端,基于 .NETFramework v4.7 开发,已在 github 上开源,可以在 github 上直接下载. gi ...

  7. google authenticator python_谷歌验证器(Google Authenticator)

    双因素身份认证就是经过你所知道再加上你所能拥有的这二个要素组合到一块儿才能发挥做用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产生的一次性密码来代替传统的 ...

  8. google authenticator python_谷歌验证器 Google Authenticator工作原理

    很多人都听过谷歌验证 (Google Authenticator) 或用过谷歌验证 (Google Authenticator) .尤其是随着比特币等虚拟货币的兴起,各大交易所都要求绑定谷歌验证 (G ...

  9. 使用谷歌身份验证器增强SSH安全

    一般大家都是使用账号和密码远程SSH登录管理服务器.但SSH账号和密码很容易泄露,或者经常遭遇暴力破解.咨询过前同事赛赛,他们目前使用了谷歌身份验证器.查看了谷歌身份验证器的github和其它网上文档 ...

  10. 二次验证码小程序与谷歌身份验证器不同点是?

    名称1[二次验证码]小程序 名称2 谷歌身份验证器(Google Authenticator) 粗略对比两个产品异同 [二次验证码]小程序 搜索:微信搜索.微信目前65个小程序入口,倒是容易找到它 使 ...

最新文章

  1. AI框架外部用户贡献代码
  2. android universal image loader 缓冲原理详解
  3. 简单借还书管理系统c语言,急求程序!!!简单图书馆借/还书管理子系统
  4. C#最基本的小说爬虫
  5. (Hibernate进阶)Hibernate系列——总结篇(九)
  6. ansible配置详解及基本示例
  7. vsftp配置日志及其启用本地时间
  8. Shiro——RememberMe
  9. python 弹出对话框_python+selenium 抓取弹出对话框信息
  10. java和oc_Java和OC中的数据容器和数组
  11. c语言程序设计自学跟谁好,双辽c语言编程学习,双辽学c语言编程哪个好,双辽学c语言编程自学好还是报班好...
  12. mysql 5.622_新特新解读 | MySQL 8.0 对 count(*)的优化
  13. linux内核分析与应用 -- 进程与线程(下)
  14. VSCode: 快捷键大全
  15. Matlab题目及答案,Matlab考试试题库+答案解析.doc
  16. 2017年油价调整时间表
  17. 索尼MUC-M2BT1换电池+爆改
  18. Android 自定义价格日历控件
  19. RFID固定资产定位管理系统-智慧资产人员可视化管理系统
  20. Apache+php的安装和配置

热门文章

  1. 弹出启动windows安全中心服务器,Win10每次开机都弹出启用Windows安全中心服务怎么办?...
  2. matlab黄金分割实例,实战案例教你黄金分割
  3. Exif文件格式描述(转载)
  4. Eclipse2020下载安装教程详细
  5. 机器人庄园作文_关于周庄一日游作文六年级汇总5篇
  6. C# QQ连连看外挂(内存版)源代码
  7. 计算机桌面ie图标无法删除,解决桌面IE图标或者我的电脑无法删除
  8. 正反转信号判别原理以及数字鉴相器原理
  9. 获取 iOS 设备 UDID
  10. roc曲线spss怎么做_如何用SPSS做ROC曲线分析