Actually, it's part of some rather complex code used to create shareware
authorization licenses so I can't really post a complete program here. But a
key part calculates a 32-bit checksum of a string and looks something like
this (I'm still trying to figure out what's available in the CRT). This is
designed to duplicate some existing C++ code where each character is
converted to a WORD (so I get the same result in both multibyte and Unicode
versions). The result is then computed by adding all four DWORDs of the
result, which is a bit awkward in C#.
UInt32 CalcCheckSum(string s)
{
MD5 md5 = MD5.Create();
// md5.Initialize();
byte[] data = StringToByteArray(s);
data = md5.ComputeHash(data);
UInt32 result = 0;
for (int i = 0; i < data.Length; i += sizeof(UInt32))
result += ByteArrayToDword(data, i);
return result;
}
byte[] StringToByteArray(string s)
{
byte[] b = new byte[s.Length << 1];
for (int i = 0; i < s.Length; i++)
{
b[i << 1] = (byte)s[i];
b[(i << 1) + 1] = (byte)(s[i] >8);
}
return b;
}
UInt32 ByteArrayToDword(byte[] b, int startIndex)
{
return (UInt32)(b[startIndex] |
b[startIndex + 1] << 8 |
b[startIndex + 2] << 16 |
b[startIndex + 3] << 24);
}
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jonathan Wood <jw***@softcircuits.comwrote:
>Does anyone know why the documentation for
System.Security.Cryptography.MD5.Create() seems to omit completely any
description of allowed arguments.
I'm trying to convert some C++ code to C# and seem to be getting
different
MD5 results. If other algorithms are available, maybe that is related to
the
problem I'm having. But the docs for this method don't tell me anything
about what algorithms are supported?
MD5 *is* the algorithm - the version which accepts names of different
implementations of the algorithm should by and large be ignored.
Could you post a short but complete program which demonstrates the
problem you're having? I wouldn't be surprised if it were to do with
converting the data you've got into a binary form to be hashed in the
first place.
--
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