CANopen Slave Documentation
Version 7.00.02
Loading...
Searching...
No Matches
mc_md5.h File Reference

Detailed Description

This simple MD5 implementation is derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm and modified slightly to be functionally identical but condensed into control structures.

All functions provided by this module require a structure McMD5Context_s to store an intermediate hash value as well as the MD5 result (digest). The structure has to be initialized by McMD5Init(). The MD5 checksum is computed by calling McMD5Add() and McMD5Finalize().

#include "md5.h"
..
int32_t computeMd5()
{
char szBufferT[40];
char szInputT[] = "Hello";
int32_t slResultT;
McMD5Context_ts tsMD5ContextT;
//---------------------------------------------------------------------------------------------------
// MD5 checksum on "Hello, World!", input is added in 3 parts
//
McMD5Init(&tsMD5ContextT);
McMD5Add(&tsMD5ContextT, (uint8_t *) szInputT, strlen(szInputT));
strcpy(szInputT, ", W");
McMD5Add(&tsMD5ContextT, (uint8_t *) szInputT, strlen(szInputT));
strcpy(szInputT, "orld!");
McMD5Add(&tsMD5ContextT, (uint8_t *) szInputT, strlen(szInputT));
McMD5Finalize(&tsMD5ContextT);
//---------------------------------------------------------------------------------------------------
// MD5 checksum of "Hello, World!" input: 65a8e27d8879283831b664bd8b7f0ad4
//
McMD5String(&tsMD5ContextT, &szBufferT[0], false);
printf("Checksum %s \n", szBufferT);
slResultT = strcmp(&szBufferT[0], "65a8e27d8879283831b664bd8b7f0ad4");
return (slResultT);
}
void McMD5Finalize(McMD5Context_ts *ptsMd5ContextV)
void McMD5String(McMD5Context_ts *ptsMd5ContextV, char *pszBufferV, bool_t btUpperCaseV)
void McMD5Add(McMD5Context_ts *ptsMd5ContextV, uint8_t *pubInputV, uint32_t ulInputSizeV)
void McMD5Init(McMD5Context_ts *ptsMd5ContextV)
Definition mc_md5.h:111

The output of this code example is:

Checksum 65a8e27d8879283831b664bd8b7f0ad4
+ Include dependency graph for mc_md5.h:

Data Structures

struct  McMD5Context_s
 

Functions

void McMD5Add (McMD5Context_ts *ptsMd5ContextV, uint8_t *pubInputV, uint32_t ulInputSizeV)
 
void McMD5Finalize (McMD5Context_ts *ptsMd5ContextV)
 
void McMD5Init (McMD5Context_ts *ptsMd5ContextV)
 
void McMD5String (McMD5Context_ts *ptsMd5ContextV, char *pszBufferV, bool_t btUpperCaseV)
 

Function Documentation

◆ McMD5Add()

void McMD5Add ( McMD5Context_ts * ptsMd5ContextV,
uint8_t * pubInputV,
uint32_t ulInputSizeV )
Parameters
[in]ptsMd5ContextVPointer to MD5 context structure
[in]pubInputVPointer to input data
[in]ulInputSizeVSize of input data in bytes
See also
McMD5Finalize()

The function adds input data to the context MD5 context structure defined by ptsMd5ContextV. The size of the input data in bytes is defined by parameter ulInputSizeV.

If the input fills out a block of 512 bits, the internal MD5 algorithm is applied and the result in stored in the buffer of the MD5 context structure. The function also updates the overall size inside the context structure defined by ptsMd5ContextV.

◆ McMD5Finalize()

void McMD5Finalize ( McMD5Context_ts * ptsMd5ContextV)
Parameters
[in]ptsMd5ContextVPointer to MD5 context structure
See also
McMD5Add()

The function pads the current input which has been applied by McMD5Add() to get to 448 bytes. The result of the final iteration is saved into the digest of the MD5 context structure (McMD5Context_ts::aubDigest). The binary MD5 checksum can be retrieved by reading the digest array. As alternative it is possible to convert the MD5 checksum into a character string by calling McMD5String().

◆ McMD5Init()

void McMD5Init ( McMD5Context_ts * ptsMd5ContextV)
Parameters
[in]ptsMd5ContextVPointer to MD5 context structure

The function initializes the MD5 context structure defined by ptsMd5ContextV for further operation.

◆ McMD5String()

void McMD5String ( McMD5Context_ts * ptsMd5ContextV,
char * pszBufferV,
bool_t btUpperCaseV )
Parameters
[in]ptsMd5ContextVPointer to MD5 context structure
[in]pszBufferVPointer to string buffer
[in]btUpperCaseVFlag for upper-case character output

The function prints the digest (MD5 checksum) of the MD5 context structure defined by ptsMd5ContextV to the character buffer pszBufferV. The size of the buffer must be at least 33 bytes. By parameter btUpperCaseV the output string can be controlled between lower-case and upper-case character.

#include "md5.h"
..
int32_t printChecksum()
{
char szBufferT[40];
McMD5Context_ts tsMD5ContextT;
//---------------------------------------------------------------------------------------------------
// MD5 checksum of empty input
//
McMD5Init(&tsMD5ContextT);
McMD5Add(&tsMD5ContextT, CPP_NULL, 0);
McMD5Finalize(&tsMD5ContextT);
//---------------------------------------------------------------------------------------------------
// MD5 checksum of empty input:
//
McMD5String(&tsMD5ContextT, &szBufferT[0], false);
printf("Checksum lower-case: %s \n", szBufferT);
McMD5String(&tsMD5ContextT, &szBufferT[0], true);
printf("Checksum upper-case: %s \n", szBufferT);
}

The output of this code example is:

Checksum lower-case: d41d8cd98f00b204e9800998ecf8427e
Checksum upper-case: D41D8CD98F00B204E9800998ECF8427E