mirror of
https://gitlab.com/vyla-entertainment/home.git
synced 2026-11-14 14:17:25 -05:00
huge rewrite
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
const crypto = require('crypto');
|
||||
const os = require('os');
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
class CryptoUtils {
|
||||
static getMachineKey(cacheDir) {
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const cachePath = cacheDir ? path.join(cacheDir, '.machine-key-cache') : null;
|
||||
|
||||
if (cachePath && fs.existsSync(cachePath)) {
|
||||
try {
|
||||
const cached = fs.readFileSync(cachePath, 'utf8').trim();
|
||||
if (cached) return cached;
|
||||
if (cached && cached.length === 64) return cached;
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
@@ -22,8 +21,7 @@ class CryptoUtils {
|
||||
os.hostname(),
|
||||
os.platform(),
|
||||
os.arch(),
|
||||
os.cpus()[0].model,
|
||||
this.getMachineId(),
|
||||
os.cpus()[0] ? os.cpus()[0].model : '',
|
||||
process.env.COMPUTERNAME || '',
|
||||
process.env.USERNAME || ''
|
||||
].join('|');
|
||||
@@ -46,36 +44,6 @@ class CryptoUtils {
|
||||
return key;
|
||||
}
|
||||
|
||||
static getMachineId() {
|
||||
const fs = require('fs');
|
||||
if (os.platform() === 'win32') {
|
||||
try {
|
||||
const { spawnSync } = require('child_process');
|
||||
const res = spawnSync('reg', ['query', 'HKLM\\SOFTWARE\\Microsoft\\Cryptography', '/v', 'MachineGuid'], { encoding: 'utf8' });
|
||||
if (res.stdout) {
|
||||
const match = res.stdout.match(/MachineGuid\s+REG_SZ\s+([^\r\n]+)/);
|
||||
if (match) return match[1].trim();
|
||||
}
|
||||
} catch (e) { }
|
||||
return 'windows-node-' + os.hostname();
|
||||
} else if (os.platform() === 'darwin') {
|
||||
try {
|
||||
const { spawnSync } = require('child_process');
|
||||
const res = spawnSync('ioreg', ['-rd1', '-c', 'IOPlatformExpertDevice'], { encoding: 'utf8' });
|
||||
const match = res.stdout.match(/"IOPlatformUUID"\s*=\s*"([^"]+)"/);
|
||||
if (match) return match[1].trim();
|
||||
} catch (e) { }
|
||||
return 'darwin-node-' + os.hostname();
|
||||
} else {
|
||||
for (const idPath of ['/etc/machine-id', '/var/lib/dbus/machine-id']) {
|
||||
try {
|
||||
if (fs.existsSync(idPath)) return fs.readFileSync(idPath, 'utf8').trim();
|
||||
} catch (e) { }
|
||||
}
|
||||
return 'linux-node-' + os.hostname();
|
||||
}
|
||||
}
|
||||
|
||||
static deriveKey(machineKey, salt = 'vyla-home-salt') {
|
||||
const hash = crypto.createHash('sha256');
|
||||
hash.update(machineKey + salt);
|
||||
@@ -98,11 +66,11 @@ class CryptoUtils {
|
||||
}
|
||||
}
|
||||
|
||||
static decrypt(encryptedData, key) {
|
||||
static decrypt(ciphertext, key) {
|
||||
try {
|
||||
const parts = encryptedData.split(':');
|
||||
const parts = ciphertext.split(':');
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('Invalid encrypted data format');
|
||||
throw new Error('Invalid ciphertext format');
|
||||
}
|
||||
|
||||
const iv = Buffer.from(parts[0], 'hex');
|
||||
@@ -122,25 +90,14 @@ class CryptoUtils {
|
||||
}
|
||||
|
||||
static encryptObject(obj, key) {
|
||||
const json = JSON.stringify(obj);
|
||||
return this.encrypt(json, key);
|
||||
const jsonString = JSON.stringify(obj);
|
||||
return this.encrypt(jsonString, key);
|
||||
}
|
||||
|
||||
static decryptObject(encryptedData, key) {
|
||||
const json = this.decrypt(encryptedData, key);
|
||||
return JSON.parse(json);
|
||||
}
|
||||
|
||||
static hashData(data) {
|
||||
const hash = crypto.createHash('sha256');
|
||||
hash.update(data);
|
||||
return hash.digest('hex');
|
||||
}
|
||||
|
||||
static verifyHash(data, expectedHash) {
|
||||
const actualHash = this.hashData(data);
|
||||
return actualHash === expectedHash;
|
||||
static decryptObject(ciphertext, key) {
|
||||
const jsonString = this.decrypt(ciphertext, key);
|
||||
return JSON.parse(jsonString);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CryptoUtils;
|
||||
module.exports = CryptoUtils;
|
||||
|
||||
Reference in New Issue
Block a user