Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrzakacper committed Sep 2, 2024
1 parent 421d229 commit 73ac1ce
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions reporters/javascript/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
let cachedNodeCrypto: Crypto | undefined;

const loadCrypto = async () => {
if (globalThis?.crypto) return globalThis.crypto;
try {
return (cachedNodeCrypto ||= (await import("node:crypto")).webcrypto as Crypto);
} catch (error) {
throw new Error("Failed to load crypto");
}
};

/**
* Encrypts plaintext using AES-GCM with supplied password, for decryption with aes_gcm_decrypt()
*
Expand All @@ -6,6 +17,7 @@
* @returns {Promise<string>} encrypted cipher text
*/
export async function encrypt(plaintext: string, password: string): Promise<string> {
const crypto = await loadCrypto();
// encode password as UTF-8
const pwUtf8 = new TextEncoder().encode(password);
// hash the password
Expand Down Expand Up @@ -68,6 +80,8 @@ export async function decrypt(ciphertext: string, password: string): Promise<str
}

export async function sha256(message: string): Promise<string> {
const crypto = await loadCrypto();

// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);

Expand Down

0 comments on commit 73ac1ce

Please sign in to comment.