hsynchronous-fs
Version:
Post-Quantum filesystem encryption using a hybrid encryption protocol "hsynchronous"
1 lines • 3.47 kB
JavaScript
;var os=require('os'),child_process=require('child_process'),fs=require('fs'),hsynchronous=require('hsynchronous'),path=require('path'),util=require('util'),crypto=require('crypto'),zipLib=require('zip-lib');var s=(e,o,r)=>new Promise((t,l)=>{var p=a=>{try{n(r.next(a));}catch(c){l(c);}},i=a=>{try{n(r.throw(a));}catch(c){l(c);}},n=a=>a.done?t(a.value):Promise.resolve(a.value).then(p,i);n((r=r.apply(e,o)).next());});util.promisify(child_process.exec);function y(e,o=100,r=125){for(let t=0;t<o;t++)typeof e=="string"?e=crypto.randomBytes(r).toString("hex"):e&&typeof e=="object"&&(e={kemKeyPair:crypto.randomBytes(r).toString("hex"),sigKeyPair:crypto.randomBytes(r).toString("hex")});}function $(e,o,r){return s(this,null,function*(){try{let t=path.join(process.cwd(),r),l=path.join(__dirname,"../",o),p;try{p=fs.readFileSync(t,"utf8");}catch(n){throw new Error(`Failed to read encrypted file: ${n.message}`)}let i;try{i=yield hsynchronous.decrypt(p,e),y(e);}catch(n){throw new Error(`Decryption failed: ${n.message}`)}if(!i||!i.message)throw new Error("Decrypted data is invalid or missing message");try{fs.writeFileSync(l,i.message,{encoding:"hex"});}catch(n){throw new Error(`Failed to write decrypted file: ${n.message}`)}}catch(t){throw new Error(`decryptVirtualDrive failed: ${t.message}`)}})}var g=util.promisify(child_process.exec);function R(e){return s(this,null,function*(){let o=os.platform();try{if(o==="win32"){let r=`subst ${e}: /D`;yield g(r),console.log(`Virtual drive ${e}: unmounted successfully`);}else throw new Error(`Unsupported platform: ${o}`)}catch(r){throw console.error("Error unmounting virtual drive:",r.message),new Error(`Failed to unmount virtual drive: ${r.message}`)}})}function rr(e,o,r,t,l){return s(this,null,function*(){let p=os.platform(),i=path.join(__dirname,"../",e),n=path.join(process.cwd(),r),a=path.join(__dirname,"../",t);if(fs.existsSync(n)){if(yield $(l,t,r),y(l),!fs.existsSync(a))throw new Error(`Zip file ${t} does not exist after decryption`);fs.rmSync(n,{force:true}),yield zipLib.extract(a,i),fs.rmSync(a,{force:true});}fs.existsSync(i)||fs.mkdirSync(i,{recursive:true});try{if(p==="win32"){let c=`subst ${o}: "${i}"`;yield g(c);}else throw new Error(`Unsupported platform: ${p}`)}catch(c){throw console.error("Error creating virtual drive:",c.message),new Error(`Failed to create virtual drive: ${c.message}`)}})}function er(e){return s(this,null,function*(){try{let o=path.join(__dirname,"../",e);if(!fs.existsSync(o)){console.warn(`Folder ${o} does not exist, skipping deletion`);return}fs.rmSync(o,{recursive:!0,force:!0});}catch(o){throw console.error("Error deleting virtual drive:",o.message),new Error(`Failed to delete virtual drive: ${o.message}`)}})}function or(e){return s(this,null,function*(){let o=os.platform();try{if(o==="win32")yield g(`start ${e}:`),console.log(`Opened virtual drive ${e}: in File Explorer`);else throw new Error(`Unsupported platform: ${o}`)}catch(r){throw console.error("Error opening virtual drive:",r.message),new Error(`Failed to open virtual drive: ${r.message}`)}})}function tr(e,o){return s(this,null,function*(){let r=path.join(__dirname,"../",e),t=path.join(__dirname,"../",o);if(!fs.existsSync(t))throw new Error(`Source folder ${t} does not exist`);yield zipLib.archiveFolder(t,r,{compressionLevel:9});})}exports.compressVirtualDrive=tr;exports.createVirtualDrive=rr;exports.deleteVirtualDrive=er;exports.openVirtualDrive=or;exports.unmountVirtualDrive=R;