473,513 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MD5 hash in C# vs. others

Hi all,

I am trying to generate an MD5 hash for a string (ABC) but I am getting a
different hash when I use the C# program below vs. a script I wrote in Perl
or other MD5 programs. Here is the relevant C# code:

string sData = "ABC";
string sResult = "";
Byte[] data1ToHash = ConvertStringToByteArray(sData);
byte[] hashvalue1 = ((HashAlgorithm)
CryptoConfig.CreateFromName("MD5")).ComputeHash(da ta1ToHash);
sResult = BitConverter.ToString(hashvalue1);

public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}

The resulting hash is:
71-67-74-B5-3A-92-B9-85-25-BB-A1-79-A1-59-CC-71

while the other programs show:

90-2F-BD-D2-B1-DF-0C-4F-70-B4-A5-D2-35-25-E9-32

See for example this online MD5 javascript:
http://pajhome.org.uk/crypt/md5/. Here is the Perl script I wrote
(generating the same hash as the online JavaScript):

$u = utf8("ABC");
$md5 = new MD5;
$md5->add($u);
$digest = $md5->digest();
$Hash = unpack("H*", $digest);
print "Hash: $Hash\n";

What I am doing wrong? Is it the Unicode encoding (UTF-16 vs. UTF-8)? If
yes, do I have to transform the string to Unicode before being able to use
the MD5 class?

--
Regards,

Adrian Grigorof
Nov 17 '05 #1
2 5108
Adrian Grigorof <adi@my_last_name_here.com> wrote:

<snip>
What I am doing wrong? Is it the Unicode encoding (UTF-16 vs. UTF-8)?
Absolutely.
If yes, do I have to transform the string to Unicode before being able to use
the MD5 class?


No - just use Encoding.UTF8 instead of UnicodeEncoding.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks Jon, just for the record, using the same encoding type did the trick.
--
Regards,

Adrian

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adrian Grigorof <adi@my_last_name_here.com> wrote:

<snip>
What I am doing wrong? Is it the Unicode encoding (UTF-16 vs. UTF-8)?


Absolutely.
If yes, do I have to transform the string to Unicode before being able to use the MD5 class?


No - just use Encoding.UTF8 instead of UnicodeEncoding.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5815
by: Weiguang Shi | last post by:
Hi there, I'm thinking of using binascii.crc32 as a hash-function when I read in the reference http://www.python.org/doc/current/lib/module-binascii.html: crc32( data) Compute CRC-32, the...
34
14425
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
47
5029
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
12
3183
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
12
6976
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
21
3171
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
21
3856
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
11
2660
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
1
3580
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.