Notifications
Clear all

[Closed] SHA1 and MD5

Hi. I’m trying to generate a MD5 or SHA1 from a string in maxscript. I found this post: http://forums.cgsociety.org/showthread.php?f=98&t=826750 and it all seems to work, until I compare the results with any other MD5 or SHA1 I used so far, for example from PHP.

The results are very different. I tried forcing the correct codepage and many other stuff (basically I’ve been fighting with this for the whole day) and ended up with nothing.

I’m not very big on maxscript, but I’ve managed so far with all the scripts I wanted to create.
What confuses me here is the use of dotNet.

I’ve learned that this problem (.NET MD5 is different from PHP) is very common.

Here are some of my finds related to this topic:
http://www.daniweb.com/web-development/php/threads/319067
http://forums.cgsociety.org/showthread.php?t=643827
http://www.gibedigital.com/blog/post/2006/02/16/MD5-hashing-in-NET-to-match-PHP.aspx

If I encode an empty string with maxscript than the results are the same as in PHP.
However when I use anything else, than the results are different.

Somehow I’m just not able to get this thing to work. I could really use your help.

This is what I’m testing it with: hello@this@is.a.test
And this is the result maxscript should generate: f236709efc76dd20f3524c80b2c7e949

BTW. How far back does the dotNetClass support go? Max 2009? Maybe earlier?

Thanks.

6 Replies

Hey, I’m looking into this, didn’t know about this issue as I’ve written the script and never touched it again

Anyway, I’m researching about it and I wanted to ask you if you could supply a php page with a md5 and sh1 form so I can test text on php and .net

Ok, I’ve found it. I was using unicode enconding and when I switched to ascii encoding it matched your md5 example right away.

function getTextHash HashMethod SourceText =
	(
		case tolower(HashMethod) of
		(
			"sha1" : hMethod = dotNetObject "System.Security.Cryptography.SHA1CryptoServiceProvider"
			default : hMethod = dotNetObject "System.Security.Cryptography.MD5CryptoServiceProvider"
		)
		ue =  dotNetObject "System.Text.ASCIIEncoding"
		bytesToHash = ue.GetBytes SourceText
		
		hash = hMethod.ComputeHash bytesToHash
		buff = dotnetObject "System.Text.StringBuilder"
		SysString = dotNetClass "System.String"
		for hashbyte in hash do
		(
			buff.Append (SysString.Format "{0:X2}" hashByte)
		)	
		buff.ToString()
	)

        oText = "hello@this@is.a.test"
	md5Text=getTextHash "MD5" oText
	sha1Text=getTextHash "SHA1" oText

        print ("MD5 text checksum: " + md5Text)
	print ("SHA1 text checksum: " + sha1Text)

Result:

“MD5 text checksum: F236709EFC76DD20F3524C80B2C7E949”
“SHA1 text checksum: E271441A814597499784CBA0EBA7F00005545E18”

Sure thing. Here you go:
http://maltaannon.com/hash.php?string=test

to use it just change the “test” to something you’re testing against.

Thanks for looking into this.

Sweet. Thanks a lot. Thumbs up. I was trying to do the same. I just hit the wall in the dotNetClass thing. Thanks again.

Thats very cool. Would you mind sharing the php code you’re using for encoding the string?

<?php

echo “MD5: “.md5($_GET[‘string’]).”</br>”;
echo “SHA1: “.sha1($_GET[‘string’]);

?>