# Hashing and Checksums

> StringZilla for Go

A fast 64-bit additive checksum and a seeded 64-bit non-cryptographic hash:

```go
func Bytesum(str string) uint64
func Hash(str string, seed uint64) uint64
```

```go
sum := sz.Bytesum("hello")
h := sz.Hash("hello", 42)
```

For incremental hashing, `Hasher` implements `hash.Hash64` and `io.Writer`:

```go
func NewHasher(seed uint64) *Hasher
func (h *Hasher) Write(p []byte) (n int, err error)
func (h *Hasher) Sum(b []byte) []byte
func (h *Hasher) Sum64() uint64
func (h *Hasher) Digest() uint64
func (h *Hasher) Reset()
func (h *Hasher) Size() int
func (h *Hasher) BlockSize() int
```

`Sum64` and its alias `Digest` return the current digest without consuming the state.
Create a `Hasher` with `NewHasher(seed)`; its zero value is not ready to use.

```go
h := sz.NewHasher(0)
h.Write([]byte("hello "))
h.Write([]byte("world"))
digest := h.Sum64()
```

### SHA-256

A cryptographic SHA-256 is available both as a one-shot and as a streaming hasher implementing `hash.Hash` and `io.Writer`:

```go
func HashSha256(data []byte) [32]byte

func NewSha256() *Sha256
func (h *Sha256) Write(p []byte) (n int, err error)
func (h *Sha256) Sum(b []byte) []byte
func (h *Sha256) Digest() [32]byte
func (h *Sha256) Hexdigest() string
func (h *Sha256) Reset()
func (h *Sha256) Size() int
func (h *Sha256) BlockSize() int
```

`Digest` returns the raw 32-byte hash and `Hexdigest` returns its lowercase hex string, both without consuming the state.
Create a `Sha256` with `NewSha256()`; its zero value is not ready to use.

```go
sum := sz.HashSha256([]byte("hello"))

h := sz.NewSha256()
h.Write([]byte("hello"))
fmt.Println(h.Hexdigest())
```

`import "github.com/ashvardanian/stringzilla/golang"`

## Hasher

Hasher is a streaming 64-bit non-cryptographic hasher that implements hash.Hash64 and io.Writer.

```go
type Hasher struct {
    // contains filtered or unexported fields
}
```

Create one with [NewHasher](#NewHasher); the zero value is not ready to use.

## NewHasher

NewHasher creates a new streaming hasher with the given seed.

```go
func NewHasher(seed uint64) *Hasher
```

## Block*BlockSize

BlockSize returns the hash's underlying block size.

```go
func (h *Hasher) BlockBlockSize() int
```

Implements hash.Hash.

## *

Digest returns the current 64-bit hash without consuming the state.

```go
func (h *Hasher) Digest() uint64
```

- `Digest`
- `Sum64`

This is an alias for [Hasher.Sum64](#Sum64).

Implements hash.Hash64.

## *Reset

Reset resets the hasher to its initial state.

```go
func (h *Hasher) Reset()
```

Implements hash.Hash.

## *Size

Size returns the number of bytes Sum will return.

```go
func (h *Hasher) Size() int
```

Implements hash.Hash.

## *Sum

Sum appends the current hash to b and returns the resulting slice.

```go
func (h *Hasher) Sum(b []byte) []byte
```

It does not change the underlying hash state. Implements hash.Hash.

## Write

Write adds data to the streaming hasher.

```go
func (h *Hasher) Write(p []byte) (n int, err error)
```

Implements io.Writer.

## Sha256

Sha256 is a streaming SHA-256 hasher that implements hash.Hash and io.Writer.

```go
type Sha256 struct {
    // contains filtered or unexported fields
}
```

Create one with [NewSha256](#NewSha256); the zero value is not ready to use.

## NewSha256

NewSha256 creates a new streaming SHA-256 hasher.

```go
func NewSha256() *Sha256
```

## Digest

Digest returns the current SHA-256 hash as a 32-byte array without consuming the state.

```go
func (h *Sha256) Digest() [32]byte
```

This is a convenience method in addition to the standard hash.Hash interface.

## Hexdigest

Hexdigest returns the current SHA-256 hash as a lowercase hexadecimal string.

```go
func (h *Sha256) Hexdigest() string
```

This is a convenience wrapper over [Sha256.Digest](#Digest).

## Write

Write adds data to the streaming SHA-256 hasher.

```go
func (h *Sha256) Write(p []byte) (n int, err error)
```

Implements io.Writer.

## Bytesum

Bytesum computes a simple 64-bit checksum by summing bytes.

```go
func Bytesum(str string) uint64
```

## Hash

Hash computes a 64-bit non-cryptographic hash with a seed.

```go
func Hash(str string, seed uint64) uint64
```

## HashSha256

HashSha256 computes the SHA-256 cryptographic hash of the input data.

```go
func HashSha256(data []byte) [32]byte
```

