adler32

package
v0.0.0-...-5fdc4cc Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package adler32 implements the Adler-32 rolling checksum.

Example
package main

import (
	"fmt"
	"log"

	adler32 "github.com/greatroar/rolling/adler32"
)

func main() {
	const w = 16 // Window size.

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

	classic := adler32.New()
	rolling := adler32.NewRolling(w)

	// You must load an initial window into the rolling hash before being
	// able to roll bytes.
	rolling.WriteInitial(s[:w])

	for i := rolling.WindowSize(); i < len(s); i++ {
		// Reset and write the window in the "classic" hash.
		classic.Reset()
		classic.Write(s[i-w+1 : i+1])

		// Roll the hash by showing it the incoming and outgoing bytes.
		rolling.Roll(s[i], s[i-w])

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

		// Compare the hashes
		if classic.Sum32() != rolling.Sum32() {
			log.Fatalf("%v: expected %x, got %x",
				s[i-w+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

This section is empty.

Variables

This section is empty.

Functions

func Checksum

func Checksum(p []byte) uint32

Checksum returns the Adler-32 checksum of p.

Types

type Hash

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

A Hash computes the Adler-32 checksum without rolling.

This is a fast replacement for the standard hash/adler32 package.

func New

func New() *Hash

New returns a new non-rolling Adler-32 hasher.

func (*Hash) BlockSize

func (h *Hash) BlockSize() int

func (*Hash) Reset

func (h *Hash) Reset()

func (*Hash) Size

func (h *Hash) Size() int

Size returns the number of bytes appended by Sum, which is four.

func (*Hash) Sum

func (h *Hash) Sum(d []byte) []byte

Sum appends the checksum accumulates by h to d and returns the result.

func (*Hash) Sum32

func (h *Hash) Sum32() uint32

Sum32 returns the Adler-32 checksum.

func (*Hash) Write

func (h *Hash) Write(p []byte) (int, error)

Write updates h by the data p. It always returns len(p), nil.

type Rolling

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

A Rolling computes the Adler-32 checksum with rolling.

func NewRolling

func NewRolling(windowSize uint32) *Rolling

NewRolling returns a new rolling Adler-32 hasher.

func (*Rolling) Reset

func (h *Rolling) Reset()

Reset sets h to its initial state. The window size is unmodified.

func (*Rolling) Roll

func (h *Rolling) Roll(in, out byte)

Roll updates h by an incoming and an outgoing byte.

func (*Rolling) Sum32

func (h *Rolling) Sum32() uint32

Sum32 returns the Adler-32 checksum.

func (*Rolling) WindowSize

func (h *Rolling) WindowSize() int

WindowSize returns the window size passed to New.

func (*Rolling) WriteInitial

func (h *Rolling) WriteInitial(p []byte) int

WriteInitial feeds initial data p to h.

Jump to

Keyboard shortcuts

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