TOTP 介绍及基于 C# 的简单实现

Intro

TOTP 是基于时间的一次性密码生成算法,它由 RFC 6238 定义。和基于事件的一次性密码生成算法不同 HOTP,TOTP 是基于时间的,它和 HOTP 具有如下关系:

  1. TOTP = HOTP(K, T)

  2. HOTP(K,C) = Truncate(HMAC-SHA-1(K,C))

其中:

  • T:T = (Current Unix time - T0) / X, T0 = 0,X = 30

  • K:客户端和服务端的共享密钥,不同的客户端的密钥各不相同。

  • HOTP:该算法请参考 RFC,也可参考 理解 HMAC-Based One-Time Password Algorithm

TOTP 算法是基于 HOTP 的,对于 HOTP 算法来说,HOTP 的输入一致时始终输出相同的值,而 TOTP 是基于时间来算出来的一个值,可以在一段时间内(官方推荐是30s)保证这个值是固定以实现,在一段时间内始终是同一个值,以此来达到基于时间的一次性密码生成算法,使用下来整体还不错,有个小问题,如果需要实现一个密码只能验证一次需要自己在业务逻辑里实现,只能自己实现,TOTP 只负责生成和验证。

C# 实现 TOTP

实现代码

  1. using System;

  2. using System.Security.Cryptography;

  3. using System.Text;

  4. namespace WeihanLi.Totp

  5. {

  6. public class Totp

  7. {

  8. private readonly OtpHashAlgorithm _hashAlgorithm;

  9. private readonly int _codeSize;

  10. public Totp() : this(OtpHashAlgorithm.SHA1, 6)

  11. {

  12. }

  13. public Totp(OtpHashAlgorithm otpHashAlgorithm, int codeSize)

  14. {

  15. _hashAlgorithm = otpHashAlgorithm;

  16. // valid input parameter

  17. if (codeSize <= 0 || codeSize > 10)

  18. {

  19. throw new ArgumentOutOfRangeException(nameof(codeSize), codeSize, "length must between 1 and 9");

  20. }

  21. _codeSize = codeSize;

  22. }

  23. private static readonly Encoding Encoding = new UTF8Encoding(false, true);

  24. public virtual string Compute(string securityToken) => Compute(Encoding.GetBytes(securityToken));

  25. public virtual string Compute(byte[] securityToken) => Compute(securityToken, GetCurrentTimeStepNumber());

  26. private string Compute(byte[] securityToken, long counter)

  27. {

  28. HMAC hmac;

  29. switch (_hashAlgorithm)

  30. {

  31. case OtpHashAlgorithm.SHA1:

  32. hmac = new HMACSHA1(securityToken);

  33. break;

  34. case OtpHashAlgorithm.SHA256:

  35. hmac = new HMACSHA256(securityToken);

  36. break;

  37. case OtpHashAlgorithm.SHA512:

  38. hmac = new HMACSHA512(securityToken);

  39. break;

  40. default:

  41. throw new ArgumentOutOfRangeException(nameof(_hashAlgorithm), _hashAlgorithm, null);

  42. }

  43. using (hmac)

  44. {

  45. var stepBytes = BitConverter.GetBytes(counter);

  46. if (BitConverter.IsLittleEndian)

  47. {

  48. Array.Reverse(stepBytes); // need BigEndian

  49. }

  50. // See https://tools.ietf.org/html/rfc4226

  51. var hashResult = hmac.ComputeHash(stepBytes);

  52. var offset = hashResult[hashResult.Length - 1] & 0xf;

  53. var p = "";

  54. for (var i = 0; i < 4; i++)

  55. {

  56. p += hashResult[offset + i].ToString("X2");

  57. }

  58. var num = Convert.ToInt64(p, 16) & 0x7FFFFFFF;

  59. //var binaryCode = (hashResult[offset] & 0x7f) << 24

  60. // | (hashResult[offset + 1] & 0xff) << 16

  61. // | (hashResult[offset + 2] & 0xff) << 8

  62. // | (hashResult[offset + 3] & 0xff);

  63. return (num % (int)Math.Pow(10, _codeSize)).ToString();

  64. }

  65. }

  66. public virtual bool Verify(string securityToken, string code) => Verify(Encoding.GetBytes(securityToken), code);

  67. public virtual bool Verify(string securityToken, string code, TimeSpan timeToleration) => Verify(Encoding.GetBytes(securityToken), code, timeToleration);

  68. public virtual bool Verify(byte[] securityToken, string code) => Verify(securityToken, code, TimeSpan.Zero);

  69. public virtual bool Verify(byte[] securityToken, string code, TimeSpan timeToleration)

  70. {

  71. var futureStep = (int)(timeToleration.TotalSeconds / 30);

  72. var step = GetCurrentTimeStepNumber();

  73. for (int i = -futureStep; i <= futureStep; i++)

  74. {

  75. if (step + i < 0)

  76. {

  77. continue;

  78. }

  79. var totp = Compute(securityToken, step + i);

  80. if (totp == code)

  81. {

  82. return true;

  83. }

  84. }

  85. return false;

  86. }

  87. private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

  88. /// <summary>

  89. /// timestep

  90. /// 30s(Recommend)

  91. /// </summary>

  92. private static readonly long _timeStepTicks = TimeSpan.TicksPerSecond * 30;

  93. // More info: https://tools.ietf.org/html/rfc6238#section-4

  94. private static long GetCurrentTimeStepNumber()

  95. {

  96. var delta = DateTime.UtcNow - _unixEpoch;

  97. return delta.Ticks / _timeStepTicks;

  98. }

  99. }

  100. }

