Files
2019-11-15 17:37:03 +08:00

82 lines
2.0 KiB
JavaScript

#!/usr/bin/evn node
const crypto = require('crypto');
const path = require('path')
const fs = require('fs')
let arg = process.argv[2];
if (!arg) {
console.error("need arg (file/dir)")
return
}
if (!path.isAbsolute(arg)) {
arg = path.resolve(arg)
}
fs.stat(arg, function (err, stats) {
if (err) console.error(err.message)
if (stats.isDirectory()) {
batchEnc(arg)
} else {
if (path.extname(arg) !== '.cbt') {
console.error("need file with ext 'cbt'")
return
}
encSameDir(arg)
}
})
function encSameDir(filePath) {
let targetPath = getTargetPath(filePath)
enc(filePath, targetPath)
}
function getTargetPath(filePath) {
return path.join(path.dirname(filePath), path.basename(filePath, 'cbt') + "ttx")
}
function batchEnc(dirPath) {
fs.readdirSync(dirPath, {encoding: "utf-8", withFileTypes: true}).forEach((dirent) => {
if (dirent.isFile() && dirent.name.trim().toLowerCase().endsWith(".cbt")) {
let filePath = path.join(dirPath, dirent.name)
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(filePath)
}
encSameDir(filePath)
console.log('generate success \t' + dirent.name)
}
})
}
function enc(srcFilePath, targetFilePath) {
const algorithm = 'aes-192-cbc';
const password = 'multiple profile creator';
// Use the async `crypto.scrypt()` instead.
const key = crypto.scryptSync(password, 'salt', 24);
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
// shown here.
const iv = Buffer.alloc(16, 0); // Initialization vector.
const cipher = crypto.createCipheriv(algorithm, key, iv);
const input = fs.createReadStream(srcFilePath);
const output = fs.createWriteStream(targetFilePath);
output.setDefaultEncoding("utf-8")
cipher.setEncoding("binary")
input.pipe(cipher).pipe(output);
}
// fs.unlink(filePath, (err)=>{
// if (err) console.error(err.message)
// else console.log("removed")
// })