+- +-

+-User

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

+-Stats

Members
Total Members: 130
Latest: REEG
New This Month: 1
New This Week: 0
New Today: 0
Stats
Total Posts: 319
Total Topics: 160
Most Online Today: 2
Most Online Ever: 159
(June 29, 2021, 10:20:55 pm)
Users Online
Members: 0
Guests: 1
Total: 1

Author Topic: Big-endian +hex translation SHA1 hash winapi  (Read 152 times)

rollgillie

  • Newbie
  • *
  • Posts: 1
    • View Profile
Big-endian +hex translation SHA1 hash winapi
« on: July 12, 2016, 12:44:42 am »
Hi guys, I read this post:
http://www.rohitab.com/discuss/topic/33162-c-win32-sha-1-password-hashing-example/

Code: [Select]
#include <windows.h>
#include <wincrypt.h>
 
#define SHA1_HASH_LEN 20
 
BOOL SHA1PasswordHash(LPCTSTR lpszPassword, LPTSTR lpszHash)
{
    HCRYPTPROV hCryptProv; // Handle to our context
    HCRYPTHASH hCryptHash; // Handle to our hash
    BYTE       bHashValue[SHA1_HASH_LEN]; // This will hold our SHA-1 hash
    DWORD     dwSize   = SHA1_HASH_LEN, dwCount; // Size of output, and a count variable for a for loop
    BOOL       bSuccess = FALSE; // We change this to TRUE if we complete the operations
    // Declare all the variables at the start of our code for C89 compatability
 
    if(CryptAcquireContext(&hCryptProv, NULL, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    { // Initiate usage of the functions
        if(CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hCryptHash))
        { // Create a SHA1 hash
            if(CryptHashData(hCryptHash, (PBYTE)lpszPassword, lstrlen(lpszPassword) * sizeof(TCHAR), 0))
            { // Update the hash, (process our password)
                if(CryptGetHashParam(hCryptHash, HP_HASHVAL, bHashValue, &dwSize, 0))
                { // Extract the hash
                    for(dwCount = 0, *lpszHash = 0; dwCount < SHA1_HASH_LEN; dwCount++)
                    { // Format the hash into a big endian, hexadecimal string
                        wsprintf(
                            lpszHash + (dwCount * 2),
                            TEXT("%02x"),
                            bHashValue[dwCount]
                        );
                    }
                    bSuccess = TRUE;
                }
            }
            CryptDestroyHash(hCryptHash);
        }
        CryptReleaseContext(hCryptProv, 0);
    }
 
    return bSuccess;
}
 
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    LPCTSTR lpszPassword = TEXT("hello");
    LPTSTR  lpszHash     = LocalAlloc(LPTR, ((SHA1_HASH_LEN * 2) + 1) * sizeof(TCHAR));
 
    if(!lpszHash) {
        MessageBox(NULL, TEXT("Couldn't allocate memory"), TEXT("Error!"), MB_OK | MB_ICONERROR);
        return 0;
    }
 
    if(SHA1PasswordHash(lpszPassword, lpszHash)) {
        MessageBox(NULL, lpszHash, TEXT("Success!"), MB_OK);
    }else {
        MessageBox(NULL, TEXT("SHA1PasswordHash() Failed"), TEXT("Error!"), MB_OK | MB_ICONERROR);
    }
 
    LocalFree(lpszHash);
    return 0;
}

However, I don't understand how and why translate to big-endian and hexdecimal?
What is the original form after CryptHashData() done?

P/s: I tried to use CALG_SHA_256 instead of CALG_SHA1, but it didn't work. Any ideas?
P/s2: I used 0x0000800c as CALG_SHA_256, and put SHA1_HASH_LEN to 32. But it seems that's not right answer.
« Last Edit: July 12, 2016, 12:55:42 am by rollgillie »

Share on Facebook Share on Twitter


 

+-Recent Topics

Independent Call Girls in Chandigarh by dilpreetkaur
June 21, 2021, 01:02:52 pm

Hi zwclose7. How to create process by using NT apis? by zwclose7
June 01, 2021, 03:09:52 pm

Poison of the Day by zwclose7
March 16, 2020, 06:45:08 pm

IRC by AzeS
February 17, 2020, 08:18:01 am

Native API tutorial by hMihaiDavid
January 08, 2019, 02:11:02 am

The properties of GP nerve agent by xchg
October 19, 2018, 07:40:57 pm

A new route of synthesis for G-series agents by Basquyatti
October 15, 2018, 06:12:57 am

Synthesis of Methylisobutylcarbinylsarin (GH) by APC process by Basquyatti
October 14, 2018, 07:55:33 am

Synthesis conventional of Sarin by Basquyatti
October 02, 2018, 07:57:32 am

Reaction CX-7 (Experimental) by zwclose7
October 02, 2018, 12:46:47 am