惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

博客园 - 华安

Unity 相机:正交 (Orthographic) vs 透视 (Perspective) - 华安 unity中的button中的onclick控制面板中四个参数的意思分别是什么 - 华安 微信小程序开发中触发 onshow的几种特殊情况 SQL Server备份心得 MYSQL 备份数据库 微信与支付支付功能开发 微信小程序中页面配置下拉刷新 unity中 相机没有视锥效果线框了,如何打开 JSONPath表达式 C# 中的操作JSON类 JObject cookie中的 HttpOnly 、Secure、SameSite 解释 Spring-boot 中基于 IP 的限流和自动封禁 Filter 利用Spring Boot的 filter 结合ConcurrentHashMap 实现“同一IP每分钟最多允许300个404请求,超出后禁用30分钟访问” pixi-filters中的BackdropBlurFilter使用注意事项 PixiJS中的 SplitBitmapText.chars中的每个 BitmapText的x值分析 legend隐藏不想显示的图例 spring-boot HttpServletResponse response.sendRedirect是会跳转到 http而不是https springboot获取post请求参数 Javascript如何判断是触摸屏还是PC端 JavaScript获取鼠标点一个元素,获取鼠标点击的元素内的位置 spring-boot中配置Mongodbd的问题小结 Rest Template中添加 PATCH请求。 CSS实现修改CheckBox样式 SpringBoot+Thyemleaf报错:Error resolving template Template might not exist or might not be accessible div display flex 如何出现横向滚动条
登录 用 HMAC-SHA256 实现 TwoFA(二重验证)的坑
华安 · 2026-01-27 · via 博客园 - 华安

今天按照客户的要求,用 HMAC-SHA256 实现 TwoFA(二重验证),刚开始是用 HmacSHA1 实现的,客户端 用 Microsoft Authenticator扫码,是完全正常的。

但是我换成 用 HMAC-SHA256 算法后,无论怎么用 Microsoft Authenticator 都不能成功。

生成的验证代码:

otpauth://totp/xxx.com:admin?secret=KLWKVCOK3BI6WGN6ADDWWMTELMW55P5L&issuer=xxx.com&algorithm=SHA256&digits=6&period=30

我看代码就是 SHA256,没错。就这问题我消耗了整整半天。

最后在网上看到以为大神的留言。Microsoft Authenticator实际使用 SHA1,尽管 URI 指定了 SHA256.

我立马更改 校验动态码的算法,改成用SHA1来校验,正确。

由此可以断定 Microsoft Authenticator实际使用 SHA1,尽管 URI 指定了 SHA256.

总结:

用SHA1生成的动态码,必须用 SHA1算法来校验。

用SHA256生成的动态码,必须用 SHA256算法来校验。

我们在校验的时候,无法限制客户端时用 SHA1生成的动态码,还是 SHA256生成的动态码。

所以我们两个算法都要写。只要有一个校验正确就行。

譬如:Microsoft Authenticator我在 otpauth中写了 SHA256算法,但是生成动态码还是使用的SHA1算法,控制不了。

无论使用 SHA1和SHA256生成的密匙,在SHA1和SHA256校验的时候都可以通用。因为密匙生成的时候与算法无关,就是一个动态的字符串。

生成的动态验证码是否正确,可以用这个来校验  https://totp.danhersam.com/  不过这个是用 SHA1生成的动态验证码。输入用户对应的 Your Secret Key就可以了。其它不用改。