Skip to content

Commit 0627f86

Browse files
authored
Use node:crypto aes on deno (#5)
1 parent 402f970 commit 0627f86

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 0.2.3
4+
5+
- Use `node:crypto`'s aes implementation on deno
6+
37
## 0.2.2
48

59
- Add `chacha20-poly1305` support

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
[![License](https://img.shields.io/github/license/ecies/js-ciphers.svg)](https://github.com/ecies/js-ciphers)
44
[![NPM Package](https://img.shields.io/npm/v/@ecies/ciphers.svg)](https://www.npmjs.com/package/@ecies/ciphers)
5-
![NPM Downloads](https://img.shields.io/npm/dm/@ecies/ciphers)
5+
[![NPM Downloads](https://img.shields.io/npm/dm/@ecies/ciphers)](https://npm-stat.link/@ecies/ciphers)
66
[![Install size](https://packagephobia.com/badge?p=@ecies/ciphers)](https://packagephobia.com/result?p=@ecies/ciphers)
77
[![CI](https://img.shields.io/github/actions/workflow/status/ecies/js-ciphers/ci.yml)](https://github.com/ecies/js-ciphers/actions)
88
[![Codecov](https://img.shields.io/codecov/c/github/ecies/js-ciphers.svg)](https://codecov.io/gh/ecies/js-ciphers)
99

1010
Node/Pure JavaScript symmetric ciphers adapter.
1111

12-
On browsers (or React Native, deno), it'll use [`@noble/ciphers`](https://github.com/paulmillr/noble-ciphers)'s implementation for compatibility.
12+
If native implementations are available on some platforms (e.g. node, deno, bun), it'll use [`node:crypto`](https://nodejs.org/api/crypto.html#cryptocreatecipherivalgorithm-key-iv-options) for efficiency.
1313

14-
On node (or bun), it'll use [`node:crypto`](https://nodejs.org/api/crypto.html#cryptocreatecipherivalgorithm-key-iv-options)'s implementation for efficiency.
14+
Otherwise (e.g. browser, react native), it'll use [`@noble/ciphers`](https://github.com/paulmillr/noble-ciphers) for compatibility.
15+
16+
| | aes | chacha |
17+
| ------------ | ---------------- | ---------------- |
18+
| Node | `node:crypto`| `node:crypto`|
19+
| Bun | `node:crypto`| `@noble/ciphers` |
20+
| Deno | `node:crypto`| `@noble/ciphers` |
21+
| Browser | `@noble/ciphers` | `@noble/ciphers` |
22+
| React Native | `@noble/ciphers` | `@noble/ciphers` |
1523

1624
> [!NOTE]
1725
> You may need to polyfill [`crypto.getRandomValues`](https://github.com/LinusU/react-native-get-random-values) for React Native.
@@ -58,5 +66,5 @@ If key is fixed and nonce is less than 16 bytes, avoid randomly generated nonce.
5866
## Known limitations
5967

6068
- `xchacha20-poly1305` is implemented with pure JS [`hchacha20`](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha#section-2.2) function and `node:crypto`'s `chacha20-poly1305` on node.
61-
- Currently (Nov 2024), `node:crypto`'s `chacha20-poly1305` is not supported on deno and [bun](https://github.com/oven-sh/bun/issues/8072), `@noble/ciphers`'s implementation is used on both platforms instead.
62-
- `deno` does not support **indirect** conditional exports. If you use this library to build another library, client code of your library probably falls back to the `node:crypto` implementation and may not work properly, specifically `aes-256-gcm` (16 bytes nonce) and `chacha20-poly1305`.
69+
- Currently (Mar 2025), `node:crypto`'s `chacha20-poly1305` is not supported on deno and [bun](https://github.com/oven-sh/bun/issues/8072), `@noble/ciphers`'s implementation is used on both platforms instead.
70+
- Some old versions of `deno` [do not support](https://github.com/denoland/deno/discussions/17964#discussioncomment-10917259) **indirect** conditional exports. If you use this library to build another library, client code of your library may fall back to the `node:crypto` implementation and not work properly, specifically `aes-256-gcm` (16 bytes nonce) and `chacha20-poly1305`. If you found such a problem, try upgrade deno to the latest version.

example/main.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ const ciphers = [
3838
for (const { keyLength, nonceLength, callback, aad } of ciphers) {
3939
const key = randomBytes(keyLength);
4040
const nonce = randomBytes(nonceLength);
41+
console.log(`${callback.name} (nonce length ${nonce.length}) decrypted:`);
4142
const cipher = callback(key, nonce, aad);
42-
console.log(
43-
`${callback.name} (nonce length ${nonce.length}) decrypted:`,
44-
decoder.decode(cipher.decrypt(cipher.encrypt(msg)))
45-
);
43+
console.log(decoder.decode(cipher.decrypt(cipher.encrypt(msg))));
4644
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"types": "./dist/aes/node.d.ts",
3838
"browser": "./dist/aes/noble.js",
3939
"react-native": "./dist/aes/noble.js",
40-
"deno": "./dist/aes/noble.js",
40+
"deno": "./dist/aes/node.js",
4141
"bun": "./dist/aes/node.js",
4242
"default": "./dist/aes/node.js"
4343
},

src/_node/compat.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { CipherGCM, createCipheriv, createDecipheriv, DecipherGCM } from "node:c
33

44
const AEAD_TAG_LENGTH = 16;
55

6+
// @ts-ignore: only necessary for deno
7+
const IS_DENO = globalThis.Deno !== undefined;
8+
69
/**
710
* make `node:crypto`'s ciphers compatible with `@noble/ciphers`.
811
*
@@ -44,6 +47,12 @@ export const _compat = (
4447
}
4548
(decipher as DecipherGCM).setAuthTag(tag);
4649
}
50+
51+
/* v8 ignore next 3 */
52+
if (!isAEAD && IS_DENO) {
53+
decipher.setAutoPadding(false); // See: https://github.com/denoland/deno/issues/28381
54+
}
55+
4756
const updated = decipher.update(rawCipherText);
4857
const finalized = decipher.final();
4958
return concatBytes(updated, finalized);

0 commit comments

Comments
 (0)