This is a simple program which generates MD5 digest of a file and compares with the predefined MD5 digest in a program and checks for its integrity.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include <openssl/md5.h>
char * calculate_file_md5(const char *filename)
{
unsigned char c[MD5_DIGEST_LENGTH];
int i;
MD5_CTX mdContext;
int bytes;
unsigned char data[1024];
char *filemd5 = (char*) malloc(33 *sizeof(char));
FILE *inFile = fopen (filename, "rb");
if (inFile == NULL) {
perror(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 ) {
sprintf(&filemd5[i*2], "x", (unsigned int)c[i]);
}
printf("calculated md5:%s ", filemd5);
printf (" %s\n", filename);
fclose (inFile);
return filemd5;
}
int main(int argc, char **argv) {
char * predefined_md5 = "8e2745d333daa9666a8bbebcd32a39bb";
char *new_md5 = calculate_file_md5("backup.json");
if (!strcmp(predefined_md5, new_md5)) {
printf("md5 matched\n");
} else {
printf("md5 not matched\n");
}
free(new_md5);
return 0;
}
Program compiled and run gracefully but can any one tell me to sort the following warnings
GenerateMD5Hash.c: In function ‘calculate_file_md5’:
GenerateMD5Hash.c:18:9: warning: ‘MD5_Init’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations]
18 | MD5_Init (&mdContext);
| ^~~~~~~~
In file included from headers.h:5,
from GenerateMD5Hash.c:1:
/usr/include/openssl/md5.h:49:27: note: declared here
49 | OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c);
| ^~~~~~~~
GenerateMD5Hash.c:22:9: warning: ‘MD5_Update’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations]
22 | MD5_Update (&mdContext, data, bytes);
| ^~~~~~~~~~
In file included from headers.h:5,
from GenerateMD5Hash.c:1:
/usr/include/openssl/md5.h:50:27: note: declared here
50 | OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len);
| ^~~~~~~~~~
GenerateMD5Hash.c:24:9: warning: ‘MD5_Final’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations]
24 | MD5_Final (c,&mdContext);
| ^~~~~~~~~
In file included from headers.h:5,
from GenerateMD5Hash.c:1:
/usr/include/openssl/md5.h:51:27: note: declared here
51 | OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c);
using ubuntu mate OS command used to compile gcc GenerateMD5Hash.c -lcrypto -o GenerateMD5Hash
CodePudding user response:
From OpenSSL:
The following functions have been deprecated since OpenSSL 3.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data, unsigned long len);
int MD5_Final(unsigned char *md, MD5_CTX *c);
All of the functions described on this page are deprecated. Applications should instead use
EVP_DigestInit_ex(3),
EVP_DigestUpdate(3) and
EVP_DigestFinal_ex(3).
They are there for backwards compatibility. If you wish to ignore the warnings, you could:
- Just ignore them.
- Redirect the output to
/dev/null
. - Use the functions they recommend instead.