buffer-xor icon indicating copy to clipboard operation
buffer-xor copied to clipboard

When lengths are different, smaller should be zero-padded from the left

Open nanaknihal opened this issue 3 years ago • 0 comments

xor(Buffer.from('7f5022b7e361a9ef34f2792b045bf413a985f8caf7978e67027dbb01b137b67d', 'hex'),
Buffer.from('C8834C1FcF0Df6623Fc8C8eD25064A4148D99388', 'hex'))

gives the wrong value. The answer should be 7f5022b7e361a9ef34f2792bccd8b80c66880ea8c85f468a277bf140f9ee25f5, but it gives b7d36ea82c6c5f8d0b3ab1c6215dbe52e15c6b42f7978e67027dbb01b137b67d

The reason is that it doesn't zero pad it from the left. It only indirectly zero-pads it to the right when sizes mismatch, because it will be comparing with NaNs which are treated as zeroes when the index exceeds the smaller buffer's length.

I fixed this by adding zero-padding:

var Buffer = require('safe-buffer').Buffer

module.exports = function xor (a, b) {
  // pad the shorter buffer with 0s
  let padded
  let a_
  let b_
  if(a.length > b.length){
    padded = Buffer.alloc(a.length)
    a_ = a
    b.copy(padded, padded.length-b.length)
    b_ = padded

  } else if(a.length < b.length){
    padded = Buffer.alloc(b.length)
    a_ = a.copy(padded, padded.length-a.length)
    a_ = padded
    b_ = b
  } else {
    a_ = a;
    b_ = b;
  }

  var length = a_.length
  var buffer = Buffer.allocUnsafe(length)

  for (var i = 0; i < length; ++i) {
    buffer[i] = a_[i] ^ b_[i]
  }
  return buffer
}

It is far longer than your original code and perhaps there's a more elegant solution. If you'd like, I can submit this as a PR, or feel free to copy if you want to add it.

nanaknihal avatar Mar 07 '22 20:03 nanaknihal