bozo32

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

Documentation

Overview

Example
package main

import (
	"fmt"
	"hash"
	"log"

	"github.com/chmduquesne/rollinghash/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
	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 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

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.

Jump to

Keyboard shortcuts

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