2019独角兽企业重金招聘Python工程师标准>>>

Google authenticator是基于TOTP(Time-based One-time Password Algorithm)原理实现的双因子认证方案。

通过 一致算法保持手机端和服务端相同,并每30秒改变认证码。

主要对外调用方法说明:

  1. GetSecret() :获取秘钥(32位字符串)

  2. GetCode() :获取动态码
  3. GetQrcode() :获取动态码二维码内容
  4. GetQrcodeUrl() :获取动态码二维码图片地址
  5. VerifyCode() :验证动态码

otp是什么知道么?  是一次性密码,简单的说,totp是基于时间的,htop是基于次数的。

操作过程:

1、下载google 身份验证器

2、添加账号、秘钥(秘钥需要通过以下程序生成:NewGoogleAuth().GetSecret())

3、登录app、网站时,填写”google身份验证器“显示的6位数字

4、服务端验证6位数字是否正确:NewGoogleAuth().VerifyCode(secret, code)

秘钥生成原理(基于时间):

1、时间戳,精确到微秒,除以1000,除以30(动态6位数字每30秒变化一次)

2、对时间戳余数 hmac_sha1 编码

3、然后 base32 encode 标准编码

4、输出大写字符串,即秘钥

动态6位数字验证:

Google Authenticator会基于密钥和时间计算一个HMAC-SHA1的hash值,这个hash是160 bit的,然后将这个hash值随机取连续的4个字节生成32位整数,最后将整数取31位,再取模得到一个的整数。

这个就是Google Authenticator显示的数字。

在服务器端验证的时候,同样的方法来计算出数字,然后比较计算出来的结果和用户输入的是否一致。

googleAuth.go

package mainimport ("bytes""crypto/hmac""crypto/sha1""encoding/base32""encoding/binary""fmt""strings""time"
)type GoogleAuth struct {
}func NewGoogleAuth() *GoogleAuth {return &GoogleAuth{}
}func (this *GoogleAuth) un() int64 {return time.Now().UnixNano() / 1000 / 30
}func (this *GoogleAuth) hmacSha1(key, data []byte) []byte {h := hmac.New(sha1.New, key)if total := len(data); total > 0 {h.Write(data)}return h.Sum(nil)
}func (this *GoogleAuth) base32encode(src []byte) string {return base32.StdEncoding.EncodeToString(src)
}func (this *GoogleAuth) base32decode(s string) ([]byte, error) {return base32.StdEncoding.DecodeString(s)
}func (this *GoogleAuth) 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 (this *GoogleAuth) toUint32(bts []byte) uint32 {return (uint32(bts[0]) << 24) + (uint32(bts[1]) << 16) +(uint32(bts[2]) << 8) + uint32(bts[3])
}func (this *GoogleAuth) oneTimePassword(key []byte, data []byte) uint32 {hash := this.hmacSha1(key, data)offset := hash[len(hash)-1] & 0x0FhashParts := hash[offset : offset+4]hashParts[0] = hashParts[0] & 0x7Fnumber := this.toUint32(hashParts)return number % 1000000
}func (this *GoogleAuth) GetSecret() string {var buf bytes.Bufferbinary.Write(&buf, binary.BigEndian, this.un())return strings.ToUpper(this.base32encode(this.hmacSha1(buf.Bytes(), nil)))
}func (this *GoogleAuth) GetCode(secret string) (string, error) {secretUpper := strings.ToUpper(secret)secretKey, err := this.base32decode(secretUpper)if err != nil {return "", err}number := this.oneTimePassword(secretKey, this.toBytes(time.Now().Unix()/30))return fmt.Sprintf("%06d", number), nil
}func (this *GoogleAuth) GetQrcode(user, secret string) string {return fmt.Sprintf("otpauth://totp/%s?secret=%s", user, secret)
}func (this *GoogleAuth) GetQrcodeUrl(user, secret string) string {qrcode := this.GetQrcode(user, secret)return fmt.Sprintf("http://www.google.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=%s", qrcode)
}func (this *GoogleAuth) VerifyCode(secret, code string) (bool, error) {_code, err := this.GetCode(secret)fmt.Println(_code, code, err)if err != nil {return false, err}return _code == code, nil
}

main.go

package mainimport ("fmt"
)func main() {secret := NewGoogleAuth().GetSecret()code, err := NewGoogleAuth().GetCode(secret)fmt.Println(secret, code, err)
}

