# split-hash

> Split the stream based on bytes and get digests from each part.

Latest version **0.3.4** (published 2026-02-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install split-hash
pnpm add split-hash
yarn add split-hash
bun add split-hash
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.3.4 |
| Published | 2026-02-20 |
| First published | 2020-09-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=16.5 |
| Dependencies | 3 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | BlackGlory |
| Maintainers | black_glory |
| Keywords | split, hash, stream, block, chunk |

## Links

- npm: https://www.npmjs.com/package/split-hash
- Repository: https://github.com/BlackGlory/split-hash
- Homepage: https://github.com/BlackGlory/split-hash#readme
- Issues: https://github.com/BlackGlory/split-hash/issues
- npm.io page: https://npm.io/package/split-hash

## Dependencies (3)

- [extra-utils](https://npm.io/package/extra-utils.md) ^5.21.0
- [extra-stream](https://npm.io/package/extra-stream.md) ^0.2.0
- [@blackglory/errors](https://npm.io/package/@blackglory/errors.md) ^3.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.3.4 (latest) — 2026-02-20
- 0.3.3 — 2026-02-20
- 0.3.2 — 2023-06-11
- 0.3.1 — 2023-03-27
- 0.3.0 — 2023-03-24
- 0.2.2 — 2023-01-31
- 0.2.1 — 2022-11-02
- 0.2.0 — 2022-11-01
- 0.1.7 — 2022-05-15
- 0.1.6 — 2022-01-06
- 0.1.5 — 2021-10-14
- 0.1.4 — 2021-02-04
- 0.1.3 — 2021-02-03
- 0.1.2 — 2020-10-10
- 0.1.1 — 2020-09-02
- … 1 more at https://npm.io/package/split-hash/versions

## README

# split-hash
Split the stream based on bytes and get digests from each part.

## Install
```sh
npm install --save split-hash
# or
yarn add split-hash
```

## Usage
### Hash
```js
import { splitHash } from 'split-hash/nodejs'
import * as crypto from 'crypto'

const KiB = 1024

const createHash = () => {
  const hash = crypto.createHash('sha256')
  return {
    update(buffer) {
      hash.update(buffer)
    }
  , digest() {
      return hash.digest('hex')
    }
  }
}

const stream = fs.createReadStream('filename.bin')
const iter = splitHash(stream, 512 * KiB, createHash)

for await (const hash of iter) {
  console.log(hash)
}
```

### Validate
```js
import { SplitHashValidator } from 'split-hash/nodejs'
import * as crypto from 'crypto'
import { pipeline } from 'stream'

const KiB = 1024

const createHash = () => {
  const hash = crypto.createHash('sha256')
  return {
    update(buffer) {
      hash.update(buffer)
    }
  , digest() {
      return hash.digest('hex')
    }
  }
}

const hashList = [/* ... */]
const validator = new SplitHashValidator(hashList, 512 * KiB, createHash)

const stream = pipeline(
  fs.createReadStream('filename.bin')
, validator
, err => {
    // ...
  }
)
```

## API
### Node.js
```ts
type ProgressiveHashFactory<T> = () => ProgressiveHash<T>

interface IProgressiveHash<T> {
  update(buffer: Buffer): void
  digest(): T
}
```

#### splitHash
```ts
function splitHash<T>(
  stream: NodeJS.ReadableStream
, blockSizeBytes: number
, createHash: ProgressiveHashFactory<T>
): AsyncIterableIterator<T>
```

It throws `StreamEncodingError` when the `stream` encoding is set.

#### SplitHashValidator
```ts
class SplitHashValidator<T> extends Stream.Transform {
  constructor(
    digests: T[]
  , blockSizeBytes: number
  , createHash: ProgressiveHashFactory<T>
  , equals: (a: T, b: T) => boolean = Object.is
  )
}
```

It throws `NotMatchedError` when the `stream` does not match digests.

#### StreamEncodingError
```ts
class StreamEncodingError extends Error
```

#### NotMatchedError
```ts
class NotMatchedError extends Error
```

### WHATWG
```ts
type ProgressiveHashFactory<T> = () => IProgressiveHash<T>

interface IProgressiveHash<T> {
  update(buffer: Uint8Array): void
  digest(): Promise<T>
}
```

#### splitHash
```ts
async function* splitHash<T>(
  stream: ReadableStream
, blockSize: number
, createHash: ProgressiveHashFactory<T>
): AsyncIterableIterator<T>
```

---
_Source: https://npm.io/package/split-hash · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
