Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用用户名密码和当前时间生成代码_C#_Hash - Fatal编程技术网

C# 使用用户名密码和当前时间生成代码

C# 使用用户名密码和当前时间生成代码,c#,hash,C#,Hash,我希望生成一个代码,该代码将根据用户的用户名和密码以及当前分钟生成,这将在windows窗体应用程序上完成,然后我在网站上重复相同的代码,当用户尝试登录时,将要求他们提供此验证码,如果它与他们将登录 我搜索了,但找不到从散列字符串生成4位代码的方法 public static string HashSHA512String(string Username, string Password) { string AllString = Username + Password

我希望生成一个代码,该代码将根据用户的用户名和密码以及当前分钟生成,这将在windows窗体应用程序上完成,然后我在网站上重复相同的代码,当用户尝试登录时,将要求他们提供此验证码,如果它与他们将登录

我搜索了,但找不到从散列字符串生成4位代码的方法

public static string HashSHA512String(string Username, string Password)
    {
        string AllString = Username + Password + DateTime.UtcNow.Minute;
        if (string.IsNullOrEmpty(AllString)) throw new ArgumentNullException();
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(AllString);
        buffer = System.Security.Cryptography.SHA512Managed.Create().ComputeHash(buffer);
        return System.Convert.ToBase64String(buffer).Substring(0, 86); 
    }

这就是我到现在为止所做的,我想把这个长字符串转换成一个4位数的代码。您能告诉我生成这个4位代码需要做什么吗?

请按如下方式修改您的代码

public static int HashSHA512String(string Username, string Password)
{
    const int RandomFactor = 27; // Added this to make the code appear more "random".
    string AllString = Username + Password + DateTime.UtcNow;
    if (string.IsNullOrEmpty(AllString)) throw new ArgumentNullException();
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(AllString);
    buffer = System.Security.Cryptography.SHA512Managed.Create().ComputeHash(buffer);
    int code = 0;
    for (var i = 0; i < buffer.Length; i++)
    {
        code += buffer[i] * RandomFactor;
        code = code % 10000;
    }

    return code;
}
public static int HashSHA512String(字符串用户名、字符串密码)
{
const int RandomFactor=27;//添加此选项使代码看起来更“随机”。
string AllString=Username+Password+DateTime.UtcNow;
if(string.IsNullOrEmpty(AllString))抛出新的ArgumentNullException();
byte[]buffer=System.Text.Encoding.UTF8.GetBytes(AllString);
buffer=System.Security.Cryptography.SHA512Managed.Create().ComputeHash(缓冲区);
int代码=0;
for(var i=0;i
这将返回一个介于0和9999之间的代码。然后,您可以从此代码创建一个零左填充字符串

不过,我建议使用完整的日期,而不是仅仅用一分钟的时间让你的哈希表给出随机结果,可能还要加一些盐

如果您希望将从上面返回的int转换为四位字符串代码,请使用此

public static string PadInt(int value)
{
    var output = value.ToString();
    while (output.Length < 4)
    {
        output = "0" + output;
    }

    return output;
}
公共静态字符串PadInt(int值)
{
var输出=value.ToString();
while(输出长度<4)
{
输出=“0”+输出;
}
返回输出;
}

您可以在上面的哈希方法中调用它,然后只返回字符串,而不是int。

4位代码是什么意思?你是在寻找一个只能假设值在0到15之间的散列吗?我的意思是,比如说9210,而不是我认为你是指一个4字节(32位)的代码时产生的很长的字符串。是的,很抱歉没有解释清楚,我只是一个初学者,不知道这些术语。所以。。。你是说一个四字节的值吗?四个十六进制数字(两字节)的值?如果是这样,您可以从
散列…
方法中提取最后这么多字节并使用它。您可能希望将该for循环包装在一个循环中,以防溢出异常,默认情况下它应该被取消选中,但以防编译器将其自身设置为选中,代码仍将工作。我将对其进行修改以考虑到这一点。谢谢:)非常感谢这正是我想要的:)