how do you hash a string using md5
by Jules · in Torque 3D Professional · 02/02/2011 (8:22 am) · 40 replies
I see that Torque has md5.cpp/h in the source, how do I use this to hash a string in TorqueScript? Can't find anything in the docs.
#2
02/02/2011 (11:52 am)
Maybe people have not used MD5 in Torque 3D yet, but I guess I'll have to use MD5 with RSA in c++ if Torque 3D cannot achieve this. I can't figure out how 3 function calls MD5init, MD5Update and MD5Final are used to encrypt a string.
#3
Add this to the top of md5.cpp
Then at the very bottom of md5.cpp add this
Then recompile and it should work.
02/02/2011 (12:17 pm)
Here you go, try this.Add this to the top of md5.cpp
#include "console/engineAPI.h" #include "console/consoleTypes.h" static const char Base16Values[] = "0123456789ABCDEF";
Then at the very bottom of md5.cpp add this
DefineConsoleFunction( getStringMD5, const char *, (const char *What),, "")
{
MD5_CTX context;
U8 digest[16];
static U8 digestB16[128];
U32 len = dStrlen (What);
MD5Init (&context);
MD5Update (&context, (U8 *) What, len);
MD5Final (digest, &context);
for (U32 Index = 0; Index < 16; Index++)
{
U32 Byte1 = Index * 2;
U32 Byte2 = Byte1 + 1;
U8 Value1 = (digest[Index] & 0xF0) >> 4;
U8 Value2 = digest[Index] & 0x0F;
digestB16[Byte1] = Base16Values[Value1];
digestB16[Byte2] = Base16Values[Value2];
}
digestB16[32] = 0;
char *ret = Con::getReturnBuffer(32);
dStrcpy(ret, (char*)digestB16);
return ret;
}Then recompile and it should work.
#4
02/02/2011 (12:20 pm)
Thanks Alex, appreciate it.
#5
Edit: I'll give it a go, and find out :)
02/02/2011 (12:21 pm)
Would this also be compatible with MD5 in PHP do you know?Edit: I'll give it a go, and find out :)
#6
02/02/2011 (12:31 pm)
I believe so.
#7
result from above = CE0BF2C5D3AC794E65C267F7C73BDF6E
result from php md5 = 5d41402abc4b2a76b9719d911017c592
02/02/2011 (12:48 pm)
Results are different. Based on a string of "hello"result from above = CE0BF2C5D3AC794E65C267F7C73BDF6E
result from php md5 = 5d41402abc4b2a76b9719d911017c592
#8
02/02/2011 (12:49 pm)
I did try them case sensitive also, but again the results were not the same. Something to do with the hashing?
#9
02/02/2011 (1:04 pm)
Ya it looks like I'm going to have to port the other resource over to Torque 3D to get the correct hash. I'll do that real quick.
#10
02/02/2011 (1:07 pm)
Cheers Alex.
#11
02/02/2011 (1:11 pm)
Worth you also doing this as a resource (port) from original. I can then point people to your resource from my pack and give you and the original owner credit, as it requires encryption.
#12
The function above is not at all MD5. You need the magic numbers for it to produce the same (mathematically sound) results as PHP MD5 or the command line tool included with some operating systems. See this link: FreeBSD's MD5
02/02/2011 (1:31 pm)
MD5 isn't encryption. It's a hash ;)The function above is not at all MD5. You need the magic numbers for it to produce the same (mathematically sound) results as PHP MD5 or the command line tool included with some operating systems. See this link: FreeBSD's MD5
#13
There is also an md5 resource somewhere, but as I can't find it in search (not working) on the top bar again, but works when in search, it's a nightmare to find and was originally developed in TGE and is a perfect match for the php md5 encrytion.
02/02/2011 (1:38 pm)
@Ronny - this is only part of the function, it calls the other functions in md5.cpp/.h which has the RSA equivalent md5 hashing. But, somethings not right and my knowledge in this area is pretty limited in c++ anyway. If MD5 or other encryption algorythms were to be added as a TorqueScript function it would be very useful indeed.There is also an md5 resource somewhere, but as I can't find it in search (not working) on the top bar again, but works when in search, it's a nightmare to find and was originally developed in TGE and is a perfect match for the php md5 encrytion.
#14
02/02/2011 (1:49 pm)
Yeah, the code I posted earlier I thought would work since it was an RSA MD5 equivalent. I didn't even bother to check the code, but now I am just fixing the previous resource that should do what its supposed to.
#15
02/02/2011 (2:22 pm)
Sweet, I got it working. I'll post the code when I get the chance.
#16
02/02/2011 (2:24 pm)
Great. Look forward to it, and I'm sure others will benefit from it too.
#17
02/02/2011 (3:34 pm)
Ahhh beat me too things somewhere I have md5 working in torque and could have posted
#18
02/02/2011 (3:43 pm)
Never mind Andy. Always good to have more than one type of resource though.
#19
Files
Edit : This is the original resource Resource
In consoleFunctions.cpp add this to the top, but after the other #includes
then in consoleFunctions.cpp add this at the very bottom
02/02/2011 (4:26 pm)
Download these files and put them in your "Engine/source/console" folderFiles
Edit : This is the original resource Resource
In consoleFunctions.cpp add this to the top, but after the other #includes
#include "console/stringmd5.h"
then in consoleFunctions.cpp add this at the very bottom
DefineConsoleFunction( getStringMD5, const char *, (const char *What),, "")
{
MD5_CTX context;
U8 digest[16];
static U8 digestB16[128];
U32 len = dStrlen (What);
MD5Init (context);
MD5Update (context, (U8 *) What, len);
MD5Final (digest, context);
for (U32 Index = 0; Index < 16; Index++)
{
U32 Byte1 = Index * 2;
U32 Byte2 = Byte1 + 1;
U8 Value1 = (digest[Index] & 0xF0) >> 4;
U8 Value2 = digest[Index] & 0x0F;
digestB16[Byte1] = Base16Values[Value1];
digestB16[Byte2] = Base16Values[Value2];
}
digestB16[32] = 0;
char *ret = Con::getReturnBuffer(32);
dStrcpy(ret, (char *)digestB16);
return ret;
}
#20
Edit: I can confirm this works with php md5. If you are using it to encrypt data to a web server.
02/02/2011 (4:31 pm)
Hey thanks Alex, really appreciate your help on this. You've saved me a ton of time.Edit: I can confirm this works with php md5. If you are using it to encrypt data to a web server.
Torque Owner Jules
Something2Play