Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crc32 values are wrong? #1

Closed
cainus opened this issue Aug 1, 2012 · 2 comments
Closed

crc32 values are wrong? #1

cainus opened this issue Aug 1, 2012 · 2 comments

Comments

@cainus
Copy link

cainus commented Aug 1, 2012

var hash = require("mhash").hash;
hash("crc32", "alejandro");
// '1c7af1b5'
hash("crc32b", "alejandro");
// '4fa1c5aa'

I believe the response should be AAC5A14F for crc32. I've tried this in ruby, php, and two online sources and they all agree. The expected decimal value would then be 2865078607 .

This package gets it right: https://npmjs.org/package/crc32

Let me know if I'm calling it wrong though or doing something else that's obvious. FYI, I'm on a 64-bit version of node.

@Sembiance
Copy link
Owner

Short story: this appears to be a problem with the mhash library itself.

Long story:

Results from several libs and languages:

ruby : aac5a14f

require "zlib";
print Zlib::crc32("alejandro").to_s(16);

python : 0xaac5a14fL

import zlib;
print hex(zlib.crc32("alejandro") & 0xffffffffL);

perl : aac5a14f

use String::CRC32;
printf "%x\n", crc32("alejandro");

php : aac5a14f

echo(dechex(crc32("alejandro")));

php : aac5a14f

echo(hash("crc32b", "alejandro"))

java : aac5a14f

import java.util.zip.CRC32;

public class Test
{
    public static void main(String[] args)
    {
        CRC32 hasher = new CRC32();
        hasher.update("alejandro".getBytes());
        System.out.println(Long.toHexString(hasher.getValue()));
    }
}

So all those seem to agree.

Note however the last PHP example was using hash("crc32b") to compute the correct hash.

If I use just regular hash("crc32") in PHP:

php : 1c7af1b5

echo(hash("crc32", "alejandro"));

Which is the same thing that the "mhash" lib creates:

javascript(mhash) : 1c7af1b5

var mhash = require("mhash");
console.log(mhash("crc32", "alejandro"));

Just to further verify that the node.js mhash binding code isn't faulty, I tried the Python mhash module:

python(mhash) : 1c7af1b5

import mhash;
hasher = mhash.MHASH(mhash.MHASH_CRC32);
hasher.update("alejandro")
print hasher.hexdigest();

The python bindings to mhash also produce the same result, so the node.js mhash binding code does not appear to be the problem.

It appears that the 'mhash' lib just uses some sort of different CRC32 algo than is standard in most languages.

In fact I've found other people having this same issue with CRC32 Checksums behaving incorrectly:
http://developers.blog.box.com/2011/10/12/crc32-checksums-the-good-the-bad-and-the-ugly/

I haven't found any way to get the mhash() library to produce the same value that zlib and others do.

@cainus
Copy link
Author

cainus commented Aug 3, 2012

Ah okay... makes sense... as an mhash wrapper, this fix is understandably a bit out of scope for your lib. You'd only be introducing inconsistency with mhash.

I'll close. Thanks for the swift reply.

@cainus cainus closed this as completed Aug 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants