-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
27 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |