Skip to content

Commit 06a94c8

Browse files
authored
Merge pull request #7 from xaviermonin/main
Arm64 support and platform support indicator
2 parents 4e92a98 + a239ea4 commit 06a94c8

File tree

6 files changed

+955
-1204
lines changed

6 files changed

+955
-1204
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#### 2.0.0 (2025-01-01)
2+
3+
##### Features
4+
5+
* Add support for arm64 ([03707dd](https://github.com/primno/dpapi/commit/03707dd007a60f4f4abfc423f781e503edbcc92e))
6+
* Add platform support flag ([755cb93](https://github.com/primno/dpapi/commit/755cb9326c8254861859aa0219d49fa06aa421d5))
7+
8+
19
#### 1.1.1 (2023-03-25)
210

311
##### Chores

README.md

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
Native module to encrypt/decrypt data on Windows with DPAPI.
88

9-
This native module is **prebuilt** for Node.JS running on Windows. It provides the win32-x64 N-API module.
9+
This native module is **prebuilt** for Node.JS running on Windows. It provides the **x64** and the **arm64** N-API modules for Windows.
10+
11+
This package indicates if the prebuilt module is supported on the current platform.
1012

1113
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).
1214

@@ -21,7 +23,7 @@ This package is prebuilt, so you don't need to have build tools installed on the
2123

2224
## Install
2325

24-
The win32-x64 prebuilt module will be installed with the following command.
26+
The prebuilt module will be installed with the following command.
2527

2628
```bash
2729
npm install @primno/dpapi
@@ -30,28 +32,53 @@ npm install @primno/dpapi
3032
## Definition
3133

3234
```ts
33-
function protectData(
34-
userData: Uint8Array,
35-
optionalEntropy: Uint8Array | null,
36-
scope: "CurrentUser" | "LocalMachine"
37-
): Uint8Array;
38-
39-
function unprotectData(
40-
encryptedData: Uint8Array,
41-
optionalEntropy: Uint8Array | null,
42-
scope: "CurrentUser" | "LocalMachine"
43-
): Uint8Array;
35+
class Dpapi {
36+
public protectData(
37+
userData: Uint8Array,
38+
optionalEntropy: Uint8Array | null,
39+
scope: "CurrentUser" | "LocalMachine"
40+
): Uint8Array;
41+
42+
public unprotectData(
43+
encryptedData: Uint8Array,
44+
optionalEntropy: Uint8Array | null,
45+
scope: "CurrentUser" | "LocalMachine"
46+
): Uint8Array;
47+
}
48+
49+
const isPlatformSupported: boolean;
4450
```
4551

4652
## Usage
4753

54+
### ECMAScript Module
4855
```ts
49-
import { Dpapi } from "@primno/dpapi";
56+
import { Dpapi, isPlatformSupported } from "@primno/dpapi";
57+
58+
if (isPlatformSupported) {
59+
const buffer = Buffer.from("Hello world", "utf-8");
60+
61+
const encrypted = Dpapi.protectData(buffer, null, "CurrentUser");
62+
const decrypted = Dpapi.unprotectData(encrypted, null, "CurrentUser");
63+
}
64+
else {
65+
console.error("Platform not supported. Only Windows is supported (x64, ARM64)");
66+
}
67+
```
68+
69+
### CommonJS
70+
```js
71+
const { Dpapi, isPlatformSupported } = require("@primno/dpapi");
5072

51-
const buffer = Buffer.from("Hello world", "utf-8");
73+
if (isPlatformSupported) {
74+
const buffer = Buffer.from("Hello world", "utf-8");
5275

53-
const encrypted = Dpapi.protectData(buffer, null, "CurrentUser");
54-
const decrypted = Dpapi.unprotectData(encrypted, null, "CurrentUser");
76+
const encrypted = Dpapi.protectData(buffer, null, "CurrentUser");
77+
const decrypted = Dpapi.unprotectData(encrypted, null, "CurrentUser");
78+
}
79+
else {
80+
console.error("Platform not supported. Only Windows is supported (x64, ARM64)");
81+
}
5582
```
5683

5784
## Credits

lib/index.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,39 @@ export interface DpapiBindings {
77

88
export type DataProtectionScope = "CurrentUser" | "LocalMachine";
99

10-
export var Dpapi: DpapiBindings = require("node-gyp-build")(path.join(__dirname, ".."));
10+
class ErrorWithInnerError extends Error {
11+
public innerError: Error;
12+
13+
constructor(message: string, innerError: Error) {
14+
super(message);
15+
this.innerError = innerError;
16+
}
17+
}
18+
19+
class UnsupportedPlatformDpapiBindings implements DpapiBindings {
20+
private error: Error;
21+
22+
constructor(error: Error) {
23+
this.error = error;
24+
}
25+
26+
protectData(dataToEncrypt: Uint8Array, optionalEntropy: Uint8Array | null, scope: DataProtectionScope): Uint8Array {
27+
throw new ErrorWithInnerError("DPAPI is not supported on this platform.", this.error);
28+
}
29+
unprotectData(encryptData: Uint8Array, optionalEntropy: Uint8Array | null, scope: DataProtectionScope): Uint8Array {
30+
throw new ErrorWithInnerError("DPAPI is not supported on this platform.", this.error);
31+
}
32+
}
33+
34+
let dpapi: DpapiBindings;
35+
36+
try {
37+
dpapi = require("node-gyp-build")(path.join(__dirname, ".."));
38+
}
39+
catch (e: any) {
40+
dpapi = new UnsupportedPlatformDpapiBindings(e);
41+
}
42+
43+
export var isPlatformSupported : boolean = !(dpapi instanceof UnsupportedPlatformDpapiBindings);
44+
export var Dpapi: DpapiBindings = dpapi;
1145
export default Dpapi;

0 commit comments

Comments
 (0)