Link to this headingCRC
Link to this headingCollision
#
# CRC-32 forcer (Python)
#
# Copyright (c) 2020 Project Nayuki
# https://www.nayuki.io/page/forcing-a-files-crc-to-any-value
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see COPYING.txt).
# If not, see <http://www.gnu.org/licenses/>.
#
# ---- Main application ----
# Handle arguments
return
: =
return
return
return
: =
return
: =
return
# Process the file
return +
return +
return +
return None
# ---- Main function ----
# Public library function. offset is uint, and newcrc is uint32.
# May raise IOError, ValueError, AssertionError.
: =
# Read entire file and calculate original CRC-32 value
: =
# Compute the change to make
: = ^
=
# Patch 4 bytes in the file
: =
^= & 0xFF
# Recheck entire file
:
# ---- Utilities ----
: = 0x104C11DB7 # Generator polynomial. Do not modify, because there are many dependencies
: = - 1
: = 0
: =
return
=
: = 0
= |
>>= 1
return
# ---- Polynomial arithmetic ----
# Returns polynomial x multiplied by polynomial y modulo the generator polynomial.
# Russian peasant multiplication algorithm
: = 0
^= *
>>= 1
<<= 1
^=
return
# Returns polynomial x to the power of natural number y modulo the generator polynomial.
# Exponentiation by squaring
: = 1
=
=
>>= 1
return
# Computes polynomial x divided by polynomial y, returning the quotient and remainder.
return
: =
: = 0
^= <<
|= 1 <<
return
# Returns the reciprocal of polynomial x with respect to the modulus polynomial m.
# Based on a simplification of the extended Euclidean algorithm
: =
=
: = 0
: = 1
, =
= ^
=
=
=
=
return
return - 1
# ---- Miscellaneous ----
=