我的 C# 代码如下:
private static string password = "Password";
private static string salt = "SALT";
private static string hashAlgorithm = "SHA1";
private static int iterations = 2;
var saltValueBytes = Encoding.UTF8.GetBytes(salt);
var passwordKey = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, iterations)
...
我需要在 Mac 中实现相同的功能,我了解到 Opnessl 实现了相关方法(即 libcrypto)。
Opnessl 中与上述代码等效的方法是什么?
最佳答案
这展示了如何使用 OpenSSL 实现 PBKDF1,根据文档,这是 PasswordDeriveBytes
使用的算法。
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
void pbkdf1(const char *password, const char *salt, long iter, unsigned char dk[SHA_DIGEST_LENGTH])
{
size_t pwlen = strlen(password);
size_t dlen = pwlen + 8;
unsigned char *buf;
if (dlen > SHA_DIGEST_LENGTH)
buf = malloc(dlen);
else
buf = malloc(SHA_DIGEST_LENGTH);
memcpy(buf, password, pwlen);
strncpy((char *)buf + pwlen, salt, 8);
while (iter-- > 0)
{
SHA1(buf, dlen, buf);
dlen = SHA_DIGEST_LENGTH;
}
memcpy(dk, buf, SHA_DIGEST_LENGTH);
free(buf);
}
关于c# - 相当于openssl中的PasswordDeriveBytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3855526/