From 5a23d6f1458aba47efd6ccd3bf4204b785d145a4 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Tue, 27 Feb 2024 11:33:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20BCrypt=E5=8A=A0=E5=AF=86=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=B7=BB=E5=8A=A0Salt=E7=94=9F=E6=88=90=E5=92=8C?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Crypt/BCrypt/BCrypt.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/framework/NPin.Framework.Core/Crypt/BCrypt/BCrypt.cs b/framework/NPin.Framework.Core/Crypt/BCrypt/BCrypt.cs index ac6e9f6..9b682bc 100644 --- a/framework/NPin.Framework.Core/Crypt/BCrypt/BCrypt.cs +++ b/framework/NPin.Framework.Core/Crypt/BCrypt/BCrypt.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Security.Cryptography; +using System.Text; namespace NPin.Framework.Core.Crypt.BCrypt; @@ -11,4 +12,29 @@ public static class BCrypt return Org.BouncyCastle.Crypto.Generators.BCrypt.Generate(passBytes, saltBytes, cost); } + + public static string GenerateSalt() + { + var buf = new byte[16]; + using var rand = RandomNumberGenerator.Create(); + rand.GetBytes(buf); + + return Convert.ToBase64String(buf); + } + + /// + /// 检查密码是否正确 + /// 通过再次生成一次密码匹配 + /// + /// + /// + /// + /// + /// + public static bool Check(string encrypt, string password, string salt, int cost) + { + var passStr = Generate(password, salt, cost).ToString(); + + return string.Equals(encrypt, passStr); + } } \ No newline at end of file