Re: Aren't All MD5 Hashes the Same?
- From: rossum <rossum48@xxxxxxxxxxxx>
- Date: Wed, 17 Jun 2009 17:23:09 +0100
On Tue, 16 Jun 2009 21:02:43 -0600, "Jonathan Wood"
<jwood@xxxxxxxxxxxxxxxx> wrote:
Greetings,You have done well up to here. Think about how many bytes there are
I can't seem to find a solution to this.
According to Google's Safe Browsing API, the following code should produce a
matching base64-encoded checksum.
string clientKey = "8eirwN1kTwCzgWA2HxTaRQ==";
string table =
"+8070465bdf3b9c6ad6a89c32e8162ef1\t\n+86fa593a025714f89d6bc8c9c5a191ac\t\n+bbbd7247731cbb7ec1b3a5814ed4bc9d\t\n";
string mac = "dRalfTU+bXwUhlk0NCGJtQ==";
byte[] results = Convert.FromBase64String(clientKey);
in results[].
StringBuilder sb = new StringBuilder(32);Now think about how many bytes there are in key. Does this number
foreach (byte b in results)
sb.Append(b.ToString("x2"));
string key = sb.ToString();
match with the number of bytes in results[] above.
You are concatenating strings, which is easier than concatenating byte
string s = String.Format("{0}:coolgoog:{1}:coolgoog:{0}", key, table);
arrays, but more error prone. Better to concatenate byte arrays since
you will eventually be hashing a byte array.
MD5 md5 = MD5.Create();You have. Rather than treating key as an array of bytes, you are
results = md5.ComputeHash(Encoding.Default.GetBytes(s));
s = Convert.ToBase64String(results);
if (s != mac)
s = "Whoops! Doesn't match!";
So, it appears I have something wrong.
treating it as a double sized array of chars (representing hexadecimal
digits). Hence MD5 is giving you a different hash.
However, I found someone apparently
resolve this same problem at
http://stackoverflow.com/questions/181994/code-to-verify-updates-from-the-google-safe-browsing-api.
While it appears they are doing the same thing I'm doing, they do it in
another language (Python?).
Should an MD5 checksum be the same regardless of the language?Yes.
This code works for me. I have written it for clarity rather than
Can anyone see what I've missed?
Thanks.
Jonathan
speed or efficiency:
// --- Begin Code ---
public static void Main() {
// Supplied data.
string clientKey = "8eirwN1kTwCzgWA2HxTaRQ==";
string table = "+8070465bdf3b9c6ad6a89c32e8162ef1\t\n" +
"+86fa593a025714f89d6bc8c9c5a191ac\t\n" +
"+bbbd7247731cbb7ec1b3a5814ed4bc9d\t\n";
string mac = "dRalfTU+bXwUhlk0NCGJtQ==";
string coolgoog = ":coolgoog:";
// Convert input strings to byte arrays.
byte[] keyBytes = Convert.FromBase64String(clientKey);
byte[] coolBytes = System.Text.Encoding.UTF8.GetBytes(coolgoog);
byte[] tableBytes = System.Text.Encoding.UTF8.GetBytes(table);
// Concatenate the arrays in the right order.
byte[] theLot = Concatenate(keyBytes, coolBytes,
tableBytes, coolBytes, keyBytes);
// Calculate the MD5 hash.
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(theLot);
// Check the hash against the supplied MAC.
String s = Convert.ToBase64String(hash);
if (s != mac) { s = "Whoops! Doesn't match!"; }
Console.WriteLine(s);
} // end Main()
/// Concatenates the supplied byte arrays into one.
static byte[] Concatenate(params byte[][] bArrays) {
List<byte> result = new List<byte>();
foreach (byte[] bAry in bArrays) {
result.AddRange(bAry);
}
return result.ToArray();
} // end Concatenate()
// --- End Code ---
rossum
.
- Follow-Ups:
- Re: Aren't All MD5 Hashes the Same?
- From: Jonathan Wood
- Re: Aren't All MD5 Hashes the Same?
- From: Ben Voigt [C++ MVP]
- Re: Aren't All MD5 Hashes the Same?
- References:
- Aren't All MD5 Hashes the Same?
- From: Jonathan Wood
- Aren't All MD5 Hashes the Same?
- Prev by Date: Re: VS2005 can't open form
- Next by Date: Re: Aren't All MD5 Hashes the Same?
- Previous by thread: Re: Aren't All MD5 Hashes the Same?
- Next by thread: Re: Aren't All MD5 Hashes the Same?
- Index(es):
Relevant Pages
|