How to Get the Md5 Hash of a File in C++

How to calculate the MD5 hash of a large file in C?

example

gcc -g -Wall -o file file.c -lssl -lcrypto

#include <stdio.h>
#include <openssl/md5.h>

int main()
{
unsigned char c[MD5_DIGEST_LENGTH];
char *filename="file.c";
int i;
FILE *inFile = fopen (filename, "rb");
MD5_CTX mdContext;
int bytes;
unsigned char data[1024];

if (inFile == NULL) {
printf ("%s can't be opened.\n", filename);
return 0;
}

MD5_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, inFile)) != 0)
MD5_Update (&mdContext, data, bytes);
MD5_Final (c,&mdContext);
for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf (" %s\n", filename);
fclose (inFile);
return 0;
}

result:

$ md5sum file.c
25a904b0e512ee546b3f47574703d9fc file.c
$ ./file
25a904b0e512ee546b3f47574703d9fc file.c

How to hash the contents of a file in C?

Use OpenSSL C APIs

#include <openssl/md5.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {
unsigned char sha256_digest[SHA256_DIGEST_LENGTH];
unsigned char md5_digest[MD5_DIGEST_LENGTH];
unsigned char *buffer = "Hello World!";
int i;

SHA256(buffer, strlen(buffer), sha256_digest);
MD5(buffer, strlen(buffer), md5_digest);


for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
printf("%02x", sha256_digest[i]);
}
printf("\n");
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md5_digest[i]);
}

}

To compile this code you need to link it properly using the crypto library

 gcc testmd5.c -lcrypto

Once you execute, you will get this output

 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
ed076287532e86365e841e92bfc50d8c

MD5 hashing in a string in C

Probably one of the biggest inconveniences of C is string handling. It's all very low level. char* is not simply a string that can be modified and written to however; it's just a pointer to memory.

There are several ways to correct the code, but here is my recommended fix:

// hashes two words and writes the digest to output.
void hashfunc(char* word1, char* word2, char* output) {
//concat both words
char concat[100];
strcpy(concat, word1);
strcat(concat, word2);

MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5,concat,strlen(concat));
MD5_Final(output,&md5);
}

Changes were:

  • Using a temporary work buffer to join the words together to be hashed.
  • Not returning a value, instead opting to let the user pass a buffer in to handle the return value.

Benefits of the second change is that the consumer can use memory on the stack rather than always being forced to deal with memory allocations inside of the function. (The other method would be to malloc memory and return that, which is slower and must be freed manually).

Also I'm not exactly sure how the MD5 library you're using works, but you can probably avoid the manual concatenation altogether:

void hashfunc(char* word1, char* word2, char* output) {
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5,word1,strlen(word1));
MD5_Update(&md5,word2,strlen(word2));
MD5_Final(output,&md5);
}

And in case you need a usage example:

char ret[100];
hashfunc(a, b, ret);

How to find the MD5 hash of a file?

We can use the Windows CertUtil tool to find the MD5 hash sum and Parse the output by StrSpliting it into different lines (The MD5 sum itself will be on the second line).

cmd command used:

CertUtil -hashfile %appdata%/appsettings/app.ini MD5

(Note: replace %appdata%/appsettings/app.ini with your actual file path)


Final Code:

var:= ComObjCreate("WScript.Shell").Exec("cmd.exe /q /c CertUtil -hashfile %appdata%/appsettings/app.ini MD5").StdOut.ReadAll()
MsgBox %var%
outputArr := (StrSplit(var , "`r`n"))
out:=outputArr[2]
MsgBox %out%

It is tested for Windows 10, although it should work for some older versions as well.

How to create a md5 hash of a string in C?

I don't know this particular library, but I've used very similar calls. So this is my best guess:

unsigned char digest[16];
const char* string = "Hello World";
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, string, strlen(string));
MD5Final(digest, &context);

This will give you back an integer representation of the hash. You can then turn this into a hex representation if you want to pass it around as a string.

char md5string[33];
for(int i = 0; i < 16; ++i)
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);


Related Topics



Leave a reply



Submit