我有以下功能:

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++ - C++ PBKDF2问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45062886/

10-11 22:49