adler32

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: 5 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"hash"
	"hash/adler32"
	"log"

	_adler32 "github.com/chmduquesne/rollinghash/v4/adler32"
)

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

	classic := hash.Hash32(adler32.New())
	rolling := _adler32.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 31e905d9
e quick brown fo: checksum 314805e0
 quick brown fox: checksum 30ea05f3
quick brown fox : checksum 34dc05f3
uick brown fox j: checksum 33b705ec
ick brown fox ju: checksum 325205ec
ck brown fox jum: checksum 31b105f0
k brown fox jump: checksum 317d05fd
 brown fox jumps: checksum 30d10605
brown fox jumps : checksum 34d50605
rown fox jumps o: checksum 34c60612
own fox jumps ov: checksum 33bb0616
wn fox jumps ove: checksum 32d6060c
n fox jumps over: checksum 316c0607
 fox jumps over : checksum 304405b9
fox jumps over t: checksum 3450060d
ox jumps over th: checksum 33fe060f
x jumps over the: checksum 33120605
 jumps over the : checksum 313e05ad
jumps over the l: checksum 353605f9
umps over the la: checksum 348505f0
mps over the laz: checksum 332905f5
ps over the lazy: checksum 32590601
s over the lazy : checksum 310905b1
 over the lazy d: checksum 2f7a05a2
over the lazy do: checksum 336a05f1
ver the lazy dog: checksum 326205e9

Index

Examples

Constants

View Source
const (
	Mod  = 65521
	Size = 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adler32

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

Adler32 is a digest which satisfies the rollinghash.Hash32 interface. It implements the adler32 algorithm https://en.wikipedia.org/wiki/Adler-32

func New

func New() *Adler32

New returns a new Adler32 digest

func (*Adler32) BatchBoundaries added in v4.2.0

func (d *Adler32) 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(b<<16 | a)" with the masked test on that value. It does not modify the receiver. (a and b are the lane hit buffers; the adler accumulators are aA/bA and aB/bB.)

func (*Adler32) BatchRoll added in v4.2.0

func (d *Adler32) 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 modular-arithmetic chains overlap in the pipeline. BatchRoll does not modify the receiver.

func (*Adler32) BlockSize

func (d *Adler32) BlockSize() int

BlockSize is 1 byte

func (*Adler32) Reset

func (d *Adler32) Reset()

Reset resets the digest to its initial state.

func (*Adler32) Roll

func (d *Adler32) Roll(b byte)

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

func (*Adler32) Size

func (d *Adler32) Size() int

Size is 4 bytes

func (*Adler32) Sum

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

Sum returns the hash as a byte slice

func (*Adler32) Sum32

func (d *Adler32) Sum32() uint32

Sum32 returns the hash as a uint32

func (*Adler32) Write

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

Write appends data to the rolling window and updates the digest.

func (*Adler32) WriteWindow

func (d *Adler32) 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