# @primno/dpapi

> Pre-built version of DPAPI (Data Protection API of Windows). Encrypt and decrypt data.

Latest version **2.0.1** (published 2025-01-12) · MIT license · 7.2K weekly downloads

## Install

```sh
npm install @primno/dpapi
pnpm add @primno/dpapi
yarn add @primno/dpapi
bun add @primno/dpapi
```

## Health

**Score 45/100 (D)** — status: maintenance-mode.

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

Warnings: no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-01-12 |
| First published | 2022-11-24 |
| Weekly downloads | 7.2K |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 1 |
| Unpacked size | 271.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 11 |
| Author | Xavier Monin |
| Maintainers | xaviermonin |
| Keywords | Dpapi, Prebuilt, Encrypt, Crypt |

## Links

- npm: https://www.npmjs.com/package/@primno/dpapi
- Repository: https://github.com/primno/dpapi
- Homepage: https://github.com/primno/dpapi#readme
- Issues: https://github.com/primno/dpapi/issues
- npm.io page: https://npm.io/package/@primno/dpapi

## Dependencies (1)

- [node-gyp-build](https://npm.io/package/node-gyp-build.md) ^4.8.4

## 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
- [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
- [win-dpapi](https://npm.io/package/win-dpapi.md) — 872 weekly downloads

## Recent versions

- 2.0.1 (latest) — 2025-01-12
- 2.0.0 — 2025-01-01
- 1.1.2 — 2023-10-07
- 1.1.1 — 2023-03-25
- 1.1.0 — 2023-03-16
- 1.0.0 — 2023-01-16
- 1.0.0-alpha.0 — 2022-11-24

## README

# DPAPI
[![npm](https://img.shields.io/npm/v/@primno/dpapi.svg)](https://www.npmjs.com/package/@primno/dpapi)
[![npm](https://img.shields.io/npm/l/@primno/dpapi.svg)](https://github.com/primno/dpapi/blob/main/LICENSE)
![build](https://img.shields.io/github/actions/workflow/status/primno/dpapi/test.yml)
[![coverage](https://codecov.io/gh/primno/dpapi/branch/main/graph/badge.svg?token=J4AVWIOR9F)](https://codecov.io/gh/primno/dpapi)

Native module to encrypt/decrypt data on Windows with DPAPI.

This native module is **prebuilt** for Node.JS running on Windows. It provides the **x64** and the **arm64** N-API modules for Windows.

This package indicates if the prebuilt module is supported on the current platform.

Based on the port to N-API made by [Microsoft](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/extensions/msal-node-extensions/src/dpapi-addon) in @msal-node-extension from the work of [Brad Hughes](https://github.com/bradhugh/node-dpapi).

> This package is part of the [Primno](https://primno.io) framework.

## Why this package?

Other similar packages require to build the native module on the target machine during the installation.
This means that you must have Python and Visual C++ installed, production environment included.

This package is prebuilt, so you don't need to have build tools installed on the target machine.

## Install

The prebuilt module will be installed with the following command.

```bash
npm install @primno/dpapi
```

## Definition

```ts
class Dpapi {
    public protectData(
        userData: Uint8Array,
        optionalEntropy: Uint8Array | null,
        scope: "CurrentUser" | "LocalMachine"
    ): Uint8Array;

    public unprotectData(
        encryptedData: Uint8Array,
        optionalEntropy: Uint8Array | null,
        scope: "CurrentUser" | "LocalMachine"
    ): Uint8Array;
}

const isPlatformSupported: boolean;
```

## Usage

### ECMAScript Module
```ts
import { Dpapi, isPlatformSupported } from "@primno/dpapi";

if (isPlatformSupported) {
    const buffer = Buffer.from("Hello world", "utf-8");

    const encrypted = Dpapi.protectData(buffer, null, "CurrentUser");
    const decrypted = Dpapi.unprotectData(encrypted, null, "CurrentUser");
}
else {
    console.error("Platform not supported. Only Windows is supported (x64, ARM64)");
}
```

### CommonJS
```js
const { Dpapi, isPlatformSupported } = require("@primno/dpapi");

if (isPlatformSupported) {
    const buffer = Buffer.from("Hello world", "utf-8");

    const encrypted = Dpapi.protectData(buffer, null, "CurrentUser");
    const decrypted = Dpapi.unprotectData(encrypted, null, "CurrentUser");
}
else {
    console.error("Platform not supported. Only Windows is supported (x64, ARM64)");
}
```

## Credits

- Brad Hughes for the original code.
- Microsoft for the N-API port in MSAL-Node-Extension.

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