Skip to content

Commit

Permalink
Make sure there is enought initialization vector (iv).
Browse files Browse the repository at this point in the history
  • Loading branch information
mrluanma committed Mar 16, 2024
1 parent 0c7aa6b commit be7b844
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class Encryptor {
decrypt(buf) {
if (!this.decipher) {
const decipher_iv_len = this.get_cipher_len(this.method)[1];
if (buf.length < decipher_iv_len) throw new Error('insufficient iv');
const decipher_iv = buf.subarray(0, decipher_iv_len);
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
return this.decipher.update(buf.subarray(decipher_iv_len));
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ wsserver.on('connection', async (ws) => {
const readable = conn.pipe(
createTransform(encryptor.decrypt.bind(encryptor)),
);
readable.on('error', (e) => console.error(`server: ${e}`));

let data = await readable.read();
while (!data) {
Expand Down
6 changes: 5 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function memoize(func) {
export function createTransform(withFn) {
return new Transform({
transform(chunk, encoding, callback) {
callback(null, withFn(chunk));
try {
callback(null, withFn(chunk));
} catch (err) {
callback(err);
}
},
});
}

0 comments on commit be7b844

Please sign in to comment.