C#学习教程:寻找与php的密码等价的c#——validation()Moodle使用password_hash()函数来创建密码哈希。我需要能够在C#中验证这些密码。换句话说,我正在寻找PHP密码验证功能的c#实现(http://www.php.net/manual/en/function.password-verify.php)。我用谷歌搜索了一下,但真的找不到任何东西,所以我要求避免重新发明轮子:-)谢谢!知道了!首先通过NuGet包安装CryptSharp。(使用2.0“官方”包),顺便说一句,BCrypt.net对我不起作用。然后:使用CryptSharp;boolmatches=Crypter.CheckPassword("密码在这里","散列在这里");请注意,散列应以:“$2y$...”开头!奇迹般地起作用了!?我知道你不想为它编写代码,.Net有一个内置的加密库来计算哈希值并对其进行加密。?您必须通过导入来使用Security.Cryptography。您可以将结果与保存在数据库中的结果进行比较。这是代码。以上就是C#学习教程:寻找c#等价php密码-validation()分享全部内容,如果对大家有用还需要详细了解C#学习教程,希望大家多加关注-class程序{staticintSaltValueSize=8;staticvoidMain(string[]args){stringpass="密码";stringresult=ComputeHash(pass,newMD5CryptoServiceProvider());Console.WriteLine("原始:"+pass+"nEncrypted:"+result);Console.WriteLine("用户是否有效:"+IsUserValid("UserName",pass));Console.WriteLine("WithSalt,Original:"+pass+"nEcrypted:"+System.Text.Encoding.Default.GetString(ComputePasswordHash(pass,salted)));控制台.ReadLine();}privatestaticbyte[]ComputePasswordHash(stringpassword,intsalt){byte[]saltBytes=newbyte[4];saltBytes[0]=(字节)(盐>>24);saltBytes[1]=(字节)(盐>>16);saltBytes[2]=(字节)(盐>>8);saltBytes[3]=(字节)(盐);byte[]passwordBytes=UTF8Encoding.UTF8.GetBytes(密码);byte[]preHashed=newbyte[saltBytes.Length+passwordBytes.Length];System.Buffer.BlockCopy(passwordBytes,0,preHashed,0,passwordBytes.Length);System.Buffer.BlockCopy(saltBytes,0,preHashed,passwordBytes.Length,saltBytes.Length);SHA1sha1=SHA1.Create();返回sha1.ComputeHash(preHashed);}publicstaticstringComputeHash(stringinput,HashAlgorithmalgorithm){Byte[]inputBytes=Encoding.UTF8.GetBytes(input);Byte[]hashedBytes=algorithm.ComputeHash(inputBytes);returnBitConverter.ToStrings(hashed);}BytepublicstaticboolIsUserValid(stringuserName,stringpassword){boolisValid;字符串结果=验证密码(密码);//isValid=您的数据库以倒置语句的形式调用,您//可以检查具有散列密码的用户是否存在或不返回isValid;}publicstaticstringVerifyPassword(stringpassword){returnComputeHash(password,newMD5CryptoServiceProvider());转载请注明出处:
