Search code examples
javascriptmathxor

How to find a value that XORs to another value?


I'm trying to have 3 keys that XOR to the master key value.

A XOR B XOR C = MASTER KEY

If A, B and the MASTER KEY can be generated as a randomByte(LENGTH=32), how can I find the C value? (in javascript)


Solution

  • Since XOR is its own inverse (that is A XOR A = 0 for all bit patterns, A), you can find C by the following:

    C = A ^ B ^ MASTER_KEY;
    

    Where ^ is the JavaScript XOR operator, and A, B, and MASTER_KEY are the values you defined above.