feat: BCrypt加密方法添加Salt生成和检查逻辑。

main
NoahLan 10 months ago
parent a213bd1316
commit 5a23d6f145

@ -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);
}
/// <summary>
/// 检查密码是否正确
/// 通过再次生成一次密码匹配
/// </summary>
/// <param name="encrypt"></param>
/// <param name="password"></param>
/// <param name="salt"></param>
/// <param name="cost"></param>
/// <returns></returns>
public static bool Check(string encrypt, string password, string salt, int cost)
{
var passStr = Generate(password, salt, cost).ToString();
return string.Equals(encrypt, passStr);
}
}
Loading…
Cancel
Save