web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

MD5 calculatehash for special characters éàè

(0) ShareShare
ReportReport
Posted on by

I found some code on this page : http://krishhdax.blogspot.co.il/2014/07/useful-functions-to-use-in-ax2012.html

to calculate a MD5 cash in X++

I works fine, but if I have a string with special characters it gives a different hash code then for example md5 via php gives me, I'm using this to check a string coming from a webservice call.

Does anyone have experience with this ?

This is the code I use :

public static str CalculateHash(str tb)
{

// code taken from : krishhdax.blogspot.co.il/.../useful-functions-to-use-in-ax2012.html
str s;
ClrObject obj;
ClrObject md5;
System.Text.StringBuilder sBuilder;
ClrObject clrStr;
ClrObject clrStrObject;
System.Exception clrException;
System.Array resultByteArray;
int i;
int arrayLength ;
InteropPermission perm;

perm = new InteropPermission(InteropKind::ClrInterop);
perm.assert();
try
{
obj = System.Text.Encoding::get_ASCII().GetBytes(tb);
md5 = System.Security.Cryptography.MD5::Create();
resultByteArray = md5.ComputeHash(obj);
//BP deviation documented
sBuilder = new System.Text.StringBuilder();
arrayLength = resultByteArray.get_Length() ;
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (i = 0; i <arrayLength; i++)
{
clrStrObject = resultByteArray.GetValue(i);
clrStr = clrStrObject.ToString('x2');
sBuilder.Append(clrStr);
}

// Return the hexadecimal string.
s = sBuilder.ToString();
}
catch (Exception::CLRError)
{
//BP deviation documented
clrException = CLRInterop::getLastException();
s = clrException.get_Message();
error(s);
throw error("@SYS106158");
}

CodeAccessPermission::revertAssert();
return s;

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    It's because you're using ASCII encoding (System.Text.Encoding::get_ASCII()), which doesn't support such characters.

    Use a different encoding, such as UTF-8.

  • Community Member Profile Picture
    on at

    I thought that would be the problem, I replaced these lines in my code :

    //        obj = System.Text.Encoding::get_ASCII().GetBytes(tb);

           obj = System.Text.Encoding::get_UTF8().GetBytes(tb);

    If I encode the string in PHP and get the MD5 key they are the same.

    How do I decode the string back from UTF8 in X++ ?

  • Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    What do you mean by that? MD5 creates a hash, the whole point is that it can't be decoded. Also, the algorithm encodes texts of any length to a fixed-size hash, therefore there is infinite number of plain texts leading to the same hash.

  • Brandon Wiese Profile Picture
    17,788 on at

    Strings in X++ are Unicode.  Why not just use Unicode as is?

  • Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    Unicode is a standard describing a character set; it's not an encoding. UTF-8 is one way of encoding Unicode character set.

    If you meant UnicodeEncoding class, it has a misleading name. It actually represents UTF-16.

  • Community Member Profile Picture
    on at

    I thought I would have to send the string, I send along with the MD5 code to check if it comes from a trusted origin, in the encoded format, but that is not needed, my code works now, thanks for the tip about the UTF8 !

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans