# Level 2: Fixed XOR

## <mark style="color:purple;">Task</mark>

Write a function that takes two equal-length buffers and produces their XOR combination.

If your function works properly, then when you feed it the string:

```
1c0111001f010100061a024b53535009181c
```

... after hex decoding, and when XOR'd against:

```
686974207468652062756c6c277320657965
```

... should produce:

```
746865206b696420646f6e277420706c6179
```

***

### <mark style="color:purple;">Explanation</mark>

#### What is Fixed XOR?

XOR is a binary operation. Essentially, two bits are compared. If they are the same, the result is 0; otherwise, the result is 1.

Example

* 0 + 0 = 0
* 1 + 0 = 1
* 0 + 1 = 1
* 1 + 1 = 0

"Fixed" means that both strings have the same length.

{% hint style="info" %}
You can learn more about XOR by reading [my article](https://www.linkedin.com/posts/patricioburattini_xor-the-simple-operation-behind-major-activity-7308565249832415232-EMSB?utm_source=social_share_send\&utm_medium=member_desktop_web\&rcm=ACoAACzoH-4BxIxxWtPU_MQNMwjumdgLCzeG5BA) on linkedin
{% endhint %}

***

### <mark style="color:purple;">Resolution</mark>

First, we are going to create a file named `task2.py`

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc8KOHhlTJWsN8X2AXe3QQ-n2BbAo44u91b-JHi-dn5ZusY6T1Za-zAu25JgaQrkUzfoZ_2sk6kV33lDC4VR6eJlq2743qloaaEz1-f-8-c47iTupsQut-xRf5umg1WYkVcFA-EfQ?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>

#### Understanding the code

* *<mark style="color:blue;">binascii</mark>* library is imported to manipulate hexadecimal data.
* After that, <mark style="color:blue;">`fixed_xor`</mark> function is defined, which takes two parameters
  * `hex1`, which contains the first hex string.
  * `hex2`, which contains the second hex string.
    * `bytes1` and `bytes2`  store the byte representation of `hex1` and `hex2`, respectively.
    * The variable `xored_bytes` stores the XOR result by following these steps
      * <mark style="color:blue;">`zip(bytes1, bytes2)`</mark> is a Python function that pairs elements from both byte sequences.
        * Example: a = \[1, 2, 3] b = \['x', 'y', 'z']. <mark style="color:blue;">`zip`</mark> returns \[(1, 'x'), (2, 'y'), (3, 'z')
      * `a ^ b for a, b in`` `<mark style="color:blue;">`zip(bytes1, bytes2)`</mark> is a Python expression that iterates through both byte sequences and applies XOR(^) to each pair of bytes.
      * `bytes()` is a Python function that takes an iterable and returns a <mark style="color:orange;">`bytes`</mark> object.
* Next, `hex1` and `hex2` are initialized with their respective strings.
* The variable `resultado` stores the result of <mark style="color:blue;">`fixer_xor(hex1, hex2)`</mark>.
* Finally, `resultado` is printed

***

### <mark style="color:purple;">Result</mark>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfPJDbPfiqu-ldZVxEEek8KjOE2cjj_4IYz8lqQIor5ekdMJegEJTBZUIxyjuB8C-GyJezy6uIcrV4cUOi2r-6e8BEydXc-WbbnWMhQejK7XxTET9YlKfHf-F7MSy_hwQPb4m8Zqw?key=0z75chVsoEW_cfTQtgh3riLM" alt=""><figcaption></figcaption></figure>
