When lengths are different, smaller should be zero-padded from the left
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 50/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- javascript
- Domain
- tooling
Research direction
Start by reproducing the shown xor(Buffer.from(...)) call and inspect the module's buffer-alignment logic for unequal lengths. Done means the call returns the stated expected hex value, with equal-length inputs continuing to work as before.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- JavaScript
- Stars
- 32
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from browserify/buffer-xor
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
browserify/buffer-xor#16 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 50/100
browserify/buffer-xor#15 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
browserify/buffer-xor#11 ·
All issues in browserify/buffer-xor
Similar issues
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
githubnext/gh-aw-workshop#3692 ·
-
agent/guide documentation hive/hosted-available-lke648397-260827-5n31
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Add: BuyPass TV Openchannels:add check:passed
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
S: triage
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·