您好,我正在为学校开展一个 C# 项目,我正在用 C# 创建一个基本的登录/注册系统,并将 MySQL 用于我的数据库。我的问题是如何使用 md5 散列密码并将散列后的密码插入数据库?
我没有任何示例代码,因为我还没有开始这个项目。直到春假之后才到期。
最佳答案
这是一个扩展方法的简单示例... 用法... string.Sha256() like "password".Sha256()
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Test
{
/// <summary>
/// Extension methods for hashing strings
/// </summary>
public static class HashExtensions
{
/// <summary>
/// Creates a SHA256 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash</returns>
public static string Sha256(this string input)
{
if (!string.IsNullOrEmpty(input))
{
using (var sha = SHA256.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
else
{
return string.Empty;
}
}
}
}
关于C#登录系统,在将密码插入数据库之前需要帮助散列密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50399685/