当前位置: 首页 > news >正文

Go中使用Google Authenticator

现在为了安全Google二次验证使用越来越平凡了,所以我们自己做的一些产品中,也会用到Google Authenticator。

介绍

Google Authenticator采用的算法是TOTP(Time-Based One-Time Password基于时间的一次性密码),其核心内容包括以下三点:

  • 一个共享密钥(一个比特序列);
  • 当前时间输入;
  • 一个签署函数。

1. 共享密钥

由服务器生成一个16位纯字母的字符串,用于在手机端上建立账户,可以手动输入,也可以生成二位维在手机上扫描
令牌的二维码的内容是一个URL:

otpauth://totp/账号名?secret=xxxxxxxxxxxxxxxx&issuer=组织名

2. 时间

服务器和手机端使用各自的时间,只要时间都是准确的,不无需与服务器做任何通信。每30秒切换一次,所以时间用的当前时间戳/30。是但为了避免时间上的误差,服务器验证的时候可以取当前一次和后面一次,一共三次判断。

3. 签署函数

签署所使用的方法是HMAC-SHA1(哈希运算消息认证码),以一个密钥和一个消息为输入,生成一个消息摘要作为输出。用共享密钥做为secret,时间戳/30做为输入值来生成20字节的SHA1值,

4. 生成6位数密码

先把SHA1的最后4个比特数用来做索引,然后用另外的4个字节进行索引。然后将它转化为标准的32bit无符号整数,最后再进行7位数(1百万)取整,就可得到6位数字了

实现

1. 手机客户端直接下载Google Authenticator

2. 服务器验证代码如下:

import ("crypto/hmac""crypto/sha1""encoding/base32""strings""time""util/log"
)func toBytes(value int64) []byte {var result []bytemask := int64(0xFF)shifts := [8]uint16{56, 48, 40, 32, 24, 16, 8, 0}for _, shift := range shifts {result = append(result, byte((value>>shift)&mask))}return result
}func toUint32(bytes []byte) uint32 {return (uint32(bytes[0]) << 24) + (uint32(bytes[1]) << 16) +(uint32(bytes[2]) << 8) + uint32(bytes[3])
}func oneTimePassword(key []byte, value []byte) uint32 {// sign the value using HMAC-SHA1hmacSha1 := hmac.New(sha1.New, key)hmacSha1.Write(value)hash := hmacSha1.Sum(nil)// We're going to use a subset of the generated hash.// Using the last nibble (half-byte) to choose the index to start from.// This number is always appropriate as it's maximum decimal 15, the hash will// have the maximum index 19 (20 bytes of SHA1) and we need 4 bytes.offset := hash[len(hash)-1] & 0x0F// get a 32-bit (4-byte) chunk from the hash starting at offsethashParts := hash[offset : offset+4]// ignore the most significant bit as per RFC 4226hashParts[0] = hashParts[0] & 0x7Fnumber := toUint32(hashParts)// size to 6 digits// one million is the first number with 7 digits so the remainder// of the division will always return < 7 digitspwd := number % 1000000return pwd
}// getCode 获取验证码
func getCode(secretKey string, epochSeconds int64) (code int32) {secretKeyUpper := strings.ToUpper(secretKey)key, err := base32.StdEncoding.DecodeString(secretKeyUpper)if err != nil {log.Error(err)return}// generate a one-time password using the time at 30-second intervalscode = int32(oneTimePassword(key, toBytes(epochSeconds/30)))return
}// 验证,传入验证key和code代码,返回验证是否成功
func CheckCode(secretKey string, code int32) bool {// 当前google值epochSeconds := time.Now().Unix()if getCode(secretKey, epochSeconds ) == code {return true}// 前30秒google值if getCode(secretKey, epochSeconds -30) == code {return true}// 后30秒google值if getCode(secretKey, epochSeconds +30) == code {return true}return false
}
http://www.dtcms.com/a/266588.html

相关文章:

  • 东软8位MCU低功耗调试总结
  • 如何使用python识别出文件夹中全是图片合成的的PDF,并将其移动到指定文件夹
  • 【ASP.NET Core】REST与RESTful详解,从理论到实现
  • 当前主流AI智能代理框架对比分析报告
  • 分布式光伏监控系统防孤岛保护装置光功率预测
  • 【论文阅读】VARGPT-v1.1
  • Webpack构建工具
  • node.js下载教程
  • 机器学习数学基础与Python实现
  • 机器学习在智能建筑中的应用:能源管理与环境优化
  • 每日问题总结记录
  • 一、如何用MATLAB画一个三角形 代码
  • 基于AR和SLAM技术的商场智能导视系统技术原理详解
  • 京东小程序JS API仓颉改造实践
  • 深圳安锐科技发布国内首款4G 索力仪!让斜拉桥索力自动化监测更精准高效
  • 【centos8服务如何给服务器开发3306端口】
  • Python 中线程和进程在实际项目使用中的区别和联系
  • 解决HttpServletRequest无法获取@RequestBody修饰的参数
  • Java并发性能优化|读写锁与互斥锁解析
  • Python 中的可迭代对象与迭代器:原理与项目实战
  • 【Verilog】parameter、localparam和 `define的区别
  • Android View的绘制原理详解
  • 基于虚拟化技术的网闸安全交换:物理隔离时代的智能数据流通引擎
  • 最快实现的前端灰度方案
  • python打卡day58@浙大疏锦行
  • 算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
  • 用原生 JS + Vue 实现一套可复用的前端错误监控系统
  • Python 机器学习核心入门与实战进阶 Day 2 - KNN(K-近邻算法)分类实战与调参
  • 【MATLAB代码】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,订阅专栏后可以获得完整代码
  • 计算机网络笔记(不全)