Pergunta

am trying to implement an application that uses an MD5 hash underneath, i have acquired the MD5 hash program from the internet, but i don't know how to pass the string from my program ( my program written in C) to the program that calculate the MD5 hash.

this is the main of the program that calculate the MD5 hash..

//
// MD5 Hashing Example - Using Windows Crypto API
//
// by Napalm @ NetCore2K
//
#include <stdio.h>
#include <string.h>
#include "winmd5.h"
HCRYPTPROV hCryptProv;

int main(int argc, char *argv[])
{
    int i;
    CryptStartup();
    if(argc > 1){
        MD5Context ctx;
        MD5Init(&ctx);
        MD5Update(&ctx, (unsigned char *)argv[1], strlen(argv[1]));
        MD5Final(&ctx);
        for(i = 0; i < 16; i++)
            printf("%02x", ctx.digest[i]);
        printf("\n");
    }else
        printf("Usage: %s <string>\n", argv[0]);
    CryptCleanup();
    return 0;
}

this is the header file content..

//
// MD5 Hashing Example - Using Windows Crypto API
//
// by Napalm @ NetCore2K
//
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern HCRYPTPROV hCryptProv;

void PrintMD5(const char *);

typedef struct {

unsigned char digest[16];
unsigned long hHash;
} MD5Context;
BOOL CryptStartup()
{


if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET) == 0){
    if(GetLastError() == NTE_EXISTS){
    if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) == 0){
            return FALSE;
        }   
    }
    else return FALSE;
}
return TRUE;
}
void CryptCleanup()
{
if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
hCryptProv = NULL;
}
void inline MD5Init(MD5Context *ctx)
{ 
CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
}
void inline MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned len)
{
CryptHashData(ctx->hHash, buf, len, 0);
}
void inline MD5Final(MD5Context *ctx)
{
DWORD dwCount = 16;
CryptGetHashParam(ctx->hHash, HP_HASHVAL, ctx->digest, &dwCount, 0);
if(ctx->hHash) CryptDestroyHash(ctx->hHash);
ctx->hHash = 0;
}
void PrintMD5(const char *s)
{
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, (const unsigned char *)s, strlen(s));
MD5Final(&ctx);

int i;
for (i = 0; i < 16; i++) {
    printf("%02x", ctx.hHash[i]);
}

printf("\n");
 }

how should i call this Main(), and how to pass the String to it and get the result.? thanks in advance.

Regards

Foi útil?

Solução

You don't call main() again. You rather use this program as an example to write a separate function, like this:

void PrintMD5(const char *s)
{
    MD5Context ctx;
    MD5Init(&ctx);
    MD5Update(&ctx, (const unsigned char *)s, strlen(s));
    MD5Final(&ctx);

    int i;
    for (i = 0; i < 16; i++) {
        printf("%02x", ctx.digest[i]);
    }

    printf("\n");
}

Then you can call this from your own program like:

PrintMD5("FooBarHelloWorld42");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top