【问题标题】:looking for c# equivalent of php's password-verify()寻找与 php 的 password-verify() 等效的 c#
【发布时间】:2014-05-02 04:16:53
【问题描述】:

我需要将一堆用户帐户 Moodle 导入用 c# 编写的系统。

Moodle 使用 password_hash() 函数来创建密码的哈希值。我需要能够在 c# 中验证这些密码。

换句话说,我正在寻找 PHP 密码验证功能的 c# 实现( http://www.php.net/manual/en/function.password-verify.php)。

我用谷歌搜索了一下,但真的找不到任何接近的东西,所以我在问,希望避免重新发明轮子:-)

谢谢!

【问题讨论】:

    标签: c# php passwords cryptography


    【解决方案1】:

    知道了!

    首先通过 NuGet 包安装 CryptSharp。 (使用 2.0“官方”包),顺便说一下,BCrypt.net 不适合我。

    然后:

    using CryptSharp;
    bool matches = Crypter.CheckPassword("password goes here", "hash goes here");
    

    请注意,哈希应该以以下内容开头: “$2y$...”

    像魅力一样工作! :-)

    【讨论】:

    • 您能否提供这个有用的 .NET BCrypt 实现的链接?
    • zer7.com/software/cryptsharp 是我使用的那个。通过 NuGet 包管理器也很容易安装。
    • 伙计,我真的希望这对我有用。我正在尝试这个,但我总是得到错误的回报。做了一些搜索,发现了你的帖子,所以想知道为什么它不起作用。以“$2y$”开头。我从 NuGet 中删除了 2.1.0 版本。我错过了什么?
    • 这不太一样。 Martin Steel's BCrypt.NET fork 在恒定时间内比较 bcrypt 哈希值,CryptSharp 不比较 yet
    • 刚刚使用了@Filip提供的同一链接的CryptSharpOfficial,v2.10仍然有效。
    【解决方案2】:

    好吧,我知道您不想为它编写代码,.Net 有一个内置的密码库,可以计算哈希并对其进行加密。 您必须通过导入 Security.Cryptography 来使用它。您可以将结果与保存在数据库中的结果进行比较。这是代码。

    class Program
    {
        static int SaltValueSize = 8;
        static void Main(string[] args)
        {
            string pass = "Password";
            string result = ComputeHash(pass, new MD5CryptoServiceProvider());
            Console.WriteLine("Original: " + pass + "\nEncrypted: " + result);
            Console.WriteLine("Is user valid: " + IsUserValid("UserName", pass));
            Console.WriteLine("With Salt, Original: " + pass + "\nEcrypted: " + System.Text.Encoding.Default.GetString(ComputePasswordHash(pass, salted)));
            Console.ReadLine();
    
        }
        private static byte[] ComputePasswordHash(string password, int salt)
        {
            byte[] saltBytes = new byte[4];
            saltBytes[0] = (byte)(salt >> 24);
            saltBytes[1] = (byte)(salt >> 16);
            saltBytes[2] = (byte)(salt >> 8);
            saltBytes[3] = (byte)(salt);
    
            byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
    
            byte[] preHashed = new byte[saltBytes.Length + passwordBytes.Length];
            System.Buffer.BlockCopy(passwordBytes, 0, preHashed, 0, passwordBytes.Length);
            System.Buffer.BlockCopy(saltBytes, 0, preHashed, passwordBytes.Length, saltBytes.Length);
    
            SHA1 sha1 = SHA1.Create();
            return sha1.ComputeHash(preHashed);
        }
    
    
        public static string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
    
            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
    
            return BitConverter.ToString(hashedBytes);
        }
    
        public static bool IsUserValid(string userName, string password)
        {
            bool isValid;
            string result = VerifyPassword(password);
            // isValid = Your database call in a form of Inverted statement which you
            //can check if the user with the hashed password exists or Not
            return isValid;
        }
    
        public static string VerifyPassword(string password)
        {
            return ComputeHash(password, new MD5CryptoServiceProvider());
        }
    
    
    }
    

    【讨论】:

    • 嗨艾哈迈德!感谢您尝试提供帮助,但您的解决方案将不起作用。 "$2y$" 表示 MD5CryptoServiceProvider 无法处理的基于 Blowfish 的散列。
    • 这是非常糟糕的密码散列。你忘了加盐了,MD5 太快了。
    • 这只是一个简单的方法,Filip 没有提到盐,我只是向他展示 .Net 有一个内置库。
    • 我添加了一种盐方法
    • 密码请不要使用MD5。请不要使用任何给定散列函数的单遍(或什至少数遍)作为密码。请务必阅读How to securely hash passwords?,尤其包括 Thomas Porrin 的综合答案(简称:使用 BCrypt、SCrypt 或 PBKDF2)。 RFC2898DeriveBytes 是股票 .NET (SHA-1),Jither wrote SHA-2 PBKDF2 code
    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2022-07-19
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多