Created
September 30, 2021 17:26
-
-
Save roggeo/bd474c93e1902d7ec97dd194bace6aff to your computer and use it in GitHub Desktop.
Security_Generate
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
"use strict"; | |
var __importDefault = (this && this.__importDefault) || function (mod) { | |
return (mod && mod.__esModule) ? mod : { "default": mod }; | |
}; | |
Object.defineProperty(exports, "__esModule", { value: true }); | |
const fs_1 = __importDefault(require("fs")); | |
const salt = 'iQhxsIZW5Ijoc3Nj3NWHB0'; | |
const emptyFile = 'security.json'; | |
const encryptedFile = 'security.bin'; | |
class SecurityUtil { | |
static async openWritableFile() { | |
try { | |
if (fs_1.default.existsSync(emptyFile)) { | |
throw new Error(`That file ${emptyFile} already exists`); | |
} | |
if (fs_1.default.existsSync(encryptedFile)) { | |
const contentBinFile = await SecurityUtil.dataBinFile(); | |
fs_1.default.writeFileSync(emptyFile, contentBinFile, { encoding: 'binary' }); | |
return; | |
} | |
fs_1.default.writeFileSync(emptyFile, `{ | |
"somePassward1": "", | |
"somePassward2": "", | |
"somePassward3": "" | |
}`, { encoding: 'utf8' }); | |
} | |
catch (err) { | |
throw err; | |
} | |
} | |
static async encryptBinFile() { | |
try { | |
if (!fs_1.default.existsSync(emptyFile)) { | |
throw new Error(`File ${emptyFile} not found`); | |
} | |
const fileContent = fs_1.default.readFileSync(emptyFile, { encoding: 'utf8' }); | |
const message = JSON.stringify(fileContent); | |
const buff = Buffer.from(message, 'utf8'); | |
const messageToEncrypt = buff.toString('base64'); | |
this.destroyWritableFile(); | |
fs_1.default.writeFileSync(encryptedFile, salt + messageToEncrypt, { encoding: 'utf8' }); | |
} | |
catch (err) { | |
throw err; | |
} | |
} | |
static dataBinFile(asJson = true) { | |
try { | |
if (!fs_1.default.existsSync(encryptedFile)) { | |
throw new Error(`File ${encryptedFile} not found`); | |
} | |
const fileContent = fs_1.default.readFileSync(encryptedFile, { encoding: 'utf8' }); | |
const contentBase64 = fileContent.replace(new RegExp(`^${salt}`, 'g'), ''); | |
const contentDecoded = Buffer.from(contentBase64, 'base64').toString(); | |
if (asJson) { | |
return JSON.parse(contentDecoded); | |
} | |
return contentDecoded; | |
} | |
catch (err) { | |
throw err; | |
} | |
} | |
static async destroyWritableFile() { | |
fs_1.default.unlink(emptyFile, function (err) { | |
if (err) { | |
console.log('...ERROR: It could not to remove json file ' + emptyFile + '. ' + err.message); | |
} | |
}); | |
} | |
} | |
exports.default = SecurityUtil; | |
const args = process.argv; | |
if (args.length >= 3) { | |
switch (args[2]) { | |
case '--make': | |
SecurityUtil.openWritableFile().then(data => { | |
console.log(`...File generated: ${emptyFile}`); | |
process.exit(1); | |
}).catch(err => { | |
console.log(`...ERROR: File "${emptyFile}" not generated. ${err.message}`); | |
}); | |
break; | |
case '--encrypt': | |
SecurityUtil.encryptBinFile().then(data => { | |
console.log(`...File removed: ${emptyFile}`); | |
console.log(`...File generated: ${encryptedFile}`); | |
process.exit(1); | |
}).catch(err => { | |
console.log(`...ERROR: File "${encryptedFile}" not generated. ${err.message}`); | |
}); | |
break; | |
case '--show': | |
try { | |
const data = SecurityUtil.dataBinFile(); | |
console.log('\n'); | |
console.log(''.padStart(70, '=')); | |
console.log(data); | |
console.log(''.padStart(70, '=')); | |
console.log('\n'); | |
process.exit(1); | |
} | |
catch (err) { | |
console.log(`...ERROR: File "${encryptedFile}" could not be opening. ${err.message}`); | |
} | |
break; | |
default: | |
console.log('typing --make, --encrypt or --show'); | |
} | |
} | |
else { | |
console.log('define if it is --make, --encrypt or --show'); | |
} | |
//# sourceMappingURL=SecurityUtil.js.map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment