Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used SHA256 algorithm for getting hash.
THe hash produced by my application is not same as the one generated by online tools for the same string.

What I have tried:

        BCRYPT_ALG_HANDLE       hAlg            = NULL;
        BCRYPT_HASH_HANDLE      hHash           = NULL;
        NTSTATUS                status          = STATUS_UNSUCCESSFUL;
        DWORD                   cbData          = 0,
            cbHash          = 0,
            cbHashObject    = 0;
        PBYTE                   pbHashObject    = NULL;
        PBYTE                   pbHash          = NULL;


        //open an algorithm handle
        if(!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(
            &hAlg,
            BCRYPT_SHA256_ALGORITHM,
            NULL,
            0)))
        {
            goto Cleanup;
        }

        //calculate the size of the buffer to hold the hash object
        if(!NT_SUCCESS(status = BCryptGetProperty(
            hAlg, 
            BCRYPT_OBJECT_LENGTH, 
            (PBYTE)&cbHashObject, 
            sizeof(DWORD), 
            &cbData, 
            0)))
        {

            goto Cleanup;
        }

        //allocate the hash object on the heap
        pbHashObject = (PBYTE)HeapAlloc (GetProcessHeap (), 0, cbHashObject);
        if(NULL == pbHashObject)
        {

            goto Cleanup;
        }

        //calculate the length of the hash
        if(!NT_SUCCESS(status = BCryptGetProperty(
            hAlg, 
            BCRYPT_HASH_LENGTH, 
            (PBYTE)&cbHash, 
            sizeof(DWORD), 
            &cbData, 
            0)))
        {

            goto Cleanup;
        }

        //allocate the hash buffer on the heap
        pbHash = (PBYTE)HeapAlloc (GetProcessHeap (), 0, cbHash);
        if(NULL == pbHash)
        {

            goto Cleanup;
        }

        //create a hash
        if(!NT_SUCCESS(status = BCryptCreateHash(
            hAlg, 
            &hHash, 
            pbHashObject, 
            cbHashObject, 
            NULL, 
            0, 
            0)))
        {

            goto Cleanup;
        }


        PCWSTR pwTst = (PCWSTR)csText_i;
        //hash some data
        if(!NT_SUCCESS(status = BCryptHashData(
            hHash,
            (PBYTE)pwTst,
            sizeof(rgbMsg),
            0)))
        {

            goto Cleanup;
        }

        //close the hash
        if(!NT_SUCCESS(status = BCryptFinishHash(
            hHash, 
            pbHash, 
            cbHash, 
            0)))
        {
            goto Cleanup;
        }

PCWSTR pTst = (PCWSTR)pbHash;
csEnryptedText_o = pTst;




Is it because of the way i cast??
Posted
Updated 21-Mar-18 1:08am
v2

1 solution

The cbInput parameter of BCryptHashData is wrong:
C++
if(!NT_SUCCESS(status = BCryptHashData(
    hHash,
    (PBYTE)pwTst,
    sizeof(rgbMsg),
    0)))
    {
        goto Cleanup;
    }
cbInput [in]

The number of bytes in the pbInput buffer.
It should be probably
C++
if(!NT_SUCCESS(status = BCryptHashData(
    hHash,
    (PBYTE)pwTst,
    wcslen(pwTest) * sizeof(wchar_t),
    0)))
{
        goto Cleanup;
}

This requires that csText_i is a null terminated wide string. The line
C++
PCWSTR pwTst = (PCWSTR)csText_i;
let me assume that this is not the case because otherwise there should be no need to cast to PCWSTR.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900