calebstewart / md5

MD5 Hashing Algorithm for Embedded Platforms
4 stars 1 forks source link

Implementation issue #1

Open julianblanco opened 3 years ago

julianblanco commented 3 years ago

Do you see any issue with the following implementation?

Compiler: platformio teensy36 Observed action, return empty char array

char* getMD5HashOfFile(FsFile filename)
{
int bytes_left = filename.size();
Serial.println(bytes_left);
MD5 md5;
char digest[33];
unsigned char hash[16];
unsigned char chunk[CHUNK_SZ];
unsigned int nread = 0;
while( bytes_left ){
    nread = filename.read(chunk, CHUNK_SZ);
     md5.update( chunk, nread);
     bytes_left -= nread;
    Serial.println(bytes_left);
  }
// Retrieve the final hash
  md5.finalize(hash);

  // If you'd like to reuse the MD5 object to calculate new hashes,
  // you must call reset to ensure you don't continue calculating from
  // the previous data.
  // md5.reset();

  // Output a hex digest
  MD5::digest(hash, digest);
  return digest;
}
calebstewart commented 3 years ago

You are returning a pointer to a local variable, which is no longer valid after you return from that function. It's undefined behavior, technically.