To send data over internet, as scores or player names, the best practice is encrypt data to prevent sniffing.
Sniffing allows individuals to capture data as it is transmitted over a network.

The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.
MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4.The source code in RFC 1321 contains a “by attribution” RSA license.

Unity3D

Create in Unity3D:
1. Main Camera
2. Empty GameObject, name it GameController, attach to it the next script:

MD5Function.js:


#pragma strict

var myString : String = "Andrea"; // my string
var myStringMd5 : String;         // my encrypted string

// Button to encript START
function OnGUI () {
    if (GUI.Button (Rect (10,10,550,100), "String: " + myString + " " + "Ecrypted: " + myStringMd5)) {
        // send datas to function to encrypt
        myStringMd5 = Md5Sum(myString);
    }
} // END OnGUI
// Button to encript END 
 
// MD5 Encrytpt START ############################################################### 
// Server side note: the output is the same of the PHP function - md5($myString) -
static function Md5Sum(strToEncrypt: String)
{
	var encoding = System.Text.UTF8Encoding();
	var bytes = encoding.GetBytes(strToEncrypt);
 
	// encrypt bytes
	var md5 = System.Security.Cryptography.MD5CryptoServiceProvider();
	var hashBytes:byte[] = md5.ComputeHash(bytes);
 
	// Convert the encrypted bytes back to a string (base 16)
	var hashString = "";
 
	for (var i = 0; i < hashBytes.Length; i++)
	{
		hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
	}
 
	return hashString.PadLeft(32, "0"[0]);
}// End Md5Sum
// MD5 Encrypt END #####################################################################

The result is: 28f719c89ef7f33ce2e178490676b5ab

Server Side – PHP

We can verify the result using the PHP code:


<?php
$str = 'Andrea';

if (md5($str) === '28f719c89ef7f33ce2e178490676b5ab') {
    echo "Yes! He is Andrea!";
}
?>

My official website: http://www.lucedigitale.com

Original Article: http://wiki.unity3d.com/index.php?title=MD5