-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5.c
52 lines (45 loc) · 1.05 KB
/
md5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @file md5_handler.c
* @author Lucas Vieira (lucas.engen.cc@gmail.com)
* @brief Message digest related functions
* @version 0.1
* @date 2020-03-14
*
* @copyright Copyright (c) 2020
*
*/
#include "md5.h"
#include <openssl/md5.h>
#include <stdbool.h>
#include <stdlib.h>
#include <memory.h>
static MD5_CTX context;
static bool initialized = false;
unsigned char digest[MD5_DIGEST_LENGTH + 1];
static inline void md5_begin()
{
// Initialize only when necessary
if(!initialized && MD5_Init(&context))
{
initialized = true;
memset(digest, 0, sizeof(digest));
}
}
static inline void md5_cleanup()
{
// Copy digest to buffer and cleanup resources
MD5_Final(digest, &context);
initialized = false;
}
static inline void md5_calculate(unsigned char *data, unsigned int len)
{
md5_begin();
MD5_Update(&context, data, len);
md5_cleanup();
}
unsigned char *md5_digest(unsigned char *password, unsigned int len)
{
md5_calculate(password, len);
digest[MD5_DIGEST_LENGTH] = '\0';
return &digest[0];
}