bozo32

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"hash"
	"log"

	"github.com/chmduquesne/rollinghash/v4/bozo32"
)

func main() {
	s := []byte("The quick brown fox jumps over the lazy dog")

	classic := hash.Hash32(bozo32.New())
	rolling := bozo32.New()

	// Window len
	n := 16

	// You MUST load an initial window into the rolling hash before being
	// able to roll bytes
	if _, err := rolling.Write(s[:n]); err != nil {
		log.Fatal(err)
	}

	// Roll it and compare the result with full re-calculus every time
	for i := n; i < len(s); i++ {

		// Reset and write the window in classic
		classic.Reset()
		if _, err := classic.Write(s[i-n+1 : i+1]); err != nil {
			log.Fatal(err)
		}

		// Roll the incoming byte in rolling
		rolling.Roll(s[i])

		fmt.Printf("%v: checksum %x\n", string(s[i-n+1:i+1]), rolling.Sum32())

		// Compare the hashes
		if classic.Sum32() != rolling.Sum32() {
			log.Fatalf("%v: expected %x, got %x",
				s[i-n+1:i+1], classic.Sum32(), rolling.Sum32())
		}
	}

}
Output:
he quick brown f: checksum 43ccedc8
e quick brown fo: checksum 58edb94f
 quick brown fox: checksum 24a53172
quick brown fox : checksum 2a953a52
uick brown fox j: checksum 68660e2b
ick brown fox ju: checksum a0dcc87b
ck brown fox jum: checksum a971cf
k brown fox jump: checksum 87384fec
 brown fox jumps: checksum 8aaa9434
brown fox jumps : checksum 930670f4
rown fox jumps o: checksum b1f3d3c1
own fox jumps ov: checksum 544099b5
wn fox jumps ove: checksum d4d1655b
n fox jumps over: checksum 1fafbea6
 fox jumps over : checksum cd48b1f8
fox jumps over t: checksum c986b2cc
ox jumps over th: checksum c6221c0e
x jumps over the: checksum aaf3c224
 jumps over the : checksum 316bd78c
jumps over the l: checksum 110b7f18
umps over the la: checksum 6580478f
mps over the laz: checksum 5b76ba4
ps over the lazy: checksum bedd0670
s over the lazy : checksum 43588f20
 over the lazy d: checksum cbaf2811
over the lazy do: checksum 579ec750
ver the lazy dog: checksum cfe7b948

Index

Examples

Constants

View Source
const Size = 4

The size of the checksum.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bozo32

type Bozo32 struct {
	// contains filtered or unexported fields
}

Bozo32 is a digest which satisfies the rollinghash.Hash32 interface.

func New

func New() *Bozo32

func NewFromInt

func NewFromInt(a uint32) *Bozo32

NewFromInt returns a Bozo32 with the given multiplier a. For good rolling-hash and CDC properties, a should be odd and greater than 1: even multipliers cause low bits to accumulate factors of 2 for large windows, and a=1 collapses the hash to a bounded sum. Use New() for a safe default.

func (*Bozo32) BatchBoundaries added in v4.2.0

func (d *Bozo32) BatchBoundaries(a, b []int32, data []byte, window int, mask uint64) (na, nb int)

BatchBoundaries reports the window positions where the rolling checksum satisfies sum & mask == 0, fusing the test into the hashing loop (see the boundary fast path). It mirrors BatchRoll exactly, replacing each "dst[i] = uint64(v)" with the masked test on the zero-extended value. It does not modify the receiver.

func (*Bozo32) BatchRoll added in v4.2.0

func (d *Bozo32) BatchRoll(dst []uint64, data []byte, window int)

BatchRoll computes the rolling checksum of every window-sized slice of data in one pass and writes them to dst, which must have len(data)-window+1 elements: dst[i] is the checksum of data[i:i+window] (the 32-bit value zero-extended into a uint64). It is equivalent to Write(data[:window]) followed by a Roll for each subsequent byte, recording Sum32 after each step, but it indexes the leaving byte directly (data[i]) instead of keeping a circular window and rolls two independent lanes so their multiplies overlap in the pipeline. BatchRoll does not modify the receiver; only d.a (the multiplier) is read.

func (*Bozo32) BlockSize

func (d *Bozo32) BlockSize() int

BlockSize is 1 byte

func (*Bozo32) Reset

func (d *Bozo32) Reset()

Reset resets the Hash to its initial state.

func (*Bozo32) Roll

func (d *Bozo32) Roll(c byte)

Roll updates the checksum of the window from the entering byte. You MUST initialize a window with Write() before calling this method.

func (*Bozo32) Size

func (d *Bozo32) Size() int

Size is 4 bytes

func (*Bozo32) Sum

func (d *Bozo32) Sum(b []byte) []byte

Sum returns the hash as byte slice

func (*Bozo32) Sum32

func (d *Bozo32) Sum32() uint32

Sum32 returns the hash as a uint32

func (*Bozo32) Write

func (d *Bozo32) Write(data []byte) (int, error)

Write appends data to the rolling window and updates the digest. It never returns an error.

func (*Bozo32) WriteWindow

func (d *Bozo32) WriteWindow(w io.Writer) (n int, err error)

WriteWindow writes the contents of the current window to w.

Jump to

Keyboard shortcuts

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