Skip to content

Instantly share code, notes, and snippets.

@PhoeniXkrypT
Last active September 12, 2022 17:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save PhoeniXkrypT/22b978dee79f3092d63f to your computer and use it in GitHub Desktop.
Save PhoeniXkrypT/22b978dee79f3092d63f to your computer and use it in GitHub Desktop.
Reversing shift-XORed in python
def reverse_right_shift(value, shift, mult=0xffffffff):
output, i = 0, 0
while i * shift < 32:
compartment = int(bin(0xffffffff << (32 - shift))[-32:], 2) >> (shift * i)
part_output = value & compartment
value ^= part_output >> shift & mult
output |= part_output
i += 1
return output
def reverse_left_shift(value, shift, mult=0xffffffff):
output, i = 0, 0
while i * shift < 32:
compartment = int(bin((0xffffffff >> (32- shift)) << (shift * i))[-32:], 2)
part_output = value & compartment
value ^= part_output << shift & mult
output |= part_output
i += 1
return output
value = 3009615726
_value = value ^ value >> 10
assert reverse_right_shift(_value, 10) == value
_value = value ^ value << 15 & 15141003
assert reverse_left_shift(_value, 15, 15141003) == value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment