MD5

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (The server side)
(JavaScript: Update to make JS version static type proof)
Line 34: Line 34:
 
... and just in case anyone was wondering. This is also possible using JavaScript:
 
... and just in case anyone was wondering. This is also possible using JavaScript:
  
<javascript>import System;
+
<javascript>#pragma strict
import System.Text;
+
 
import System.Security.Cryptography;
+
+
 
static function Md5Sum(strToEncrypt)
 
static function Md5Sum(strToEncrypt)
 
{
 
{
var encoding = UTF8Encoding();
+
var encoding = System.Text.UTF8Encoding();
 
var bytes = encoding.GetBytes(strToEncrypt);
 
var bytes = encoding.GetBytes(strToEncrypt);
 
   
 
   
 
// encrypt bytes
 
// encrypt bytes
var md5 = MD5CryptoServiceProvider();
+
var md5 = System.Security.Cryptography.MD5CryptoServiceProvider();
var hashBytes = md5.ComputeHash(bytes);
+
var hashBytes:byte[] = md5.ComputeHash(bytes);
 
   
 
   
 
// Convert the encrypted bytes back to a string (base 16)
 
// Convert the encrypted bytes back to a string (base 16)
Line 52: Line 50:
 
for (var i = 0; i < hashBytes.Length; i++)
 
for (var i = 0; i < hashBytes.Length; i++)
 
{
 
{
hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
+
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
 
}
 
}
 
   
 
   

Revision as of 17:41, 10 December 2008

Author: Matthew Wegner

Contents

Overview

This C# code snippet generates an MD5 hash for an input string. The formatting will match the output of PHP's md5() function.

C#

Best placed in your static-only utility class.

<csharp>public string Md5Sum(string strToEncrypt) { System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding(); byte[] bytes = ue.GetBytes(strToEncrypt);

// encrypt bytes System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hashBytes = md5.ComputeHash(bytes);

// Convert the encrypted bytes back to a string (base 16) string hashString = "";

for (int i = 0; i < hashBytes.Length; i++) { hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0'); }

return hashString.PadLeft(32, '0'); } </csharp>

JavaScript

... and just in case anyone was wondering. This is also possible using JavaScript:

<javascript>#pragma strict

static function Md5Sum(strToEncrypt) { 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]); }</javascript>

You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes.

The server side

As noted above, the above unity snippets will return a hash matching the one returned from PHP's md5() function. In case you are using another language on the server side, here are some examples:

Perl

<perl>use Digest::MD5 qw(md5_hex);

my $hashString = md5_hex($stringToHash); </perl>

PHP

<php><% $hashString = md5($stringToHash); %> </php>

Python

<python>import md5

def Md5Sum(inputString):

  m = md5.new()
  m.update(inputString)
  return m.hexdigest()

</python>

Shell

Requires that you have the md5sum program installed on the server. <bash>HASH = `echo "$STRING_TO_HASH" | md5sum | cut -f 1 -d' '` </bash>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox