settings.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var SEA = require('./root');
  2. var Buffer = require('./buffer');
  3. var s = {};
  4. s.pbkdf2 = {hash: {name : 'SHA-256'}, iter: 100000, ks: 64};
  5. s.ecdsa = {
  6. pair: {name: 'ECDSA', namedCurve: 'P-256'},
  7. sign: {name: 'ECDSA', hash: {name: 'SHA-256'}}
  8. };
  9. s.ecdh = {name: 'ECDH', namedCurve: 'P-256'};
  10. // This creates Web Cryptography API compliant JWK for sign/verify purposes
  11. s.jwk = function(pub, d){ // d === priv
  12. pub = pub.split('.');
  13. var x = pub[0], y = pub[1];
  14. var jwk = {kty: "EC", crv: "P-256", x: x, y: y, ext: true};
  15. jwk.key_ops = d ? ['sign'] : ['verify'];
  16. if(d){ jwk.d = d }
  17. return jwk;
  18. };
  19. s.keyToJwk = function(keyBytes) {
  20. const keyB64 = keyBytes.toString('base64');
  21. const k = keyB64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
  22. return { kty: 'oct', k: k, ext: false, alg: 'A256GCM' };
  23. }
  24. s.recall = {
  25. validity: 12 * 60 * 60, // internally in seconds : 12 hours
  26. hook: function(props){ return props } // { iat, exp, alias, remember } // or return new Promise((resolve, reject) => resolve(props)
  27. };
  28. s.check = function(t){ return (typeof t == 'string') && ('SEA{' === t.slice(0,4)) }
  29. s.parse = function p(t){ try {
  30. var yes = (typeof t == 'string');
  31. if(yes && 'SEA{' === t.slice(0,4)){ t = t.slice(3) }
  32. return yes ? JSON.parse(t) : t;
  33. } catch (e) {}
  34. return t;
  35. }
  36. SEA.opt = s;
  37. module.exports = s