MD5
m (remove blank lines) |
m (Text replace - "</javascript>" to "</syntaxhighlight>") |
||
(14 intermediate revisions by 8 users not shown) | |||
Line 9: | Line 9: | ||
Best placed in your static-only utility class. | Best placed in your static-only utility class. | ||
− | <csharp>public string Md5Sum(string strToEncrypt) | + | <syntaxhighlight lang="csharp">public string Md5Sum(string strToEncrypt) |
{ | { | ||
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding(); | System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding(); | ||
Line 28: | Line 28: | ||
return hashString.PadLeft(32, '0'); | return hashString.PadLeft(32, '0'); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
===JavaScript=== | ===JavaScript=== | ||
− | ... 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. |
− | + | To use, name your Javascript file something like "md5functions.js". | |
− | + | In your code, access the function using "hash = md5functions.Md5Sum("string");", where the prefix of the function matches the name of the .js file you created for it. | |
− | + | ||
− | static function Md5Sum(strToEncrypt) | + | <syntaxhighlight lang="javascript">#pragma strict |
+ | |||
+ | static function Md5Sum(strToEncrypt: String) | ||
{ | { | ||
− | 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) | ||
var hashString = ""; | var hashString = ""; | ||
− | + | ||
for (var i = 0; i < hashBytes.Length; i++) | for (var i = 0; i < hashBytes.Length; i++) | ||
{ | { | ||
− | hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, | + | hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]); |
} | } | ||
− | + | ||
− | return hashString.PadLeft(32, | + | return hashString.PadLeft(32, "0"[0]); |
− | }</ | + | }</syntaxhighlight> |
You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes. | You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes. | ||
Line 65: | Line 66: | ||
===Perl=== | ===Perl=== | ||
− | <perl> | + | <perl>use Digest::MD5 qw(md5_hex); |
− | use Digest::MD5 qw( | + | |
− | my $hashString = | + | my $hashString = md5_hex($stringToHash); |
+ | </perl> | ||
+ | |||
+ | ===PHP=== | ||
+ | <perl> | ||
+ | $hashString = md5($stringToHash); | ||
</perl> | </perl> | ||
===Python=== | ===Python=== | ||
− | <python> | + | <python>import hashlib |
− | import | + | |
− | def | + | def md5Sum(inputString): |
− | + | return hashlib.md5(inputString).hexdigest() | |
− | + | ||
− | + | ||
</python> | </python> | ||
+ | |||
+ | ===Ruby=== | ||
+ | <ruby>require 'digest/md5' | ||
+ | |||
+ | def md5Sum(inputString) | ||
+ | Digest::MD5.hexdigest(inputString) | ||
+ | end | ||
+ | </ruby> | ||
===Shell=== | ===Shell=== | ||
Requires that you have the md5sum program installed on the server. | Requires that you have the md5sum program installed on the server. | ||
− | <bash> | + | <bash>HASH = `echo "$STRING_TO_HASH" | md5sum | cut -f 1 -d' '` |
− | HASH = `echo "$STRING_TO_HASH" | md5sum | cut -f 1 -d' '` | + | |
</bash> | </bash> |
Latest revision as of 20:52, 10 January 2012
Author: Matthew Wegner
Contents |
[edit] Overview
This C# code snippet generates an MD5 hash for an input string. The formatting will match the output of PHP's md5() function.
[edit] C#
Best placed in your static-only utility class.
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'); }
[edit] JavaScript
... and just in case anyone was wondering. This is also possible using JavaScript.
To use, name your Javascript file something like "md5functions.js". In your code, access the function using "hash = md5functions.Md5Sum("string");", where the prefix of the function matches the name of the .js file you created for it.
#pragma strict 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]); }
You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes.
[edit] 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:
[edit] Perl
<perl>use Digest::MD5 qw(md5_hex);
my $hashString = md5_hex($stringToHash); </perl>
[edit] PHP
<perl> $hashString = md5($stringToHash); </perl>
[edit] Python
<python>import hashlib
def md5Sum(inputString):
return hashlib.md5(inputString).hexdigest()
</python>
[edit] Ruby
require 'digest/md5'
def md5Sum(inputString)
Digest::MD5.hexdigest(inputString)
end
[edit] Shell
Requires that you have the md5sum program installed on the server. <bash>HASH = `echo "$STRING_TO_HASH" | md5sum | cut -f 1 -d' '` </bash>