Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaced built-in crypto library with @web5/crypto #816

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: refactored code to pass encryption tests
  • Loading branch information
Toheeb-Ojuolape committed Oct 11, 2024
commit 5b705b5893d744ce06e6cd037cd7cc0a39744186
64 changes: 39 additions & 25 deletions src/utils/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AesCtr } from '@web5/crypto';
import type { Jwk } from '@web5/crypto';
import { Readable } from 'readable-stream';

// compress publicKey for message encryption
// Compress publicKey for message encryption
eciesjs.ECIES_CONFIG.isEphemeralKeyCompressed = true;

/**
Expand Down Expand Up @@ -35,24 +35,31 @@ export class Encryption {
read(): void {},
});

let buffer = Buffer.alloc(0);

plaintextStream.on('data', async (chunk) => {
// Encrypt the chunk using AesCtr
const encryptedChunk = await AesCtr.encrypt({
data : chunk,
key : jwkKey,
counter : initializationVector,
length : 256,
});

cipherStream.push(encryptedChunk);
buffer = Buffer.concat([buffer, chunk]);
});

plaintextStream.on('end', () => {
cipherStream.push(null); // Signal the end of the stream
plaintextStream.on('end', async () => {
try {
// Encrypt the entire buffer when the stream ends
const encryptedData = await AesCtr.encrypt({
data : buffer,
key : jwkKey,
counter : initializationVector,
length : 128, // FIX: Counter length must be between 1 and 128
Toheeb-Ojuolape marked this conversation as resolved.
Show resolved Hide resolved
});

cipherStream.push(encryptedData);
cipherStream.push(null); // Signal the end of the stream
} catch (error) {
cipherStream.emit('error', error); // Emit error if encryption fails
Toheeb-Ojuolape marked this conversation as resolved.
Show resolved Hide resolved
}
});

plaintextStream.on('error', (err) => {
cipherStream.emit('error', err); // Emit error if any occurs in the plaintext stream
cipherStream.emit('error', err); // Propagate errors
});

return cipherStream; // Return the cipher stream
Expand All @@ -73,24 +80,31 @@ export class Encryption {
read(): void {},
});

let buffer = Buffer.alloc(0);

cipherStream.on('data', async (chunk) => {
// Decrypt the chunk using AesCtr
const decryptedChunk = await AesCtr.decrypt({
data : chunk,
key : jwkKey,
counter : initializationVector,
length : 256, // Length of the key in bits
});

plaintextStream.push(decryptedChunk); // Push the decrypted chunk to the plaintext stream
buffer = Buffer.concat([buffer, chunk]);
});

cipherStream.on('end', () => {
plaintextStream.push(null); // Signal the end of the stream
cipherStream.on('end', async () => {
try {
// Decrypt the entire buffer when the stream ends
const decryptedData = await AesCtr.decrypt({
data : buffer,
key : jwkKey,
counter : initializationVector,
length : 128, // FIX: Counter length must be between 1 and 128
});

plaintextStream.push(decryptedData);
plaintextStream.push(null); // Signal the end of the stream
} catch (error) {
plaintextStream.emit('error', error); // Emit error if decryption fails
Toheeb-Ojuolape marked this conversation as resolved.
Show resolved Hide resolved
}
});

cipherStream.on('error', (err) => {
plaintextStream.emit('error', err); // Emit error if any occurs in the cipher stream
plaintextStream.emit('error', err); // Propagate errors
});

return plaintextStream; // Return the plaintext stream
Expand Down
Loading