Skip to content

Commit

Permalink
Update util.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarmer08 committed Sep 12, 2024
1 parent 279d3af commit c895ef6
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
const crypto = require('crypto');

const PADDING = Buffer.from('05', 'hex');
// Constants
const PADDING = Buffer.from("05", "hex");
const BLOCK_SIZE = 16;

// Pads plain text to a multiple of 16 bytes (AES block size)
// Function to pad the plaintext to be multiples of 8-byte blocks
function pad(plainText) {
const raw = Buffer.from(plainText, 'ascii');
const padLength = 16 - (raw.length % 16);
return Buffer.concat([raw, Buffer.alloc(padLength, PADDING)]);
console.log('Padding plaintext...');

let raw = Buffer.from(plainText, 'ascii');
const padNum = BLOCK_SIZE - (raw.length % BLOCK_SIZE);
const padBuffer = Buffer.alloc(padNum, PADDING);

raw = Buffer.concat([raw, padBuffer]);

console.log(`Padded plaintext: ${raw.toString('hex')}`);
return raw;
}

// Encrypts text using AES-128-CBC mode
function wyzeEncrypt(key, text) {
console.log('Encrypting text...');

const raw = pad(text);
const keyBuffer = Buffer.from(key, 'ascii');
const iv = keyBuffer; // Wyze uses the secret key as the IV as well
const iv = keyBuffer; // Wyze uses the secret key for the IV as well
const cipher = crypto.createCipheriv('aes-128-cbc', keyBuffer, iv);
const encrypted = Buffer.concat([cipher.update(raw), cipher.final()]);
return encrypted.toString('base64').replace(/\//g, '\\/');
let enc = cipher.update(raw);
enc = Buffer.concat([enc, cipher.final()]);

let b64Enc = enc.toString('base64');
b64Enc = b64Enc.replace(/\//g, '\\/');

console.log(`Encrypted text: ${b64Enc}`);
return b64Enc;
}

// Decrypts text using AES-128-CBC mode
function wyzeDecrypt(key, enc) {
console.log('Decrypting text...');

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 iv = keyBuffer;

const decipher = crypto.createDecipheriv('aes-128-cbc', keyBuffer, iv);
const decrypted = Buffer.concat([decipher.update(encBuffer), decipher.final()]);
return decrypted.toString('ascii');
let decrypt = decipher.update(encBuffer);
decrypt = Buffer.concat([decrypt, decipher.final()]);

const decryptTxt = decrypt.toString('ascii').replace(/\x05/g, '');

console.log(`Decrypted text: ${decryptTxt}`);
return decryptTxt;
}

// Creates a password hash using triple MD5 hashing
function createPassword(password) {
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');
console.log('Creating password hash...');

const hex1 = crypto.createHash('md5').update(password).digest('hex');
const hex2 = crypto.createHash('md5').update(hex1).digest('hex');
const finalHash = crypto.createHash('md5').update(hex2).digest('hex');

console.log(`Created password hash: ${finalHash}`);
return finalHash;
}

module.exports = {
Expand Down

0 comments on commit c895ef6

Please sign in to comment.