decrypt.js 941 B

1234567891011121314151617181920212223242526
  1. var SEA = require('./root');
  2. var shim = require('./shim');
  3. var S = require('./settings');
  4. var aeskey = require('./aeskey');
  5. var parse = require('./parse');
  6. SEA.decrypt = SEA.decrypt || (async (data, pair, cb, opt) => { try {
  7. var opt = opt || {};
  8. const key = pair.epriv || pair;
  9. const json = parse(data)
  10. const ct = await aeskey(key, shim.Buffer.from(json.s, 'utf8'), opt)
  11. .then((aes) => (/*shim.ossl ||*/ shim.subtle).decrypt({ // Keeping aesKey scope as private as possible...
  12. name: opt.name || 'AES-GCM', iv: new Uint8Array(shim.Buffer.from(json.iv, 'utf8'))
  13. }, aes, new Uint8Array(shim.Buffer.from(json.ct, 'utf8'))))
  14. const r = parse(new shim.TextDecoder('utf8').decode(ct))
  15. if(cb){ try{ cb(r) }catch(e){console.log(e)} }
  16. return r;
  17. } catch(e) {
  18. SEA.err = e;
  19. if(cb){ cb() }
  20. return;
  21. }});
  22. module.exports = SEA.decrypt;