golang 实现Authenticator 二次验证,可用到web 登录
🕡 Sat, 06 Oct 2018 by GolangNote
Google 的Authenticator app 可计算动态码,用于app 安全登录,利用其算法,可以使用在web 用户登录的二次验证。
下面是用golang 实现的Authenticator 二次验证,验证码主要取决于secretKey
的值。
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"fmt"
"github.com/skip2/go-qrcode"
"os"
"strings"
"time"
)
func toBytes(value int64) []byte {
var result []byte
mask := 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-SHA1
hmacSha1 := 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 offset
hashParts := hash[offset : offset+4]
// ignore the most significant bit as per RFC 4226
hashParts[0] = hashParts[0] & 0x7F
number := 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 digits
pwd := number % 1000000
return pwd
}
func main() {
secretKey := "qwertyuiopasdfghqwertyuiopasdfgh" // 16*N 个字符
secretKeyUpper := strings.ToUpper(secretKey)
key, err := base32.StdEncoding.DecodeString(secretKeyUpper)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// generate a one-time password using the time at 30-second intervals
epochSeconds := time.Now().Unix()
pwd := oneTimePassword(key, toBytes(epochSeconds/30))
secondsRemaining := 30 - (epochSeconds % 30)
fmt.Printf("%06d (%d second(s) remaining)\n", pwd, secondsRemaining)
// 生成二维码供 Authenticatior (身份验证器) 扫描
authLink := "otpauth://totp/网站名?secret=" + secretKey + "&issuer=用户名"
fmt.Println(authLink)
err = qrcode.WriteFile(authLink, qrcode.Medium, 256, "qr.png")
if err != nil {
fmt.Println("write error")
}
}
运行上面的代码后就会生成一个二维码图片,使用Authenticatior 扫描,即可同步显示验证码,每30秒刷新次。
参考:
本文网址: https://golangnote.com/topic/227.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- Golang 减小gc 压力、避免内存泄漏小tips
- 用Golang 处理每分钟100万个请求
- GoLang 根据出生日期计算年龄、星座、生肖
- an error broken: unknown escape sequence: d
- golang 简单全局计数器实现
- golang 实现简单的socket 服务端/客户端
- Golang 字符串毫秒转时间格式
- goji + mysql 例子
- 用Go net 工具包获取DNS 信息
- 推荐几款开源免费的用golang写的论坛程序
- GoLang 获取变量类型
- Golang Web 自动安装、更新Let’s Encrypt 免费证书
- golang Selenium WebDriver 使用记录
- Golang Leveldb 基本使用的例子
- go 实现图片服务器缓存
- Golang io.ReadCloser 和[]byte 相互转化的方法
- Golang WebAssembly 了解一下
- groupcache 使用入门
- Golang 随机打乱数组/Slice
- GoLang 遍历目录的不同方法