C++ PBKDF2 问题

标签 c++ hash pbkdf2

我有以下功能:

void PBKDF2_HMAC_SHA_512_string(const char* pass, const char* salt, int32_t iterations, uint32_t HashLength, char* out) {
    unsigned int i;
    HashLength = HashLength / 2;
    unsigned char* digest = new unsigned char[HashLength];
    PKCS5_PBKDF2_HMAC(pass, strlen(pass), (const unsigned char*)salt, strlen(salt), iterations, EVP_sha512(), HashLength, digest);
    for (i = 0; i < sizeof(digest); i++) {
        sprintf(out + (i * 2), "%02x", 255 & digest[i]);
    }
}

当我像下面这样调用函数时,我希望得到长度为 2400 的散列,但它返回 16:

char PBKDF2Hash[1025]; //\0 terminating space?
memset(PBKDF2Hash, 0, sizeof(PBKDF2Hash));
PBKDF2_HMAC_SHA_512_string("Password", "0123456789123456", 3500, 1024, PBKDF2Hash);
//PBKDF2Hash is now always 16 long -> strlen(PBKDF2Hash),
//while I expect it to be 2400 long?
//How is this possible and why is this happening?
//I can't figure it out

最佳答案

digestpointer , sizeof(digest)不会给出数组的长度。根据不同的平台,sizeof(digest)可能给你 4 或 8,这不是你想要的。也许你应该使用 for (i = 0; i < HashLength; i++) .

您的代码的另一个不相关的问题是,digest未在 PBKDF2_HMAC_SHA_512_string 中删除,这导致 memory leak

关于C++ PBKDF2 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062886/

相关文章:

c++ - 将函数作为类字段传递

java - 捕获 C++ lambda 表达式错误中的 JNIENV*

c++ - 分配二维数组时的内存管理

c++ - 如何将对函数的引用传递给另一个函数

c - 5 元组的哈希函数 - ipv4/6

java - 在 Java 和 PHP 中使用 PBKDF2

javascript - 为什么 AES 函数返回不同的值?

arrays - Perl - 将数组分配给另一个变量

python - 使用python从数据库验证wordpress用户登录

python - PBKDF2 中的盐 - Python