如何使用 c# 对密码值进行加盐和哈希处理?如何使用、加盐、密码

2023-09-07 09:33:39 作者:清风伴烈酒

大家好,

我从 在数据库中存储密码的首选方法...

如何使用 c# 对密码值加盐和散列?

How to salt and hash a password value using c#?

如何比较数据库中存储的值和用户给出的值?

How to compare both the values stored in DB and the one given by the user?

推荐答案

最流行的方法是使用散列算法.这里有一篇很棒的博文 关于如何使用 MD5 算法对字符串进行哈希处理,但在 System.Cryptography 命名空间中还有许多其他示例.

The most popular way to do this is using a hashing algorithm. There's an excellent blog post here about how to use the MD5 algorithm to hash a string, but there are many other examples in the System.Cryptography namespace.

至于 #2,关于其工作原理的一般分步指南如下:

As for #2, the general step-by-step guide to how this would work would be the following:

注册时:

使用您指定的算法散列用户密码并将其存储在数据库中Salt 这个哈希(可选,但首选) Hash a user's password using your specified algorithm and store it in the database Salt this hash (optional, but preferred)

登录/用户 &密码检查:

在数据库中查找用户名如果存在,则检索散列密码对输入的密码进行哈希和加盐,并将其与检索到的密码进行比较

都是比较啰嗦的,但是很安全.

It's all relatively long-winded, but it's very secure.

关于散列和加盐这里还有另一个非常深入的指南.