* XOR the contents of this stream with a certain key. * * @param[in] key The key used to XOR the data in this stream. */
| 418 | * @param[in] key The key used to XOR the data in this stream. |
| 419 | */ |
| 420 | void Xor(const std::vector<unsigned char>& key) |
| 421 | { |
| 422 | if (key.size() == 0) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | for (size_type i = 0, j = 0; i != size(); i++) { |
| 427 | vch[i] ^= key[j++]; |
| 428 | |
| 429 | // This potentially acts on very many bytes of data, so it's |
| 430 | // important that we calculate `j`, i.e. the `key` index in this |
| 431 | // way instead of doing a %, which would effectively be a division |
| 432 | // for each byte Xor'd -- much slower than need be. |
| 433 | if (j == key.size()) |
| 434 | j = 0; |
| 435 | } |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 |