Re: Aren't All MD5 Hashes the Same?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Tue, 16 Jun 2009 21:02:43 -0600, "Jonathan Wood"
<jwood@xxxxxxxxxxxxxxxx> wrote:

Greetings,

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);
You have done well up to here. Think about how many bytes there are
in results[].

StringBuilder sb = new StringBuilder(32);
foreach (byte b in results)
sb.Append(b.ToString("x2"));
string key = sb.ToString();
Now think about how many bytes there are in key. Does this number
match with the number of bytes in results[] above.


string s = String.Format("{0}:coolgoog:{1}:coolgoog:{0}", key, table);
You are concatenating strings, which is easier than concatenating byte
arrays, but more error prone. Better to concatenate byte arrays since
you will eventually be hashing a byte array.


MD5 md5 = MD5.Create();
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.
You have. Rather than treating key as an array of bytes, you are
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.


Can anyone see what I've missed?

Thanks.

Jonathan
This code works for me. I have written it for clarity rather than
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

.



Relevant Pages

  • Re: populating a hash with % used as the key and F string as the value
    ... >> why are thre operators y or tr more efficient since these operators do ... >> Originally I was using an array, actually 2 arrays one with all f string ... >> This is why I want to populate a hash of arrays. ...
    (perl.beginners)
  • Re: combining hashes
    ... (hashes used to be called associative arrays), ... have order and also string access at one time without a complex ... data into a hash element as into an array element: ...
    (comp.lang.perl.misc)
  • Re: Concatenating strings
    ... Jos wrote: ... >>> I want to concatenate a string to a multidimensional array. ... >> Use cell arrays or pad with spaces. ...
    (comp.soft-sys.matlab)
  • Re: help with some capturing syntax
    ... This is what it says in my version of Perlfaq 4 ... How do I strip blank space from the beginning/end of a string? ... arrays, or even the values of a hash if you use a slice: ...
    (comp.lang.perl.misc)
  • Re: concatenate a constant to constant string using macros
    ... how to concatenate a "hash defined" constant value to another "hash ... But i need to do this at compile time, meaing i can't use the code ... Because i will get the resultant string only during execution time. ...
    (comp.lang.c)