UNPKG

hsynchronous-fs

Version:

Post-Quantum filesystem encryption using a hybrid encryption protocol "hsynchronous"

1 lines 1.97 kB
'use strict';var fs=require('fs'),hsynchronous=require('hsynchronous'),path=require('path'),child_process=require('child_process'),util=require('util'),crypto=require('crypto');var l=(o,a,r)=>new Promise((t,i)=>{var s=c=>{try{n(r.next(c));}catch(p){i(p);}},e=c=>{try{n(r.throw(c));}catch(p){i(p);}},n=c=>c.done?t(c.value):Promise.resolve(c.value).then(s,e);n((r=r.apply(o,a)).next());});util.promisify(child_process.exec);function m(o,a=100,r=125){for(let t=0;t<a;t++)typeof o=="string"?o=crypto.randomBytes(r).toString("hex"):o&&typeof o=="object"&&(o={kemKeyPair:crypto.randomBytes(r).toString("hex"),sigKeyPair:crypto.randomBytes(r).toString("hex")});}function K(o,a){return l(this,null,function*(){try{let r=path.join(__dirname,"../",a),t;try{t=fs.readFileSync(r,"hex");}catch(e){throw new Error(`Failed to read zip file: ${e.message}`)}try{fs.rmSync(r,{force:!0});}catch(e){throw new Error(`Failed to remove original zip file: ${e.message}`)}let i;try{i=yield hsynchronous.encrypt(t,o),m(o);}catch(e){throw new Error(`Encryption failed: ${e.message}`)}let s=path.join(__dirname,"../","encrypted");try{fs.writeFileSync(s,i);}catch(e){throw new Error(`Failed to write encrypted file: ${e.message}`)}}catch(r){throw new Error(`encryptCompressVirtualDrive failed: ${r.message}`)}})}function W(o,a,r){return l(this,null,function*(){try{let t=path.join(process.cwd(),r),i=path.join(__dirname,"../",a),s;try{s=fs.readFileSync(t,"utf8");}catch(n){throw new Error(`Failed to read encrypted file: ${n.message}`)}let e;try{e=yield hsynchronous.decrypt(s,o),m(o);}catch(n){throw new Error(`Decryption failed: ${n.message}`)}if(!e||!e.message)throw new Error("Decrypted data is invalid or missing message");try{fs.writeFileSync(i,e.message,{encoding:"hex"});}catch(n){throw new Error(`Failed to write decrypted file: ${n.message}`)}}catch(t){throw new Error(`decryptVirtualDrive failed: ${t.message}`)}})}exports.decryptVirtualDrive=W;exports.encryptCompressVirtualDrive=K;