UNPKG

808 BJavaScriptView Raw
1const t=require("fs"),e=require("crypto");class s{constructor(t,e){this.filePath=t,this.password=e}encryptAsync(){return new Promise((s,i)=>{try{t.readFile(this.filePath,(r,a)=>{r&&i(r);var h=e.createCipher("aes-256-cbc",this.password),c=h.update(a.toString(),"utf8","base64");c+=h.final("base64"),this.filePath=this.filePath.substr(0,this.filePath.lastIndexOf("."))+".enc",t.writeFile(this.filePath,c,t=>{t&&i(t),s(c)})})}catch(t){i(t)}})}decryptAsync(){return new Promise((s,i)=>{t.readFile(this.filePath,(r,a)=>{r&&i(r);try{var h=e.createDecipher("aes-256-cbc",this.password),c=h.update(a.toString(),"base64","utf8");c+=h.final("utf8"),this.filePath=this.filePath.substr(0,this.filePath.lastIndexOf("."))+".json",t.writeFile(this.filePath,c,t=>{t&&i(t),s(c)})}catch(t){i(t)}})})}}exports.EncryptDecrypt=s;
\No newline at end of file