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

[System.IO.Hashing] crc32 hash does not match #76720

Closed
onefreddy opened this issue Oct 6, 2022 · 3 comments
Closed

[System.IO.Hashing] crc32 hash does not match #76720

onefreddy opened this issue Oct 6, 2022 · 3 comments
Labels
area-System.IO.Hashing question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@onefreddy
Copy link

Hi I tried this library (Version 6.0.2 in net 6 project). And the result it gives is wrong. For comparison

  1. HashTab result: B8FF80ED
    image

  2. php result: B8FF80ED

<?php

$data = file_get_contents("diary-now.json");
$hash = strtoupper(hash('crc32b', $data));
echo $hash;
  1. c# result (wrong): ed80ffb8
        static async Task Main(string[] args)
        {
            var crc32 = new Crc32();

            using (var fs = File.OpenRead("diary-now.json"))
            {
                await crc32.AppendAsync(fs);
            }

            var checkSum = crc32.GetCurrentHash();
            var hash = BitConverter.ToString(checkSum).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 6, 2022
@vcsjones
Copy link
Member

vcsjones commented Oct 6, 2022

It looks like the endianness is backward. The bytes are backward.

B8-FF-80-ED
 
ED-80-FF-B8

According to the .NET docs:

This implementation emits the answer in the Little Endian byte order so that the CRC residue relationship

I suspect those other tools are showing the CRC32 value in big endian.

I think you just need to reverse the bytes:

var checkSum = crc32.GetCurrentHash();
Array.Reverse(checkSum);
var hash = BitConverter.ToString(checkSum).Replace("-", "").ToLower();

@jkotas jkotas added area-System.IO question Answer questions and provide assistance, not an issue with source code or documentation. labels Oct 6, 2022
@ghost
Copy link

ghost commented Oct 6, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Hi I tried this library (Version 6.0.2 in net 6 project). And the result it gives is wrong. For comparison

  1. HashTab result: B8FF80ED
    image

  2. php result: B8FF80ED

<?php

$data = file_get_contents("diary-now.json");
$hash = strtoupper(hash('crc32b', $data));
echo $hash;
  1. c# result (wrong): ed80ffb8
        static async Task Main(string[] args)
        {
            var crc32 = new Crc32();

            using (var fs = File.OpenRead("diary-now.json"))
            {
                await crc32.AppendAsync(fs);
            }

            var checkSum = crc32.GetCurrentHash();
            var hash = BitConverter.ToString(checkSum).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }
Author: onefreddy
Assignees: -
Labels:

area-System.IO, untriaged

Milestone: -

@jkotas jkotas closed this as completed Oct 6, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 6, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO.Hashing question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

4 participants