转载于:https://my.oschina.net/qiongtaoli/blog/3049118

golang google authenticator相关推荐

  1. 在rMBP上利用Python的onetimepass库实现Google Authenticator Application的效果

    安装onetimepass库 在Mac上最简单的安装方法是 sudo pip install onetimepass 这里我遇到一个问题,参见 http://blog.csdn.net/tao_627 ...

  2. Linux下使用Google Authenticator配置SSH登录动态验证码

    说明: 1.一般ssh登录服务器,只需要输入账号和密码. 2.本教程的目的:在账号和密码之间再增加一个 验证码,只有输入正确的验证码之后,再输入 密码才能登录.这样就增强了ssh登录的安全性. 3.账 ...

  3. Open***+AD+Google authenticator 安装、配置

    一.      网络环境如下 1.Open××× Server 外网卡eth1配置 IP:121.12.x.y MASK:255.255.255.128 GateWay:121.12.x.1 LAN ...

  4. 谷歌验证 (Google Authenticator) 的实现原理是什么?

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:徐小花 链接:http://www.zhihu.com/question/20462696/answer/18731073 ...

  5. 如何将您的Google Authenticator凭证移至新的Android手机或平板电脑

    Most of the app data on your Android is probably synced online will automatically sync to a new phon ...

  6. Google Authenticator:将其与您自己的Java身份验证服务器配合使用

    用于移动设备的Google Authenticator应用程序是一个非常方便的应用程序,它实现了TOTP算法(在RFC 6238中指定). 使用Google Authenticator,您可以生成时间 ...

  7. 如何使用Google Authenticator在ASP.NET Core中设置两因素身份验证

    介绍 (Introduction) In this article, we are going to learn how to perform two-factor authentication in ...

  8. Linux 之 利用Google Authenticator实现用户双因素认证

    一.介绍:什么是双因素认证 双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产 ...

  9. google authenticator python_Google Authenticator TOTP原理详解(以Python为例)

    http://xsboke.blog.51cto.com 如果有疑问,请点击此处,然后发表评论交流,作者会及时回复(也可以直接在当前文章评论). -------谢谢您的参考,如有疑问,欢迎交流 一. ...

最新文章

  1. 前端笔记-前端优化简要大总结
  2. 报告解读丨企服 9 大规模化获客标杆模型(附赠案例)
  3. python正则表达式修饰符_python正则表达式,看完这篇文章就够了...
  4. PHP中的错误控制运算符
  5. 转载 Socket与TCP/IP的关系 转(非常好的一篇文章!)
  6. 毛毛虫组【Beta】Scrum Meeting 3
  7. 计算机系统基础栈,计算机系统基础 (一): 程序的表示, 转换与链接 (第七周小测验)...
  8. Zener二极管(稳压二极管)型号对照表--1N52xx系列
  9. 【docker】3-配置阿里云加速
  10. simulink-EtherCAT工具箱常用模块的简要介绍
  11. Windows的CMD的NET命令net start , net stop ...
  12. Android Android 复制Assets文件到SD卡
  13. 汤晓鸥教授的一篇很有意思的文章
  14. 【线性代数(2)】n阶行列式三种定义
  15. 系统映像恢复 进不了系统_如何从Windows系统映像中恢复特定文件
  16. 开通了个人微信公众号:slbGTD,准备把GTD相关的内容写成一本书
  17. 计算机组成原理实验报告 算术逻辑单元ALU实验(源代码全)
  18. 第6章 TCP/IP路由协议故障处理
  19. [OfficeExcel] OfficeExcel2010 第20-22讲 Excel图表
  20. java实现ln10_Java数学函数

热门文章

  1. Excel之复制除了某几行之外的所有行(非专业人士,自用的土方法)
  2. [LabVIEW]子VI中移位寄存器未初始化的严重后果
  3. (69)TreeSet练习:按照长度为主关键字,自然顺序为次关键字排序
  4. Andfix学习记录
  5. find the th smallest
  6. 送你一份价值10W的非专业的面试技巧
  7. java包装类默认值_java包装类
  8. ASEMI代理ADXL345BCCZ-RL7原装ADI车规级ADXL345BCCZ-RL7
  9. 程序员面试之道(《程序员面试笔试宝典》)之求职的时候该不该只看钱?
  10. monkey日志分析详解