NO

Author Topic: MD5 SHA1 advapi.dll  (Read 8488 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1902
MD5 SHA1 advapi.dll
« on: August 10, 2010, 07:34:13 pm »
advapi32.dll seems to have these functions.
Code: [Select]
LIBRARY advapi32.dll

EXPORTS
_A_SHAFinal@8
_A_SHAInit@4
_A_SHAUpdate@12
_MD4Final@4
_MD4Init@4
_MD4Update@12
_MD5Final@4
_MD5Init@4
_MD5Update@12

here is two examples:

 
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// http://msdn.microsoft.com/en-us/library/ff729221(VS.85).aspx

typedef struct {
  ULONG         i[2];
  ULONG         buf[4];
  unsigned char in[64];
  unsigned char digest[16];
} MD5_CTX;

void WINAPI MD5Init(MD5_CTX*);
void WINAPI MD5Update(MD5_CTX*, unsigned char* input, unsigned int inlen);
void WINAPI MD5Final(MD5_CTX*);

// polib /def:advapicrypt.def /out:advapicrypt.lib

#ifdef DYNDLL
// // The MD5Init function initializes an MD5 message digest context.
typedef void (WINAPI *MD5INIT)(MD5_CTX*);

// // The MD5Update function updates the MD5 context by using the supplied buffer for the message whose MD5 digest is being generated
typedef void (WINAPI *MD5UPDATE)(MD5_CTX*, unsigned char* input, unsigned int inlen);

// // The MD5Final function ends an MD5 message digest previously started by a call to the MD5Init function
typedef void (WINAPI *MD5FINAL)(MD5_CTX*);
#else
#pragma lib "advapicrypt.lib"
#endif

// ==============================
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// // Handle to the dll file
 //HINSTANCE hLib;

// ===============================
// Declare variable of type def
//
#ifdef DYNDLL
MD5INIT MD5Init;
MD5UPDATE MD5Update;
MD5FINAL MD5Final;
#endif
MD5_CTX md5Ctx;
char szHex[] ="0123456789ABCDEF";
char szTest[] = "test";
char szMD5[40] = "MD5_";
//char szTmp[100];

// ==============================
#ifdef DYNDLL
HANDLE hLib = LoadLibrary("cryptdll.Dll");
//HANDLE hLib = LoadLibrary("advapi32.Dll");

// If the handle is valid try to get function address
if (hLib != NULL) {
MD5Init = (MD5INIT)GetProcAddress(hLib, ("MD5Init"));
MD5Update = (MD5UPDATE)GetProcAddress(hLib, ("MD5Update"));
MD5Final = (MD5FINAL)GetProcAddress(hLib, ("MD5Final"));

// If the function address is valid try call function
if (MD5Init != NULL) {
#endif
MD5Init(&md5Ctx);
MD5Update(&md5Ctx, (BYTE*)szTest, sizeof(szTest));
MD5Final(&md5Ctx);
int nOfs;
for (int nC = 0; nC < 16; nC++) {
nOfs = nC*2+4;
szMD5[nOfs] = szHex[(md5Ctx.digest[nC]&0xF0)>>4];
szMD5[nOfs+1] = szHex[md5Ctx.digest[nC]&0x0F];
}
szMD5[nOfs+2] = 0;
OutputDebugString(szMD5);
MessageBox(0, szMD5, "MD5_Test", MB_OK);
#ifdef DYNDLL
}
FreeLibrary(hLib);
}
#endif
return 0;
}
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

typedef struct {
ULONG Unknown[6];
ULONG State[5];
ULONG Count[2];
UCHAR Buffer[64];
} SHA_CTX, *PSHA_CTX;

typedef struct {
BYTE Dig[20];
} SHA_DIG, *PSHA_DIG;

void WINAPI *A_SHAInit(SHA_CTX*);
void WINAPI *A_SHAUpdate(SHA_CTX*, unsigned char* input, unsigned int inlen);
void WINAPI *A_SHAFinal(SHA_CTX*, SHA_DIG*);

// polib /def:advapicrypt.def /out:advapicrypt.lib

#ifdef DYNDLL
// // The A_SHAInit function initializes an SHA message digest context.
typedef void (WINAPI *A_SHAINIT)(SHA_CTX*);

// // The A_SHAUpdate function updates the SHA context by using the supplied buffer for the message whose SHA digest is being generated
typedef void (WINAPI *A_SHAUPDATE)(SHA_CTX*, unsigned char* input, unsigned int inlen);

// // The A_SHAFinal function ends an SHA message digest previously started by a call to the A_SHAInit function
typedef void (WINAPI *A_SHAFINAL)(SHA_CTX*, SHA_DIG*);
#else
#pragma lib "advapicrypt.lib"
#endif
// ==============================
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// // Handle to the dll file
 //HINSTANCE hLib;
#ifdef DYNDLL
// ===============================
// Declare variable of type def
//
A_SHAINIT A_SHAInit;
A_SHAUPDATE A_SHAUpdate;
A_SHAFINAL A_SHAFinal;
#endif
SHA_CTX shaCtx;
SHA_DIG shaDig;
//BYTE shaDig[20];
char szHex[] ="0123456789ABCDEF";
char szTest[] = "abc";
char szSHA1[46] = "SHA1_";

/* FIPS PUB 180.1 example A.1 */
static char const text1[3] = "abc";
static unsigned char const hash1[20] = {
0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };

// ==============================
#ifdef DYNDLL
HANDLE hLib = LoadLibrary("advapi32.Dll");

// If the handle is valid try to get function address
if (hLib != NULL) {
A_SHAInit = (A_SHAINIT)GetProcAddress(hLib, ("A_SHAInit"));
A_SHAUpdate = (A_SHAUPDATE)GetProcAddress(hLib, ("A_SHAUpdate"));
A_SHAFinal = (A_SHAFINAL)GetProcAddress(hLib, ("A_SHAFinal"));

// If the function address is valid try call function
if (A_SHAInit != NULL) {
#endif
A_SHAInit(&shaCtx);
//SHAUpdate(&shaCtx, (BYTE*)szTest, sizeof(szTest));
A_SHAUpdate(&shaCtx, (BYTE*)text1, sizeof(text1));
A_SHAFinal(&shaCtx, &shaDig);
int nOfs;
for (int nC = 0; nC < 20; nC++) {
nOfs = nC*2+5;
szSHA1[nOfs] = szHex[(shaDig.Dig[nC]&0xF0)>>4];
szSHA1[nOfs+1] = szHex[shaDig.Dig[nC]&0x0F];
}
szSHA1[nOfs+2] = 0;
for (int nC = 0; nC < 20; nC++) {
if (shaDig.Dig[nC] != hash1[nC]) {
MessageBox(0, "Error!", "SHA1_Test", MB_OK);
break;
}
}
//OutputDebugString(szSHA1);
MessageBox(0, szSHA1, "SHA1_Test", MB_OK);
#ifdef DYNDLL
}
FreeLibrary(hLib);
}
#endif
return 0;
}

May the source be with you