在 C# 中为 mosquitto 的 auth-plug 生成密码
generate password for auth-plug for mosquitto in c#
我花了一整天的时间试图为 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;
}
---编辑-----
Rfc2898DeriveBytes 声明 --
通过使用基于 HMACSHA1 的伪随机数生成器实现基于密码的密钥派生功能 PBKDF2。
program/auth-plug 似乎在使用 sha256 是否有使用它的 c# PBKDF2。
正如您在编辑中所述,问题似乎是 mosquitto 插件(根据源代码,它仅支持 SHA-256)和 .NET 实现(它只能做 SHA-1)。
BouncyCastle, a more lightweight implementation can be found here. If you are not satisfied with those two, you could chose to implemnt PBKDF2 yourself, which is not really hard 中提供了更灵活的 PBKDF2 实现。
好吧,看来确实没有答案,而且没有多少人尝试过。我弄清楚了这个问题,在联系了 mosquitto-auth-plug 作者之后,他觉得我们可以将我的解决方案添加到插件 github repo 上他的 contrib 文件夹中。
现在,如果您需要 mosquitto-auth-plug 的 c# 哈希算法,只需转到 git 中的存储库
https://github.com/jpmens/mosquitto-auth-plug/tree/master/contrib/C%23
并按照我的指示操作 --- 如果您有任何问题,请告诉我
我花了一整天的时间试图为 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;
}
---编辑-----
Rfc2898DeriveBytes 声明 -- 通过使用基于 HMACSHA1 的伪随机数生成器实现基于密码的密钥派生功能 PBKDF2。
program/auth-plug 似乎在使用 sha256 是否有使用它的 c# PBKDF2。
正如您在编辑中所述,问题似乎是 mosquitto 插件(根据源代码,它仅支持 SHA-256)和 .NET 实现(它只能做 SHA-1)。
BouncyCastle, a more lightweight implementation can be found here. If you are not satisfied with those two, you could chose to implemnt PBKDF2 yourself, which is not really hard 中提供了更灵活的 PBKDF2 实现。
好吧,看来确实没有答案,而且没有多少人尝试过。我弄清楚了这个问题,在联系了 mosquitto-auth-plug 作者之后,他觉得我们可以将我的解决方案添加到插件 github repo 上他的 contrib 文件夹中。
现在,如果您需要 mosquitto-auth-plug 的 c# 哈希算法,只需转到 git 中的存储库
https://github.com/jpmens/mosquitto-auth-plug/tree/master/contrib/C%23
并按照我的指示操作 --- 如果您有任何问题,请告诉我