MD5 library setup and config examples

If you are asking a question, please follow this template:

  1. My goal is: [describe your goal]
    Hash a string using MD5

  2. My actions are: [describe your actions - code, commands, etc]
    looking for documentation

  3. The result I see is: [show the result - log, etc]

  4. My expectation & question is: [describe your expectation and your question]
    Is there a library for MD5 and any special setup to include it in the build process?

#include "mbedtls/md5.h"

bool make_uuid_md5(const char* name, char* uuid, size_t uuid_len) {
  mbedtls_md5_context ctx;
  mbedtls_md5_init(&ctx);
  mbedtls_md5_starts(&ctx);
  mbedtls_md5_update(&ctx, (const uint8_t*) name, strlen(name));

  uint8_t hash[16];
  mbedtls_md5_finish(&ctx, hash);

  uuid_from_hash(hash, uuid, uuid_len);
  LOG(LL_INFO, ("uuid_md5: %s", uuid));

  mbedtls_md5_free(&ctx);
  return true;
}
1 Like

Just for posterity, ie other people wondering, this is how you generate a md5 hex hash:


static bool make_uuid_md5(const char* name, char* uuid, size_t uuid_len) {
    /* how to use:
    char const* payload = "Hello SHA 256 from ESP32learning";
    int size = 16; // MD5 is 16 bytes
    char hashResult[(size * 2) + 1]; 
    make_uuid_md5((const char*)payload, hashResult, (size_t)size);
    LOG(LL_DEBUG, ("uuid result in 10: %s", hashResult));
    */

    mbedtls_md5_context ctx;
    mbedtls_md5_init(&ctx);
    mbedtls_md5_starts(&ctx);
    mbedtls_md5_update(&ctx, (const uint8_t*)name, strlen(name));

    uint8_t size = 16;
    uint8_t hash[size];
    mbedtls_md5_finish(&ctx, hash);

    LOG(LL_INFO, ("uuid_md5 : %s", hash));

    /* Allocate twice the number of the bytes in the buf array because each byte would be 
    * converted to two hex characters, also add an extra space for the terminating null byte
    * [size] is the size of the buf array, ie:
    * char output[(size * 2) + 1];
    * */

    /* pointer to the first item (0 index) of the output array */
    char* ptr = &uuid[0];

    int i;

    for (i = 0; i < size; i++) {
        /* sprintf converts each byte to 2 chars hex string and a null byte, for example
        * 10 => "0A\0".
        *
        * These three chars would be added to the output array starting from
        * the ptr location, for example if ptr is pointing at 0 index then the hex chars
        * "0A\0" would be written as output[0] = '0', output[1] = 'A' and output[2] = '\0'.
        *
        * sprintf returns the number of chars written execluding the null byte, in our case
        * this would be 2. Then we move the ptr location two steps ahead so that the next
        * hex char would be written just after this one and overriding this one's null byte.
        *
        * We don't need to add a terminating null byte because it's already added from 
        * the last hex string. */
        ptr += sprintf(ptr, "%02X", hash[i]);
    }

    //printf("uuid final: %s\n", output);

    mbedtls_md5_free(&ctx);
    return true;
}


    char const* payload = "Hello world this is hush hush";
    int size = 16;
    char hashResult[(size * 2) + 1];

    make_uuid_md5((const char*)payload, hashResult, (size_t)size);

    LOG(LL_DEBUG, ("hash in hex result: %s", hashResult));