Skip to content

Commit

Permalink
Merge pull request #181 from dajiaji/use-noble-ciphers
Browse files Browse the repository at this point in the history
Adopt noble-ciphers instead of stablelib.
  • Loading branch information
dajiaji authored Jul 15, 2023
2 parents cb3ea9b + 7e10e40 commit 399259b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 155 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ This module works on web browsers, Node.js, Deno and Cloudflare Workers.
| ChaCha20Poly1305 |\*4<br> |\*4<br> |\*4<br> |\*4<br> |\*4<br> |
| Export Only | ✅<br> | ✅<br> | ✅<br> | ✅<br> | ✅<br> |

- \*4:
[@stablelib/chacha20poly1305](https://www.stablelib.com/modules/_chacha20poly1305_chacha20poly1305_.html)
is used.
- \*4: [@noble/ciphers/chacha](https://github.com/paulmillr/noble-ciphers) is used.

## Supported Environments

Expand Down
144 changes: 7 additions & 137 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 11 additions & 15 deletions src/aeadKeys/chacha20Poly1305Key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChaCha20Poly1305 } from "npm:@stablelib/chacha20poly1305@1.0.1";
import { chacha20_poly1305 } from "npm:@noble/ciphers@0.1.4/chacha";

import type { AeadKey } from "../interfaces/aeadKey.ts";

Expand All @@ -9,10 +9,10 @@ export class Chacha20Poly1305Key implements AeadKey {
public readonly keySize: number = 32;
public readonly nonceSize: number = 12;
public readonly tagSize: number = 16;
private _key: ChaCha20Poly1305;
private _key: Uint8Array;

public constructor(key: ArrayBuffer) {
this._key = new ChaCha20Poly1305(new Uint8Array(key));
this._key = new Uint8Array(key);
}

public async seal(
Expand All @@ -37,11 +37,11 @@ export class Chacha20Poly1305Key implements AeadKey {
aad: ArrayBuffer,
): Promise<ArrayBuffer> {
return new Promise((resolve) => {
const ret = this._key.seal(
const ret = chacha20_poly1305(
this._key,
new Uint8Array(iv),
new Uint8Array(data),
new Uint8Array(aad),
);
).encrypt(new Uint8Array(data));
resolve(ret.buffer);
});
}
Expand All @@ -51,17 +51,13 @@ export class Chacha20Poly1305Key implements AeadKey {
data: ArrayBuffer,
aad: ArrayBuffer,
): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const ret = this._key.open(
return new Promise((resolve) => {
const ret = chacha20_poly1305(
this._key,
new Uint8Array(iv),
new Uint8Array(data),
new Uint8Array(aad),
);
if (ret instanceof Uint8Array) {
resolve(ret.buffer);
} else {
reject(new Error("failed to open."));
}
).decrypt(new Uint8Array(data));
resolve(ret.buffer);
});
}
}

0 comments on commit 399259b

Please sign in to comment.