buzhash32

package
v4.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2018 License: MIT Imports: 2 Imported by: 9

Documentation

Overview

Example
package main

import (
	"fmt"
	"hash"
	"log"

	"github.com/chmduquesne/rollinghash/buzhash32"
)

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

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

	// Window len
	n := 16

	// You MUST load an initial window into the rolling hash before being
	// able to roll bytes
	rolling.Write(s[:n])

	// 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()
		classic.Write(s[i-n+1 : i+1])

		// 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 53e7e066
e quick brown fo: checksum ecf5708c
 quick brown fox: checksum c12d0faf
quick brown fox : checksum f2e76fe2
uick brown fox j: checksum a8506342
ick brown fox ju: checksum 201db638
ck brown fox jum: checksum 759fe987
k brown fox jump: checksum ecf78a18
 brown fox jumps: checksum 9062a9c9
brown fox jumps : checksum 5078232e
rown fox jumps o: checksum b1d44d0d
own fox jumps ov: checksum 8177e796
wn fox jumps ove: checksum 135d33ca
n fox jumps over: checksum 7a45e290
 fox jumps over : checksum 1655abcb
fox jumps over t: checksum 710c1810
ox jumps over th: checksum bfb01cb9
x jumps over the: checksum 6ed2c594
 jumps over the : checksum f2e2c8e7
jumps over the l: checksum df544447
umps over the la: checksum 7df8d3c3
mps over the laz: checksum c8c88cc0
ps over the lazy: checksum 3e7f980c
s over the lazy : checksum fb4663b8
 over the lazy d: checksum 31ccb20e
over the lazy do: checksum c476b45f
ver the lazy dog: checksum afb3c2da

Index

Examples

Constants

View Source
const Size = 4

The size of the checksum.

Variables

This section is empty.

Functions

func GenerateHashes

func GenerateHashes(seed int64) (res [256]uint32)

GenerateHashes generates a list of hashes to use with buzhash

Types

type Buzhash32

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

Buzhash32 is a digest which satisfies the rollinghash.Hash32 interface. It implements the cyclic polynomial algorithm https://en.wikipedia.org/wiki/Rolling_hash#Cyclic_polynomial

func New

func New() *Buzhash32

New returns a buzhash based on a list of hashes provided by a call to GenerateHashes, seeded with the default value 1.

func NewFromUint32Array

func NewFromUint32Array(b [256]uint32) *Buzhash32

NewFromUint32Array returns a buzhash based on the provided table uint32 values.

func (*Buzhash32) BlockSize

func (d *Buzhash32) BlockSize() int

BlockSize is 1 byte

func (*Buzhash32) Reset

func (d *Buzhash32) Reset()

Reset resets the Hash to its initial state.

func (*Buzhash32) Roll

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

func (d *Buzhash32) Size() int

Size is 4 bytes

func (*Buzhash32) Sum

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

Sum returns the hash as byte slice

func (*Buzhash32) Sum32

func (d *Buzhash32) Sum32() uint32

Sum32 returns the hash as a uint32

func (*Buzhash32) Write

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

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

Jump to

Keyboard shortcuts

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