Google Authenticator(谷歌身份验证器)C#版
Google Authenticator(谷歌身份验证器)
什么是认证器?怎么对接?
Google Authenticator(谷歌身份验证器)是微软推出的一个动态密令工具,它有两种密令模式。分别是“TOTP 基于时间”、“HOTP 基于计数器”,通过手机上 简单的设置就可以设定自己独一的动态密令, 那么我们怎么将我们的程序和认证器进行对接呢?其实谷歌认证器并不是需要我们对接这个工具的API而是通过算法来决定,谷歌使用使用HMAC算法生成密令,通过基于次数或者基于时间两个模板进行计算,因此在程序中只需要使用相同的算法即可与之匹配。
TOTP 基于时间
- HMAC算法使用固定为HmacSHA1
- 更新时长固定为30秒
- APP端输入数据维度只有两个:账户名称(自己随意填写方便自己查看)和base32格式的key
HOTP 基于计数器
基于计数器模式是根据一个共享秘钥K和一个C计数器进行算法计算
认证器安装
效果图

容
案例
控制台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace GoogleAuthenticator
{
class Program
{
static void Main(string[] args)
{
long duration = ;
string key = "xeon997@foxmail.com";
GoogleAuthenticator authenticator = new GoogleAuthenticator(duration, key);
var mobileKey = authenticator.GetMobilePhoneKey(); while (true)
{ Console.WriteLine("手机端秘钥为:" + mobileKey); var code = authenticator.GenerateCode();
Console.WriteLine("动态验证码为:" + code); Console.WriteLine("刷新倒计时:" + authenticator.EXPIRE_SECONDS); System.Threading.Thread.Sleep();
Console.Clear();
}
}
}
}
认证器类
using GoogleAuthorization;
using System;
using System.Security.Cryptography;
using System.Text;
namespace GoogleAuthenticator
{
public class GoogleAuthenticator
{
/// <summary>
/// 初始化验证码生成规则
/// </summary>
/// <param name="key">秘钥(手机使用Base32码)</param>
/// <param name="duration">验证码间隔多久刷新一次(默认30秒和google同步)</param>
public GoogleAuthenticator(long duration = , string key = "xeon997@foxmail.com")
{
this.SERECT_KEY = key;
this.SERECT_KEY_MOBILE = Base32.ToString(Encoding.UTF8.GetBytes(key));
this.DURATION_TIME = duration;
} /// <summary>
/// 间隔时间
/// </summary>
private long DURATION_TIME { get; set; } /// <summary>
/// 迭代次数
/// </summary>
private long COUNTER
{
get
{
return (long)(DateTime.UtcNow - new DateTime(, , , , , , DateTimeKind.Utc)).TotalSeconds / DURATION_TIME;
}
} /// <summary>
/// 秘钥
/// </summary>
private string SERECT_KEY { get; set; } /// <summary>
/// 手机端输入的秘钥
/// </summary>
private string SERECT_KEY_MOBILE { get; set; } /// <summary>
/// 到期秒数
/// </summary>
public long EXPIRE_SECONDS
{
get
{
return (DURATION_TIME - (long)(DateTime.UtcNow - new DateTime(, , , , , , DateTimeKind.Utc)).TotalSeconds % DURATION_TIME);
}
} /// <summary>
/// 获取手机端秘钥
/// </summary>
/// <returns></returns>
public string GetMobilePhoneKey()
{
if (SERECT_KEY_MOBILE == null)
throw new ArgumentNullException("SERECT_KEY_MOBILE");
return SERECT_KEY_MOBILE;
} /// <summary>
/// 生成认证码
/// </summary>
/// <returns>返回验证码</returns>
public string GenerateCode()
{
return GenerateHashedCode(SERECT_KEY, COUNTER);
} /// <summary>
/// 按照次数生成哈希编码
/// </summary>
/// <param name="secret">秘钥</param>
/// <param name="iterationNumber">迭代次数</param>
/// <param name="digits">生成位数</param>
/// <returns>返回验证码</returns>
private string GenerateHashedCode(string secret, long iterationNumber, int digits = )
{
byte[] counter = BitConverter.GetBytes(iterationNumber); if (BitConverter.IsLittleEndian)
Array.Reverse(counter); byte[] key = Encoding.ASCII.GetBytes(secret); HMACSHA1 hmac = new HMACSHA1(key, true); byte[] hash = hmac.ComputeHash(counter); int offset = hash[hash.Length - ] & 0xf; int binary =
((hash[offset] & 0x7f) << )
| ((hash[offset + ] & 0xff) << )
| ((hash[offset + ] & 0xff) << )
| (hash[offset + ] & 0xff); int password = binary % (int)Math.Pow(, digits); // 6 digits return password.ToString(new string('', digits));
}
}
}
Base32转码类
using System;
namespace GoogleAuthorization
{
public static class Base32
{
public static byte[] ToBytes(string input)
{
if (string.IsNullOrEmpty(input))
{
throw new ArgumentNullException("input");
} input = input.TrimEnd('=');
int byteCount = input.Length * / ;
byte[] returnArray = new byte[byteCount]; byte curByte = , bitsRemaining = ;
int mask = , arrayIndex = ; foreach (char c in input)
{
int cValue = CharToValue(c); if (bitsRemaining > )
{
mask = cValue << (bitsRemaining - );
curByte = (byte)(curByte | mask);
bitsRemaining -= ;
}
else
{
mask = cValue >> ( - bitsRemaining);
curByte = (byte)(curByte | mask);
returnArray[arrayIndex++] = curByte;
curByte = (byte)(cValue << ( + bitsRemaining));
bitsRemaining += ;
}
} if (arrayIndex != byteCount)
{
returnArray[arrayIndex] = curByte;
} return returnArray;
} public static string ToString(byte[] input)
{
if (input == null || input.Length == )
{
throw new ArgumentNullException("input");
} int charCount = (int)Math.Ceiling(input.Length / 5d) * ;
char[] returnArray = new char[charCount]; byte nextChar = , bitsRemaining = ;
int arrayIndex = ; foreach (byte b in input)
{
nextChar = (byte)(nextChar | (b >> ( - bitsRemaining)));
returnArray[arrayIndex++] = ValueToChar(nextChar); if (bitsRemaining < )
{
nextChar = (byte)((b >> ( - bitsRemaining)) & );
returnArray[arrayIndex++] = ValueToChar(nextChar);
bitsRemaining += ;
} bitsRemaining -= ;
nextChar = (byte)((b << bitsRemaining) & );
} if (arrayIndex != charCount)
{
returnArray[arrayIndex++] = ValueToChar(nextChar);
while (arrayIndex != charCount) returnArray[arrayIndex++] = '=';
} return new string(returnArray);
} private static int CharToValue(char c)
{
var value = (int)c; if (value < && value > )
{
return value - ;
}
if (value < && value > )
{
return value - ;
}
if (value < && value > )
{
return value - ;
} throw new ArgumentException("Character is not a Base32 character.", "c");
} private static char ValueToChar(byte b)
{
if (b < )
{
return (char)(b + );
} if (b < )
{
return (char)(b + );
} throw new ArgumentException("Byte is not a value Base32 value.", "b");
}
}
}
总结
需要注意的坑
移动端下载的认证器的秘钥key是通过base32转码得到的,而程序端是直接输入源码。
如原秘钥为xeon997@foxmail.com生成的base32码PBSW63RZHE3UAZTPPBWWC2LMFZRW63I=才是移动端需要输入的秘钥。
在网上找了很多资料没有发现关于C#的案例,所以在此记录一下自己遇到的坑,让更多的人能够跳过这个坑。
案例地址:
git:https://github.com/CN-Yi/GoogleAuthenticator
gitee:https://gitee.com/hsyi/GoogleAuthenticator
在此感谢提供学习资料的大神们,如果有错误的地方欢迎留言。
Google Authenticator(谷歌身份验证器)C#版的更多相关文章
- Google Authenticator(谷歌身份验证器)
<!DOCTYPE html>Google Authenticator(谷歌身份验证器) ] Google Authenticator(谷歌身份验证器) Google Authentica ...
- Google authenticator 谷歌身份验证,实现动态口令
Google authenticator 谷歌身份验证,实现动态口令 google authenticator php 服务端 使用PHP类 require_once '../PHPGangsta/G ...
- 七牛云如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般情况下,点账户名——账户设置——安全设置,即可开通两步验证 具体步骤见链接 七牛云如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序(官网)对比谷歌身份验证器APP ...
- humlbe bundle如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 humlbe bundle如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的 ...
- R星游戏如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 R星游戏如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载 ...
- WBF交易所如何使用二次验证码/谷歌身份验证器
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 WBF交易所如何使用二次验证码/谷歌身份验证器 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载app 2.验证码 ...
- 关于虎信如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 虎信如何绑定二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载ap ...
- paypal支付平台如何使用二次验证码_虚拟MFA_两步验证_谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 paypal支付平台如何使用二次验证码_虚拟MFA_两步验证_谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的优势 ...
- SmartMS如何使用二次验证码/虚拟MFA/两步验证/谷歌身份验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 SmartMS如何使用二次验证码/虚拟MFA/两步验证/谷歌身份验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无 ...
随机推荐
- hibernate课程 初探单表映射2-5 session详解(上)
1 本章目的:获得session的两种方式: openSession 和 getCurrentSession 2 两种session的使用方法 1openSession可以直接写,getCurrent ...
- Git入门与常用操作简述
下载gitbash及使用指南: http://rogerdudler.github.io/git-guide/index.zh.html 初次使用 配置用户名和邮箱: $ git config --g ...
- 【Mood-14】龙虎榜 活跃在github中的1000位中国开发者
Last cache created on 2015-01-07 by Github API v3. ♥ made by hzlzh just for fun. Rank Gravatar usern ...
- COGS 1191. [Tyvj Feb11] 猫咪的进化
★ 输入文件:neko.in 输出文件:neko.out 简单对比时间限制:1 s 内存限制:128 MB [背景] 对于一只猫咪来说,它是有九条命的.但是并不是所有的猫咪都是这样,只 ...
- Sql Server配置管理器与 Sql Server Management Studio
起初只安装了Sql Server配置管理器,之后用Navicat连接,总是报错(命名管道提供程序: 无法打开与 SQL Server 的连接 [53]) (另记:Navicat的“主机名或IP地址:” ...
- innobackupex 全备、增备脚本
全备脚本:innobackupex --defaults-file=/etc/my.cnf --user root --password mypasswd /mydata/fullbak/ 增备脚本: ...
- openlayers 初步认识(转)
OpenLayers是一个开源的js框架,用于在您的浏览器中实现地图浏览的效果和基本的zoom,pan等功能.OpenLayers支持的地图来源 包括了WMS,GoogleMap,KaMap,MSVi ...
- 【BZOJ4810】[YNOI2017] 由乃的玉米田(莫队+bitset)
点此看题面 大致题意: 给你一段序列,每次询问一段区间内是否存在两个数的差或和或积为\(x\). 莫队算法 看到区间询问+可以离线,首先想到了莫队啊. 但是,在较短的时间内更新信息依然比较难以实现. ...
- 【洛谷4011】孤岛营救问题(状压SPFA)
点此看题面 大致题意: 有一个\(N*M\)的四联通迷宫,相邻两个可能互通,可能有一扇门,也可能有一堵墙.对于第\(i\)类的门,你需要有第\(i\)类的钥匙才可以通过.问你从\((1,1)\)到达\ ...
- Problem J: 搜索基础之红与黑
Problem J: 搜索基础之红与黑 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 170 Solved: 100[Submit][Status][ ...