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

长春网站改版河北省网站快速备案

长春网站改版,河北省网站快速备案,怎样学电商赚钱,企业文化包括哪些内容现在为了安全Google二次验证使用越来越平凡了,所以我们自己做的一些产品中,也会用到Google Authenticator。 介绍 Google Authenticator采用的算法是TOTP(Time-Based One-Time Password基于时间的一次性密码),其核心…

现在为了安全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/wzjs/826041.html

相关文章:

  • 关于学校网站建设杭州建设监理行业协会
  • 温州做微网站深圳高端响应式网站
  • 现在哪个网站做网站好监测网站空白栏目
  • 手机音乐网站程序源码十大免费行情软件下载网站
  • 西安网站制作公司有哪家网站建设仟首先金手指13
  • 金融公司网站模板网站开发验收规范
  • 如何做网站排名泉州网站建设效率网络
  • 如何做能上传视频网站软件开发技术培训中心
  • 个人做网站做什么样的话网站建设需要哪些步骤 谢谢
  • 网站由哪三部分构成wordpress搜索代码制做
  • 设计师看什么网站怎么在百度上做推广
  • 郑州仿站定制模板建站商务网站管理的主要内容数据管理
  • 有哪些可以免费做高数题的网站企业网站建设个人博客
  • 网站建设 app开发网站架构设计师主要做什么
  • 购物网站前端浮动特效怎么做wordpress 旅行
  • 兰州建设工程信息网站广州公司注册代理
  • 建立网站一般包括什么等方式wordpress 福利模板
  • 在建设网站入账网站建设火凤凰
  • 建设网站属于什么费用网站建设合同属于什么税目
  • 如何给网站挂黑链编程猫官网
  • 怎么进不了深圳市建设局网站济南最新招聘信息今天
  • 网站一定要备案吗网站设计与制作费用
  • 网页设计网站布局分析怎么制作公司logo
  • 盛泽建设局网站网站 点击量
  • 修改网站首页排序做精神科医院网站费用
  • 做糕点的网站wordpress 二级域名附件
  • 自学网站的建设wordpress随机播放器
  • 佛山专业英文网站建设wordpress 门户网站源码
  • 富源县建设局网站分销系统商城定制开发
  • x域名免费网站网页型网站