Your conversions look good to me. And as you point out, CONV()
and ^
have really 64-bit precision.
2 ^ 64 = 16 ^ 16, so strings of more than 16 hexadecimal digits must be converted to integers greater than 2 ^ 64. However, such strings will be brutally (silently) truncated to the left when trying to convert them to integers.
The point of my solution here is to slice such lines. Obviously, the result may not be displayed as an integer, but only as a string representation.
Let @input
be your "string to be changed" and @key
, your "key".
- Assign
HEX(@input)
to @hex_input
. There is no problem here, since HEX()
works with strings. - Slice
@hex_input
in 16 hexadecimal digits with long strings starting from the right - Similarly, slice
@key
into 16-bit long lines. - Compute the
X-OR
each 64-bit @hex_input
fragment with each 64-bit @key
fragment, starting from the right. Use CONV(@slice, 16, 10)
. If either @hex_input
or @key
has fewer fragments than the other line, then X-OR
remaining fragments of the other line with 0. - Convert every 64-bit number obtained from
X-OR
in step 4. back to a hexadecimal string with UNHEX()
. - Collect the resulting slices. This is your result.
The TEMPORARY
table of three columns can be used as an array for storing @hex_input
, @mask
fragments and resulting fragments.
Put it all together in a stored procedure, and voilà!
It looks like you have some skills in MySQL, you should be able to translate the above into real code. But I will be happy to help if you need further guidance.
source share