MD5 Library

Using the AVR-Crypo-Lib's MD5 implementation, I have created a library.

I am not a C++ programmer, if there are any ways I can improve this let me know.

http://www.chetos.net/chetos/files/md5.zip

It will compute around 350 hashes per sec.

Hi Chet, Thanks for taking the time to put this together. I am trying to get the ComputeHash function to take a char variable as it's input for const void* msg. I get the code to compile, however the hash seems off, any ideas on the best way to do this?

I need to insert some additional variables into the text getting hashed, so I can't use the string input documented in the examples. Here's the code:

  char temp[8] = {'a','b','c','d','e','f','g','h'};
  md5Hasher.ComputeHash(&destination, temp);

  char str[32];
  MD5::ToHexString(destination, str);
  Serial.println(str);  // Output:EC5749ECA97A13C86CB95CB0A8F9755C

  md5Hasher.ComputeHash(&destination, "abcdefgh");
  MD5::ToHexString(destination, str);
  Serial.println(str);  // Output: E8DC4081B13434B45189A720B77B6818

Thanks!

If you look at the ComputeHash function in the md5.cpp, you will see that I am using strlen() to determine the length of the string. This function requires a null terminator.

You char array does not have a null terminator. So I am assuming that the strlen function is returning too large a value. Either increase the length of the array by one, and add \0 as the last value, or please suggest a way that I can determine the length of the string/char array without using strlen. As I said, I am not a C++ programmer.

Hi Chet, Thanks for your help! Adding \0 to the end of the char array works great.

Thanks again

I’m trying to port this Python script to Arduino using your MD5 hash…

import StringIO, md5  
  
pair = "4EA92B4292701F31"  
passcode = "8222"  
expected = "BEFF520F8280591AC0BBCB83B468FAA5"  
  
merged = StringIO.StringIO()  
merged.write(pair)  
for c in passcode:  
    merged.write(c)  
    merged.write("\x00")  
  
found = md5.new(merged.getvalue()).hexdigest()  
  
print "expected =", expected  
print "found    =", found.upper()

Here is what I have:

#include <md5.h>
#include <WString.h>
#include <string.h>

String pair = "4EA92B4292701F31";  
String passcode = "8222";  
String expected = "BEFF520F8280591AC0BBCB83B468FAA5";

MD5 md5Hasher;

void setup() {
  Serial.begin(115200);
  
   md5_hash_t destination; // Create an array to hold the hash; md5_hash_t is defined as uint8_t[16] in the header.
  String tmp;
  
  for (int i=0; i<pair.length(); i++){
      tmp.append(pair.charAt(i));
  }
  
   for (int i=0; i<passcode.length(); i++){
      tmp.append(passcode.charAt(i));
      tmp.append(0x00);
   }
   
   tmp.append(0);
  
 md5Hasher.ComputeHash(&destination, tmp);
  
 Serial.print("expected:");
 Serial.print(expected);
 Serial.print(" > Found: ");
 char str[32];
 MD5::ToHexString(destination, str);
 Serial.println(str);  

 delay(1000);
  
}


void loop() {      
 
}

This returns:

expected:BEFF520F8280591AC0BBCB83B468FAA5
Found: BEFE241905DB60DA8AB4EF0FF47C700B

Close but not right… any ideas?

This is just a guess, I don't have my Arduino with me to test. The MD5 library makes a call to strlen to get the number of characters in your input string. Strlen stops counting when it hits a null-terminator \0. Your string generation code addes a \0 after each character in the passcode.

Also, you are passing a String object to ComputeHash, but it expects a char array. I am not a C programmer, I am a .NET programmer, so I don't know if this is significant, but I suspect it is. See what tmp.c_str() returns.

@Mavromatis, you can look at this post http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263301069. I have took MD5 source from the RFC of MD5 and ported it to the Arduino. It sounds like it might be similar to Chet's, however mine does alot more computations per second(lol should anyone need more than in a single second). I also put in my sample code in a line to remove the "\n" from the char array before I pass it to the MD5 method for hasing(when user's press ENTER to pass in the string, the "\n" newline char gets added to the char array). This may be the cause of your issues, as indicated earlier in this post. Good luck.