I wander if anyone can help ?

I wander if anyone can help ?
I have an Android function generating check sum of string.

public String calculateCHKsum(String data)
{
byte bytes[] = data.getBytes();
Checksum checksum = new CRC32();
// update the current checksum with the specified array of bytes
checksum.update(bytes, 0, bytes.length);
// get the current checksum value
String enc = String.format("%08X", checksum.getValue());
return enc;
}
I am sending the file to Delphi checking the check sum with

function MD5(const Data: string): string;
var idmd5: TIdHashMessageDigest5;
hash: T4x4LongWordRecord;
begin
idmd5 := TIdHashMessageDigest5.Create;
try
result := idmd5.AsHex(idmd5.HashValue(Data));
finally
idmd5.Free;
end;
end;

Obviously the number are not exact. one is 8 bytes the other is long

C62EF121 << android
686F15EA8F0F12F5C3B05BED6CB3EBF5 <<< delphi.

Using Delphi 7. Any way to solve except making my own crc?

Comments

  1. You create a 32 bit CRC hash with Java.
    You create a 128 bit MD5 hash with Delphi.
    The results of both methods will never be the same.

    ReplyDelete
  2. > Any way to solve except making my own crc?

    Yes, use CRC32 in Delphi as well. You can find plenty around the internet.

    ReplyDelete
  3. You appear to ignore the issue of encoding. A string is not an array of bytes. Text is not binary. Pick an encoding. Say UTF-8. Encode the text using that encoding. Now you have an array of bytes. Hash that array of bytes.

    ReplyDelete
  4. Christopher Wosinski you where right. My mistake. Thanks

    ReplyDelete
  5. Asbjørn Heid Thanks guys my mistake

    ReplyDelete
  6. shlomo abuisak I was a bit quick to read (on my way out the door), what David said regarding encoding is essential. Either operate on the bytes of the file directly, or if it's pure text, convert to a fixed encoding like UTF-8.

    ReplyDelete
  7. FWIW the CRC32 hash is 4 bytes long, not 8 bytes

    ReplyDelete
  8. shlomo abuisak Do you know which encoding String.getBytes() uses? You need to be able to answer that question to know you have correctly solved the problem.

    ReplyDelete

Post a Comment