index.js 406 B

12345678910111213141516171819202122232425
  1. /**
  2. * Expose secure-compare
  3. */
  4. module.exports = compare;
  5. /**
  6. * Secure compare
  7. */
  8. function compare (a, b) {
  9. if (typeof a !== 'string' || typeof b !== 'string') return false;
  10. var mismatch = a.length === b.length ? 0 : 1;
  11. if (mismatch) {
  12. b = a;
  13. }
  14. for (var i = 0, il = a.length; i < il; ++i) {
  15. mismatch |= (a.charCodeAt(i) ^ b.charCodeAt(i));
  16. }
  17. return mismatch === 0;
  18. };