* XOR the contents of this stream with a certain key. * * @param[in] key The key used to XOR the data in this stream. */
| 346 | * @param[in] key The key used to XOR the data in this stream. |
| 347 | */ |
| 348 | void Xor(const std::vector<unsigned char>& key) |
| 349 | { |
| 350 | if (key.size() == 0) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | for (size_type i = 0, j = 0; i != size(); i++) { |
| 355 | vch[i] ^= std::byte{key[j++]}; |
| 356 | |
| 357 | // This potentially acts on very many bytes of data, so it's |
| 358 | // important that we calculate `j`, i.e. the `key` index in this |
| 359 | // way instead of doing a %, which would effectively be a division |
| 360 | // for each byte Xor'd -- much slower than need be. |
| 361 | if (j == key.size()) |
| 362 | j = 0; |
| 363 | } |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | template <typename IStream> |