From ac2c52c11afeda9f1897805a0d1b155b983660ce Mon Sep 17 00:00:00 2001
From: milimoe <110188673+milimoe@users.noreply.github.com>
Date: Sat, 21 Oct 2023 02:21:13 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B92FA.KEY=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E7=9A=84=E4=BF=9D=E5=AD=98=E8=B7=AF=E5=BE=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Api/Utility/TwoFactorAuthenticator.cs | 64 +++++++++++++++++++++++----
1 file changed, 55 insertions(+), 9 deletions(-)
diff --git a/Api/Utility/TwoFactorAuthenticator.cs b/Api/Utility/TwoFactorAuthenticator.cs
index b4a0755..a1aeae4 100644
--- a/Api/Utility/TwoFactorAuthenticator.cs
+++ b/Api/Utility/TwoFactorAuthenticator.cs
@@ -1,6 +1,7 @@
-using System.Security.Cryptography;
+using System.Security.Cryptography;
using System.Text;
using Milimoe.FunGame.Core.Api.Transmittal;
+using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Api.Utility
{
@@ -9,8 +10,20 @@ namespace Milimoe.FunGame.Core.Api.Utility
///
public class TwoFactorAuthenticator
{
- private readonly SQLHelper SQLHelper;
+ ///
+ /// SQLHelper 允许为空
+ ///
+ private readonly SQLHelper? SQLHelper;
+ ///
+ /// 不使用SQL模式
+ ///
+ public TwoFactorAuthenticator() { }
+
+ ///
+ /// 使用SQL模式 记录对应账号的密文到数据库中
+ ///
+ ///
public TwoFactorAuthenticator(SQLHelper SQLHelper)
{
this.SQLHelper = SQLHelper;
@@ -37,7 +50,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
{
// TODO
// 使用username获取此账号记录在案的2FAKey,获取此时间戳内的验证码是否一致。
- SQLHelper.Execute();
+ SQLHelper?.Execute();
return true;
}
@@ -51,13 +64,23 @@ namespace Milimoe.FunGame.Core.Api.Utility
///
private const int DIGITS = 6;
+ ///
+ /// ----- PUBLIC KEY -----
+ ///
+ private const string PUBLICKEY = "----- PUBLIC KEY -----\r\n";
+
+ ///
+ /// ----- SECRET SIGN -----
+ ///
+ private const string SECRETSIGN = "----- SECRET SIGN -----\r\n";
+
///
/// 创键私钥,用于绑定账号,并生成两个文件,需要用户保存
///
- public static void CreateSecretKey()
+ public void CreateSecretKey(string username)
{
- string publicpath = "public.key"; // 公钥(密文)文件路径
- string privatepath = "private.key"; // 私钥文件路径
+ // 秘钥文件路径
+ string keypath = "authenticator.key";
// 创建RSA实例
using RSACryptoServiceProvider rsa = new();
@@ -67,16 +90,20 @@ namespace Milimoe.FunGame.Core.Api.Utility
string privatekey = rsa.ToXmlString(true);
// 要加密的明文
- string plain = Base32Encode(RandomNumberGenerator.GetBytes(10));
+ byte[] random = RandomNumberGenerator.GetBytes(10);
+ string randomstring = General.DefaultEncoding.GetString(random);
+ // TODO 记录对应账号的密文
+ SQLHelper?.Execute();
+ string plain = Base32Encode(random);
// 加密明文,获得密文
string secret = Encryption.RSAEncrypt(plain, publickey);
// 保存密文到文件
- File.WriteAllText(publicpath, secret);
+ File.WriteAllText(keypath, PUBLICKEY + secret + "\r\n");
// 保存私钥到文件
- File.WriteAllText(privatepath, privatekey);
+ File.AppendAllText(keypath, SECRETSIGN + privatekey);
}
///
@@ -181,5 +208,24 @@ namespace Milimoe.FunGame.Core.Api.Utility
}
return result;
}
+
+ ///
+ /// 拆分字符串中的密文和私钥
+ ///
+ ///
+ ///
+ ///
+ public static bool SplitKeyFile(string content, out string[] strs)
+ {
+ strs = content.Split(SECRETSIGN);
+ if (strs.Length == 2)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
}
}