timing_safe_equal.js 538 B

12345678910111213141516171819
  1. const timingSafeEqual = (a, b) => {
  2. if (!(a instanceof Uint8Array)) {
  3. throw new TypeError('First argument must be a buffer');
  4. }
  5. if (!(b instanceof Uint8Array)) {
  6. throw new TypeError('Second argument must be a buffer');
  7. }
  8. if (a.length !== b.length) {
  9. throw new TypeError('Input buffers must have the same length');
  10. }
  11. const len = a.length;
  12. let out = 0;
  13. let i = -1;
  14. while (++i < len) {
  15. out |= a[i] ^ b[i];
  16. }
  17. return out === 0;
  18. };
  19. export default timingSafeEqual;