ircmaxell / RandomLib

A library for generating random numbers and strings
MIT License
841 stars 116 forks source link

Fix XORMixer double XOR issue #60

Open aforsblo opened 7 years ago

aforsblo commented 7 years ago

This pull request fixes an issue where an XOR operation is done in both AbstractMixer and XORMixer, causing the source from the previous iteration to get XORed with itself, setting it to zero. This in turn causes XORMixer to always return the last source verbatim.

AbstractMixer::mix() contains the following code:

if ($j % 2 == 1) {
    $stub ^= $this->mixParts1($stub, $newKey);
} else {
    $stub ^= $this->mixParts2($stub, $newKey);
}

By inlining XorMixer::mixParts1() and XorMixer::mixParts2(), we get:

if ($j % 2 == 1) {
    $stub = $stub ^ $stub ^ $newKey;
} else {
    $stub = $stub ^ $stub ^ $newKey;
}

which is equivalent to:

if ($j % 2 == 1) {
    $stub = $newKey;
} else {
    $stub = $newKey;
}