The code below takes an arbitrary string, and adds some junk on the end of it to make the CRC come out to any arbitrary value. If you were going to forge many CRCs with the same polynomial, you could optimize the inner loop with a lookup table.
use String::CRC32; my $initial_str = "foo bar"; my $desired_crc = 0x12345678; my $magic = forge_crc(value => $desired_crc ^ 0xffffffff) ^ crc32($initial_str) ^ 0xffffffff; my $forged_str = $initial_str . pack("V", $magic); printf "%x\n", crc32($forged_str); sub forge_crc { my (%opts) = @_; # default to the usual crc32 polynomial my $poly = $opts{poly} || 0xedb88320; my $size = $opts{size} || 32; my $highbit = 1 << ($size-1); my $reg = $opts{value}; $poly = ($poly << 1) ^ 1; for (1..$size) { $reg = ($reg & $highbit) ? ($reg << 1) ^ $poly : $reg << 1; } return $reg; } # forge_crc

In reply to Forge CRCs by no_slogan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.