使用方式:

  1. var otp = new Totp(OtpHashAlgorithm.SHA1, 4); // 使用 SHA1算法,输出4位

  2. var secretKey = "12345678901234567890";

  3. var output = otp.Compute(secretKey);

  4. Console.WriteLine($"output: {output}");

  5. Thread.Sleep(1000 * 30);

  6. var verifyResult = otp.Verify(secretKey, output); // 使用默认的验证方式,30s内有效

  7. Console.WriteLine($"Verify result: {verifyResult}");

  8. verifyResult = otp.Verify(secretKey, output, TimeSpan.FromSeconds(60)); // 指定可容忍的时间差,60s内有效

  9. Console.WriteLine($"Verify result: {verifyResult}");

输出示例:

Reference

  • https://tools.ietf.org/html/rfc4226

  • https://tools.ietf.org/html/rfc6238

  • http://wsfdl.com/algorithm/2016/04/05/%E7%90%86%E8%A7%A3HOTP.html

  • http://wsfdl.com/algorithm/2016/04/14/%E7%90%86%E8%A7%A3TOTP.html

  • https://www.cnblogs.com/voipman/p/6216328.html

TOTP 介绍及基于 C# 的简单实现相关推荐

  1. TOTP 介绍及基于C#的简单实现

    TOTP 介绍及基于C#的简单实现 Intro TOTP 是基于时间的一次性密码生成算法,它由 RFC 6238 定义.和基于事件的一次性密码生成算法不同 HOTP,TOTP 是基于时间的,它和 HO ...

  2. springboot mysql行锁_SpringBoot基于数据库实现简单的分布式锁

    本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...

  3. mysql binlog update_mysql binlog 简单介绍与基于binlog数据恢复

    mysql binlog 简单介绍与基于binlog数据恢复 通过备份文件恢复 binlog(本节重点) binlog 二进制日志文件 show variables like 'log_bin'; 二 ...

  4. 基于Golang的简单web服务程序开发——CloudGo

    基于Golang的简单web服务程序开发--CloudGo[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 (1)基本要求 (2)扩展要求 三.具体 ...

  5. Microwindows及基于Nano-X的简单程序开发

    http://www.rdxx.com 05年09月13日 22:26 Blog.ChinaUnix.net Nano-X是一种图形编程接口,和Win32一样,在上面我们可以编写自己的应用程序,下面转 ...

  6. 基于RxJava2+Retrofit2简单易用的网络请求实现

    代码地址如下: http://www.demodashi.com/demo/13473.html 简介 基于RxJava2+Retrofit2实现简单易用的网络请求,结合android平台特性的网络封 ...

  7. iOS之基于FreeStreamer的简单音乐播放器(模仿QQ音乐)

    代码地址如下: http://www.demodashi.com/demo/11944.html 天道酬勤 前言 作为一名iOS开发者,每当使用APP的时候,总难免会情不自禁的去想想,这个怎么做的?该 ...

  8. 基于人人网的简单爬虫(一)——正则表达式

    应课程实验要求,要写一个基于人人网的简单爬虫.实验要求如下: 学会使用一种编程语言实现爬取人人网关系网络的程序.该程序功能如下: 1.  能够输入用户登陆所产生的cookie,允许爬虫对人人网进行爬取 ...

  9. python documents in chinese_基于 Python 的简单自然语言处理实践

    基于 Python 的简单自然语言处理 Twenty News Group 语料集处理 20 Newsgroup 数据集包含了约 20000 篇来自于不同的新闻组的文档,最早由 Ken Lang 搜集 ...

最新文章

  1. java的byte与C#的异同引起的字符处理问题。
  2. 将数据导入到mysql_06955.10.2如何将CM的外部PostgreSQL数据库迁移至MySQL服务
  3. 【ubuntu+opencv3】ubuntu16.04+qt5+opencv3.2.0编译与安装
  4. 华北电力大学计算机科学与技术考研,华北电力大学吴克河教授谈计算机科学与技术专业...
  5. HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)...
  6. ios9和xcode7的适配问题
  7. [CF]Codeforces Round #546 (Div. 2)
  8. 第7章[7.18] Ext JS组件嵌入HTML页面
  9. android 人脸识别边框_虹软人脸识别 - Android Camera实时人脸追踪画框适配
  10. 2020年C题认证杯SEIR模型参数拟合
  11. 微博、微信朋友圈、QQ空间功能对比
  12. AJ-Report 初学(入门教程)
  13. android 磁场传感器应用
  14. EVE模拟器关联CRT与Wireshark
  15. 达梦数据库 find_in_set 函数适配
  16. 基于Python的毕业论文怎么写?
  17. 网友吐槽常被盒马种草 但你知道带货王是怎么来的吗?
  18. “SqlSession[xxx] was not registered for synchronization because synchronization is not active”问题成功解决
  19. PDF转CAD怎么转?试试这个办法
  20. arduino的C语言基础

热门文章

  1. sketch怎么移动图层_什么是Photoshop Express,Fix,Mix和Sketch移动应用程序?
  2. java 重置定时器_可重置Java定时器
  3. Linux之ACL权限控制
  4. 15个IT技术人员必须思考的问题
  5. loadrunner 参数化数据更新方式
  6. Android系统如何实现UI的自适应
  7. 第二十五章补充内容 5 不能为0的变量
  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
  9. ASP.NET Core Web API使用静态swagger.json文件
  10. Blazor VS React / Angular / Vue.js