c# - C# 哈希算法无法正常工作
问题描述
我的用户模型和数据库中有密码哈希和密码盐供注册。我正在向我的 API 发布电子邮件和密码。我正在通过电子邮件从数据库中获取用户,并且正在对来自 API 的密码进行哈希处理。此外,我相互比较,但它不能正常工作
我经常这样工作,但我从来没有遇到过麻烦,我将我的操作系统升级到了 Windows 11。我不知道,也许是 Windows 11 的原因。
public class HashingHelper
{
public static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512())
{
passwordSalt = hmac.Key;
passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
}
}
public static bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512(passwordSalt))
{
var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
for (int i = 0; i < computedHash.Length; i++)
{
if (computedHash[i] != passwordHash[i])
{
return false;
}
}
return true;
}
}
}
CreatePasswordHash
方法是在注册之前对密码进行哈希处理。
VerifyPasswordHash
方法是比较来自 API 的密码和来自我的数据库的密码
解决方案
推荐阅读
- r - R data.table 通过返回新表的组的动态列名
- c# - 开关情况下的 Resharper 警告“源表达式始终与提供的模式匹配”
- javascript - Primevue 日历未在我的 vue 应用程序中显示图标
- ios - 在 tvOS/iOS 的 AVplayers 中缓冲和预加载视频
- laravel - Laravel 新模型缺少 created_at 和 updated_at 字段
- javascript - 如何在动态生成的对象字符串值上添加过滤器
- ruby-on-rails - 嵌套评论线程 - 视图中的递归渲染
- c# - OOP 方法隐藏和重载
- java - 将数字四舍五入到 n 十进制使用格式显示不同的结果
- php - Laravel Eloquent 如何对输出的“with”数据应用条件?