Skip to content

Commit

Permalink
Use crypto global instead of importing node:crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
longnguyen2004 committed Feb 6, 2025
1 parent 3c9ac09 commit 4c610f3
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/client/encryptor/TransportEncryptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sp from "sodium-plus";
import { webcrypto } from "node:crypto";
import { max_int32bit } from "../../utils.js";
const { SodiumPlus } = sp;

Expand All @@ -10,10 +9,10 @@ export interface TransportEncryptor {
export class AES256TransportEncryptor implements TransportEncryptor
{
private _nonce = 0;
private _secretKey: Promise<webcrypto.CryptoKey>;
private _secretKey: Promise<CryptoKey>;
constructor(secretKey: Buffer)
{
this._secretKey = webcrypto.subtle.importKey("raw",
this._secretKey = crypto.subtle.importKey("raw",
secretKey,
{
name: "AES-GCM",
Expand All @@ -27,7 +26,7 @@ export class AES256TransportEncryptor implements TransportEncryptor
nonceBuffer.writeUInt32BE(this._nonce);
this._nonce = (this._nonce + 1) % max_int32bit;

const ciphertext = Buffer.from(await webcrypto.subtle.encrypt({
const ciphertext = Buffer.from(await crypto.subtle.encrypt({
name: "AES-GCM",
iv: nonceBuffer,
additionalData,
Expand Down

0 comments on commit 4c610f3

Please sign in to comment.