Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

crc32 values are wrong? #1

Closed
cainus opened this Issue · 2 comments

2 participants

@cainus

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
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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.