var SEA = require('./root'); var shim = require('./shim'); var S = require('./settings'); var aeskey = require('./aeskey'); SEA.encrypt = SEA.encrypt || (async (data, pair, cb, opt) => { try { opt = opt || {}; var key = (pair||opt).epriv || pair; if(!key){ pair = await SEA.I(null, {what: data, how: 'encrypt', why: opt.why}); key = pair.epriv || pair; } const msg = JSON.stringify(data) const rand = {s: shim.random(8), iv: shim.random(16)}; const ct = await aeskey(key, rand.s, opt) .then((aes) => (/*shim.ossl ||*/ shim.subtle).encrypt({ // Keeping the AES key scope as private as possible... name: opt.name || 'AES-GCM', iv: new Uint8Array(rand.iv) }, aes, new shim.TextEncoder().encode(msg))) const r = 'SEA'+JSON.stringify({ ct: shim.Buffer.from(ct, 'binary').toString(opt.encode || 'base64'), iv: rand.iv.toString(opt.encode || 'base64'), s: rand.s.toString(opt.encode || 'base64') }); if(cb){ try{ cb(r) }catch(e){console.log(e)} } return r; } catch(e) { SEA.err = e; if(SEA.throw){ throw e } if(cb){ cb() } return; }}); module.exports = SEA.encrypt;