Skip to content

Instantly share code, notes, and snippets.

@drewstaylor
Created March 15, 2025 12:58
Show Gist options
  • Save drewstaylor/33a7b21cccb65c6711b095ec77e5d7da to your computer and use it in GitHub Desktop.
Save drewstaylor/33a7b21cccb65c6711b095ec77e5d7da to your computer and use it in GitHub Desktop.
Remembering that time I had to XOR in JavaScript...
const xor = function (a, b) {
if (!Buffer.isBuffer(a)) a = new Buffer.from(a);
if (!Buffer.isBuffer(b)) b = new Buffer.from(b);
let res = []
const len = (a.length > b.length)
? b.length
: a.length;
for (let i = 0; i < len; i++) {
res.push(a[i] ^ b[i]);
}
return new Buffer.from(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment