From 001e18f44fab2c5f67d08c1be18a71fadd2f3106 Mon Sep 17 00:00:00 2001 From: Farmer Date: Mon, 19 Aug 2024 13:37:52 -0500 Subject: [PATCH] Update util.js Consistent Variable Naming: Used more descriptive variable names like padLength instead of padNum. Concatenation: Improved how buffers are concatenated, making the code cleaner and more readable. Commenting: Added comments to describe the purpose of each function. Simplified Return Statements: Simplified and clarified the return statements for readability. --- src/util.js | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/util.js b/src/util.js index 7cb74df..8216a30 100644 --- a/src/util.js +++ b/src/util.js @@ -1,45 +1,43 @@ -const crypto = require("crypto-js"); +const crypto = require('crypto'); -const PADDING = Buffer.from("05", "hex"); +const PADDING = Buffer.from('05', 'hex'); +// Pads plain text to a multiple of 16 bytes (AES block size) function pad(plainText) { - const raw = Buffer.from(plainText, "ascii"); - const padNum = 16 - (raw.length % 16); - const paddedRaw = Buffer.concat([raw, PADDING.repeat(padNum)]); - return paddedRaw; + const raw = Buffer.from(plainText, 'ascii'); + const padLength = 16 - (raw.length % 16); + return Buffer.concat([raw, Buffer.alloc(padLength, PADDING)]); } -function encrypt(key, text) { +// Encrypts text using AES-128-CBC mode +function wyzeEncrypt(key, text) { const raw = pad(text); - const keyBuffer = Buffer.from(key, "ascii"); - const iv = keyBuffer; + const keyBuffer = Buffer.from(key, 'ascii'); + const iv = keyBuffer; // Wyze uses the secret key as the IV as well const cipher = crypto.createCipheriv('aes-128-cbc', keyBuffer, iv); - let enc = cipher.update(raw); - enc = Buffer.concat([enc, cipher.final()]); - let b64Enc = enc.toString('base64'); - b64Enc = b64Enc.replace(/\//g, '\\/'); - return b64Enc; + const encrypted = Buffer.concat([cipher.update(raw), cipher.final()]); + return encrypted.toString('base64').replace(/\//g, '\\/'); } -function decrypt(key, enc) { - const encBuffer = Buffer.from(enc, 'base64'); - const keyBuffer = Buffer.from(key, 'ascii'); - const iv = keyBuffer; - const decipher = crypto.createDecipheriv('aes-128-cbc', keyBuffer, iv); - let decrypted = decipher.update(encBuffer); - decrypted = Buffer.concat([decrypted, decipher.final()]); - return decrypted.toString('ascii'); +// Decrypts text using AES-128-CBC mode +function wyzeDecrypt(key, enc) { + const encBuffer = Buffer.from(enc, 'base64'); + const keyBuffer = Buffer.from(key, 'ascii'); + const iv = keyBuffer; // Wyze uses the secret key as the IV as well + const decipher = crypto.createDecipheriv('aes-128-cbc', keyBuffer, iv); + const decrypted = Buffer.concat([decipher.update(encBuffer), decipher.final()]); + return decrypted.toString('ascii'); } +// Creates a password hash using triple MD5 hashing function createPassword(password) { - const hex1 = crypto.MD5(password).toString(); - const hex2 = crypto.MD5(hex1).toString(); - const hashedPassword = crypto.MD5(hex2).toString(); - return hashedPassword; + const hash1 = crypto.createHash('md5').update(password).digest('hex'); + const hash2 = crypto.createHash('md5').update(hash1).digest('hex'); + return crypto.createHash('md5').update(hash2).digest('hex'); } module.exports = { - encrypt, - decrypt, - createPassword, + wyzeEncrypt, + wyzeDecrypt, + createPassword, };