Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to use perl digest module to calculate CRC?

by guyra (Novice)
on Mar 20, 2013 at 15:39 UTC ( #1024554=perlquestion: print w/ replies, xml ) Need Help??
guyra has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need to implemenmt CRC-32 (with custom poly) in Perl.

I have seen that there is a module called digest::crc that does it.

However, when I compare the result to an online calculator, I dont get the same CRC code.

My poly is "101101" (bin) or "2d" (hex)

My data is "1e5"

The online calc is:

https://ghsi.de/CRC/index.php?Polynom=101101&Message=1e5

The result that I get from the calc is "1010" (bin) or "A" (hex)

This is the perl code I used (found somewhere online...):

use strict; use warnings; use Digest::CRC; my $string = 0x01e5; my $ctx = Digest::CRC->new(type=>"crc32", poly=>0x2D); $ctx->add($string); print "CRC for '$string' is 0x" . $ctx->hexdigest . "\n";

This is the output of this perl:

CRC for '485' is 0x9d0fec86

I'm pretty sure that the online calculator is correct.

Can someone help me understand what is wrong with my Perl code?

Thx!

Guy

Comment on How to use perl digest module to calculate CRC?
Download Code
Re: How to use perl digest module to calculate CRC?
by BrowserUk (Pope) on Mar 20, 2013 at 16:04 UTC

    The online version is returning a single byte checksum. Perhaps you need width => 8?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to use perl digest module to calculate CRC?
by RichardK (Priest) on Mar 20, 2013 at 16:12 UTC

    Exactly what is your input data? is it the hex number 0x1e5, (485 decimal), or the string "0x1e5" ?

    Perl handles these two differently so try running these one liners, and it might make things clearer

    perl -E 'my $x=0x1e5; say $x;' # will print 485 perl -E 'my $x="0x1e5"; say $x;' #will print 0x1e5

      My input data is actualy 1e5

      Meaning it is 0001 1110 0101

      Eventtually I'm going to have a long file with bytes in hex which I need to calculate CRC on.

Re: How to use perl digest module to calculate CRC?
by BrowserUk (Pope) on Mar 21, 2013 at 10:21 UTC

    That CRC implementation is broken. It is not manipulating bits; but rather bytes of 1s and 0s. Digest::CRC will never match its output.

    It could be that the conflation of bits and bytes is deliberate for sound reasoning; but if I had to guess (which I do as I cannot locate further info on the code), it is simply that the author didn't understand the difference.

    Why have you selected that particular CRC algorithm/implementation as you model? If it is simply the first result you found; choose more wisely.

    It isn't hard to match its output; but you would be better off using a proper CRC.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I understand.

      Can you recomend a proper crc calculator?

      Thx
        Can you recomend a proper crc calculator?

        Do you mean code, or an on-line calculator?

        Either way, the first thing to know is: why are you calculating a crc? What are you hoping it will do for you?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024554]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-10-25 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (140 votes), past polls