# hasha

> Hashing made simple. Get the hash of a buffer/string/stream/file.

Latest version **7.0.0** (published 2025-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install hasha
pnpm add hasha
yarn add hasha
bun add hasha
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2025-09-12 |
| First published | 2015-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 18.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 950 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | hash, hashing, crypto, hex, base64, md5, sha1, sha256, sha512, sum, stream, file, fs, buffer, string, text, rev, revving, simple, easy |

## Links

- npm: https://www.npmjs.com/package/hasha
- Repository: https://github.com/sindresorhus/hasha
- Homepage: https://github.com/sindresorhus/hasha#readme
- Issues: https://github.com/sindresorhus/hasha/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/hasha

## Dependencies (2)

- [is-stream](https://npm.io/package/is-stream.md) ^4.0.1
- [type-fest](https://npm.io/package/type-fest.md) ^4.41.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 7.0.0 (latest) — 2025-09-12
- 6.0.0 — 2023-11-11
- 5.2.2 — 2020-10-09
- 5.2.1 — 2020-09-27
- 5.2.0 — 2020-02-16
- 5.1.0 — 2019-09-22
- 5.0.0 — 2019-04-05
- 4.0.1 — 2019-03-13
- 4.0.0 — 2019-03-12
- 3.0.0 — 2017-05-10
- 2.2.0 — 2015-12-24
- 2.1.0 — 2015-12-23
- 2.0.2 — 2015-09-23
- 2.0.1 — 2015-09-01
- 2.0.0 — 2015-09-01
- … 2 more at https://npm.io/package/hasha/versions

## README

<h1 align="center">
	<br>
	<br>
	<br>
	<img width="380" src="media/logo.svg" alt="hasha">
	<br>
	<br>
	<br>
	<br>
	<br>
</h1>

> Hashing made simple. Get the hash of a buffer/string/stream/file.

Convenience wrapper around the core [`crypto` Hash class](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm) with simpler API and better defaults.

## Install

```sh
npm install hasha
```

## Usage

```js
import {hash} from 'hasha';

await hash('unicorn');
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
```

## API

See the Node.js [`crypto` docs](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more about hashing.

### hash(input, options?)

The operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive.

Returns a hash asynchronously.

### hashSync(input, options?)

Returns a hash.

#### input

Type: `Uint8Array | string | Array<Uint8Array | string> | NodeJS.ReadableStream` *(`NodeJS.ReadableStream` is not available in `hashSync`)*

The value to hash.

While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer.

Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.

#### options

Type: `object`

##### encoding

Type: `string`\
Default: `'hex'`\
Values: `'hex' | 'base64' | 'buffer' | 'latin1'`

The encoding of the returned hash.

##### algorithm

Type: `string`\
Default: `'sha512'`\
Values: `'md5' | 'sha1' | 'sha256' | 'sha512'` *([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))*

*The `md5` algorithm is good for [file revving](https://github.com/sindresorhus/rev-hash), but you should never use `md5` or `sha1` for anything sensitive. [They're insecure.](https://security.googleblog.com/2014/09/gradually-sunsetting-sha-1.html)*

##### signal

Type: `AbortSignal`

An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to abort the hashing operation.

### hashFile(filePath, options?)

The operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive.

Returns a `Promise` for the calculated file hash.

```js
import {hashFile} from 'hasha';

// Get the MD5 hash of an image
await hashFile('unicorn.png', {algorithm: 'md5'});
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
```

### hashFileSync(filePath, options?)

Returns the calculated file hash.

```js
import {hashFileSync} from 'hasha';

// Get the MD5 hash of an image
hashFileSync('unicorn.png', {algorithm: 'md5'});
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
```

### hashingStream(options?)

Returns a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).

```js
import {hashingStream} from 'hasha';

// Hash the process input and output the hash sum
process.stdin.pipe(hashingStream()).pipe(process.stdout);
```

## Tip

For hashing multiple files, limit concurrency to `os.availableParallelism()`:

```js
import {availableParallelism} from 'node:os';
import {hashFile} from 'hasha';
import pLimit from 'p-limit';

const limit = pLimit(availableParallelism());

await Promise.all(files.map(file => limit(() => hashFile(file))));
```

## Related

- [hasha-cli](https://github.com/sindresorhus/hasha-cli) - CLI for this module
- [crypto-hash](https://github.com/sindresorhus/crypto-hash) - Tiny hashing module that uses the native crypto API in Node.js and the browser
- [hash-object](https://github.com/sindresorhus/hash-object) - Get the hash of an object
- [md5-hex](https://github.com/sindresorhus/md5-hex) - Create a MD5 hash with hex encoding

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