crc32

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

README

GitHub release Build Status Coverage Status Go Report Card GoDoc Go version Go version

crc32

An implementation of an algorithm to modify a file so that its CRC-32 checksum matches a given value. This requires four sacrificial bytes in the file that will be modified to generate the desired value. A small example:

f, err := os.OpenFile("somefile", O_RDWR, 0) // Remember to open read/write!
if err != nil {
        log.Fatal(err)
}
defer f.Close()

if err := crc32.ForceCRC32(f, 0, 0xdeadbeef); err != nil {
        log.Fatal(err)
}

In this example the first four bytes in somefile will be modified so that the CRC-32 checksum of somefile will be 0xdeadbeef however the bytes can be anywhere in the file, but must be contiguous.

Documentation

Overview

Package crc32 is an implementation of an algorithm to modify a file so that its CRC-32 checksum matches a given value. This requires four sacrificial bytes in the file that will be modified to generate the desired value.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForceCRC32

func ForceCRC32(rws io.ReadWriteSeeker, offset int64, desiredCRC uint32) error

ForceCRC32 takes an io.ReadWriteSeeker and updates the four bytes starting at offset such that the IEEE CRC-32 value of the whole stream matches desiredCRC. If the CRC-32 value already matches, no changes are made.

Example
package main

import (
	"encoding/hex"
	"fmt"
	"io"
	"os"

	"github.com/bodgit/crc32"
)

func main() {
	f, err := os.CreateTemp("", "")
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}

		if err := os.RemoveAll(f.Name()); err != nil {
			panic(err)
		}
	}()

	b := []byte("This is a test 0000")

	if _, err := f.Write(b); err != nil {
		panic(err)
	}

	if err := crc32.ForceCRC32(f, int64(len(b)-4), 0xdeadbeef); err != nil {
		panic(err)
	}

	if _, err := f.Seek(0, io.SeekStart); err != nil {
		panic(err)
	}

	if _, err := io.ReadFull(f, b); err != nil {
		panic(err)
	}

	fmt.Print(hex.Dump(b))

}
Output:

00000000  54 68 69 73 20 69 73 20  61 20 74 65 73 74 20 99  |This is a test .|
00000010  3c 5f 27                                          |<_'|

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL