hsynchronous-fs
Version:
Post-Quantum filesystem encryption using a hybrid encryption protocol "hsynchronous"
1 lines • 3.31 kB
JavaScript
;var fs=require('fs'),path=require('path'),child_process=require('child_process'),os=require('os'),util=require('util'),crypto=require('crypto');var i=(r,e,o)=>new Promise((n,t)=>{var l=c=>{try{u(o.next(c));}catch(p){t(p);}},a=c=>{try{u(o.throw(c));}catch(p){t(p);}},u=c=>c.done?n(c.value):Promise.resolve(c.value).then(l,a);u((o=o.apply(r,e)).next());});var k=util.promisify(child_process.exec);function C(r,e,o){return i(this,null,function*(){try{let n=path.join(__dirname,"../",e),t=path.join(__dirname,"../",o);try{fs.existsSync(n)&&(fs.rmSync(n,{recursive:!0,force:!0}),console.log(`Successfully removed folder: ${n}`));}catch(a){console.warn(`Warning: Could not remove folder ${n}: ${a.message}`);}try{fs.existsSync(t)&&(fs.rmSync(t,{force:!0}),console.log(`Successfully removed zip file: ${t}`));}catch(a){console.warn(`Warning: Could not remove zip file ${t}: ${a.message}`);}let l=os.platform();try{if(l==="win32"){let a=`subst ${r}: /D`;yield k(a),console.log(`Successfully unmounted drive ${r}:`);}else console.warn(`Cleanup for platform ${l} only supports file deletion, not unmounting`);}catch(a){}}catch(n){throw console.error("Error during cleanup:",n.message),new Error(`Cleanup failed: ${n.message}`)}})}function K(r){return i(this,null,function*(){try{let e=path.join(__dirname,"../",r);if(!fs.existsSync(e))throw new Error(`Key file not found at path: ${e}`);let o;try{o=fs.readFileSync(e,"utf8");}catch(t){throw new Error(`Failed to read key file: ${t.message}`)}let n=o.trim();if(!n)throw new Error("Key file is empty (contains only whitespace)");return n}catch(e){let o=`Failed to load key from ${r}: ${e.message}`;throw console.error(`Key loading error: ${o}`),new Error(o)}})}function W(r){return i(this,null,function*(){try{if(!r||typeof r!="string")throw new Error("Invalid file path provided");let e=`${r}.bak`;if(!fs.existsSync(r)){console.warn(`Warning: Source file ${r} does not exist, skipping backup creation.`);return}let o;try{o=fs.readFileSync(r,"utf8");}catch(n){throw new Error(`Failed to read source file: ${n.message}`)}if(o===void 0)throw new Error("File content was not read properly");try{fs.writeFileSync(e,o,"utf8"),console.log(`Success: Created backup at ${e}`);}catch(n){throw new Error(`Failed to create backup file: ${n.message}`)}}catch(e){throw console.error(`Backup Error: Failed to create backup for ${r}:`,e.message),new Error(`Backup operation failed: ${e.message}`)}})}function _(r){return i(this,null,function*(){try{if(!r||typeof r!="string")throw new Error("Invalid file path provided");let e=`${r}.bak`;if(!fs.existsSync(e)){console.warn(`Warning: Backup file ${e} does not exist, skipping deletion.`);return}try{fs.rmSync(e,{force:!0}),console.log(`Success: Deleted backup at ${e}`);}catch(o){throw new Error(`Failed to delete backup file: ${o.message}`)}}catch(e){throw console.error(`Backup Error: Failed to delete backup for ${r}:`,e.message),new Error(`Backup deletion failed: ${e.message}`)}})}function z(r,e=100,o=125){for(let n=0;n<e;n++)typeof r=="string"?r=crypto.randomBytes(o).toString("hex"):r&&typeof r=="object"&&(r={kemKeyPair:crypto.randomBytes(o).toString("hex"),sigKeyPair:crypto.randomBytes(o).toString("hex")});}exports.cleanup=C;exports.createBackup=W;exports.deleteBackup=_;exports.loadKey=K;exports.secureWipe=z;