# encryption-encoding

> Compact encoding for versioned encrypted values with automatic string/buffer handling.

Latest version **1.0.3** (published 2026-01-15) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install encryption-encoding
pnpm add encryption-encoding
yarn add encryption-encoding
bun add encryption-encoding
```

## Health

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

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2026-01-15 |
| First published | 2026-01-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Holepunch Inc |
| Maintainers | mafintosh, d_cassidy |
| Keywords | pear, p2p, bare, autobase, encryption, hyperschema |

## Links

- npm: https://www.npmjs.com/package/encryption-encoding
- Repository: https://github.com/holepunchto/encryption-encoding
- Issues: https://github.com/holepunchto/encryption-encoding/issues
- npm.io page: https://npm.io/package/encryption-encoding

## Dependencies (1)

- [hyperschema](https://npm.io/package/hyperschema.md) ^1.19.0

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2026-01-15
- 1.0.2 — 2026-01-15
- 1.0.1 — 2026-01-15
- 1.0.0 — 2026-01-15

## README

# encryption-encoding

Versioned encryption encoding using hyperschema.

## Installation

```
npm install encryption-encoding
```

## Usage

```js
const sodium = require('sodium-universal')
const b4a = require('b4a')
const { encrypt, decrypt } = require('encryption-encoding')

const password = b4a.alloc(32)
// ... fill password

const encrypted = await encrypt(plaintext, async (value) => {
  const buffer = b4a.allocUnsafe(
    value.byteLength + sodium.crypto_secretbox_MACBYTES + sodium.crypto_secretbox_NONCEBYTES
  )
  const nonce = buffer.subarray(0, sodium.crypto_secretbox_NONCEBYTES)
  const box = buffer.subarray(nonce.byteLength)

  sodium.randombytes_buf(nonce)
  sodium.crypto_secretbox_easy(box, value, nonce, password)

  return { value: buffer, version: 1 }
})

const decrypted = await decrypt(encrypted, async ({ value, version }) => {
  const nonce = value.subarray(0, sodium.crypto_secretbox_NONCEBYTES)
  const box = value.subarray(nonce.byteLength)
  const output = b4a.allocUnsafe(box.byteLength - sodium.crypto_secretbox_MACBYTES)

  sodium.crypto_secretbox_open_easy(output, box, nonce, password)
  return output
})
```

## API

### `await encrypt(data, callback)`

Calls `callback(data)` which should return `{ value, version }`, then encodes the result using hyperschema.

### `await decrypt(buffer, callback)`

Decodes the buffer to `{ version, value }`, passes it to `callback`, and returns the result.

## License

Apache-2.0

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