在c#

时间:2017-07-01 00:14:07

标签: cryptography mqtt mosquitto

我花了一整天时间尝试为mosquitto-auth-plug创建一个可用的pbkdf2密码。该程序将其设置为应该存储在mysql数据库中的方式。我有一个由auth-plug附带的程序生成的密码哈希,而mosquitto喜欢它。我无法在c#中复制它,如果有人可以提供帮助请告诉我。

public string CreatePasswordHash(string password)
    {

        var salt = GenerateRandomSalt();    
        var iterationCount = GetIterationCount();
        var hashValue = GenerateHashValue(password, salt, iterationCount);
        string result = "PBKDF2$sha256$" + iterationCount + "$" + Convert.ToBase64String(salt) + "$" + Convert.ToBase64String(hashValue);
        return result;

    }

    private int GetIterationCount()
    {
        return 901;
    }

    private static byte[] GenerateRandomSalt()
    {
        var csprng = new RNGCryptoServiceProvider();
        var salt = new byte[SaltByteLength];
        csprng.GetBytes(salt);
        return salt;
        //return GetLetter();
    }

    private static byte[] GenerateHashValue(string password, byte[] salt, int iterationCount)
    {
         byte[] hashValue;
         var valueToHash = string.IsNullOrEmpty(password) ? string.Empty : password;
         using (var pbkdf2 = new Rfc2898DeriveBytes(valueToHash, salt, iterationCount))
         {
             hashValue = pbkdf2.GetBytes(DerivedKeyLength);
         }
        return hashValue;


    }

--- ----- EDIT

Rfc2898DeriveBytes州 - 通过使用基于HMACSHA1的伪随机数生成器实现基于密码的密钥派生功能PBKDF2。

程序/ auth-plug似乎正在使用sha256是否有使用它的c#PBKDF2。

2 个答案:

答案 0 :(得分:0)

正如您在编辑中已经说过的那样,问题似乎是mosquitto插件(根据源代码仅支持SHA-256)和.NET实现(只能执行)之间使用的散列函数的不同之处做SHA-1)。

BouncyCastle中提供了更灵活的PBKDF2实现,可以找到更轻量级的实现here。如果你对这两个人不满意,你可以选择自己实施PBKDF2,is not really hard

答案 1 :(得分:0)

看来真的没有答案,也没有多少人尝试过。我想出了问题,在联系了mosquitto-auth-plug作者之后,他觉得将我的解决方案添加到插件github repo上的contrib文件夹会很好。

所以现在如果你需要一个针对mosquitto-auth-plug的c#散列算法,那就去git这里的repo吧

https://github.com/jpmens/mosquitto-auth-plug/tree/master/contrib/C%23

并按照我的指示---如果您有任何问题,请告诉我

相关问题