11.19.06

MD5 HexDigest for Symbian

Posted in Development, Mobile, Symbian, Uncategorized, c++ at 12:54 am by Twm

SymbianOS provides a CMD5 class for hashing 8-bit strings, but neglects to supply a hexdigest function to supply the Hash as a hex string, as required by many web services such as Flickr.

Calling CMD5::Hash() returns an 8bit binary descriptor of the hash. This snippet of code provides the HEX representation in ASCII in line with what HexDigest() returns in java and python etc.

LOCAL_C HBufC8* Md5HexDigestLC(const TDes8& aString)
  {
   CMD5* md5 = CMD5::NewL();
   CleanupStack::PushL(md5);

  TPtrC8 hashedSig(md5->Hash(aString));

  HBufC8* buf = HBufC8::NewL(hashedSig.Length() * 2);
  TPtr8 bufPtr = buf->Des();

  for(TInt i=0; i< hashedSig.Length(); i++)
    {
    bufPtr.AppendFormat(_L8("%+02x"),hashedSig[i]);
    }
  CleanupStack::PopAndDestroy(md5);
  CleanupStack::PushL(buf);
  return buf;
  }