# Hashing and Checksums

> StringZilla for JavaScript

`hash` computes StringZilla's fast 64-bit hash of a buffer, returned as a `BigInt`.
An optional second argument seeds the hash and accepts a `BigInt` or a number, defaulting to `0`.
A seed that is not a non-negative integer within 64 bits throws a `RangeError`, here and in the `Hasher` constructor.

```javascript
sz.hash(Buffer.from("hello"));        // => 64-bit BigInt
sz.hash(Buffer.from("hello"), 42n);   // seeded
```

`byteSum` returns the arithmetic sum of all byte values in a buffer as a `BigInt`, a cheap checksum.

```javascript
assert.strictEqual(sz.byteSum(Buffer.from([1, 2, 3])), 6n);
```

The `Hasher` class computes the same fast hash incrementally over chunks.
Construct it with an optional seed, feed data with `update`, read the running result with `digest`, and rewind to the initial seed with `reset`.
Both `update` and `reset` return the hasher, so calls can be chained.

```javascript
const h = new sz.Hasher(42n);
h.update(Buffer.from("hel")).update(Buffer.from("lo"));
assert.strictEqual(h.digest(), sz.hash(Buffer.from("hello"), 42n)); // 64-bit BigInt
h.reset(); // back to the seed state
```

`sha256` computes the SHA-256 digest of a buffer in one call, returning a 32-byte `Buffer`.

```javascript
sz.sha256(Buffer.from("hello")).toString("hex");
```

The `Sha256` class streams the same digest over chunks.
Use `update` to add data, then `digest` for a 32-byte `Buffer`, `hexdigest` for the 64-character lowercase hex string, or `reset` to start over.

```javascript
const s = new sz.Sha256();
s.update(Buffer.from("hel")).update(Buffer.from("lo"));
assert.strictEqual(s.hexdigest(), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
```

`import { Hasher } from 'stringzilla';`

`import { HasherConstructor } from 'stringzilla';`

`import { Sha256 } from 'stringzilla';`

`import { Sha256Constructor } from 'stringzilla';`

## Hasher

Stateful hasher for streaming hash computation, for data that arrives in chunks.

```typescript
interface Hasher
```

## *

Hashes one more chunk.

```typescript
update(buffer: Buffer): this
```

- `buffer`: Chunk to hash

## digest

The 64-bit hash of every chunk so far.

```typescript
digest(): bigint
```

## reset

Starts over with the seed the hasher was constructed with, returning this hasher.

```typescript
reset(): this
```

## HasherConstructor

Constructs a [`Hasher`](#Hasher).

```typescript
interface HasherConstructor
```

## HasherConstructor

```typescript
new HasherConstructor(seed?: number | bigint): Hasher
```

- `seed`: Optional seed, a non-negative integer that fits in 64 bits, defaulting to 0

## Sha256

Stateful SHA-256 hasher for streaming hash computation, for data that arrives in chunks.

```typescript
interface Sha256
```

## digest

The 32-byte SHA-256 digest of every chunk so far.

```typescript
digest(): Buffer
```

## hexdigest

The SHA-256 digest of every chunk so far, as 64 lowercase hexadecimal digits.

```typescript
hexdigest(): string
```

## reset

Starts over with no chunks hashed, returning this hasher.

```typescript
reset(): this
```

## Sha256Constructor

Constructs a [`Sha256`](#Sha256).

```typescript
interface Sha256Constructor
```

## Sha256Constructor

```typescript
new Sha256Constructor(): Sha256
```

