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

Announcements

News and Announcements icon
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
    239,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
    239,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,790 on at

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

  • Martin Dráb Profile Picture
    239,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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 10

#2
Harisgillani Profile Picture

Harisgillani 4

#2
dserp Profile Picture

dserp 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans