..

Cryptopals: Xor Byte Sequences

This is the second challenge in the first set. It’s a short function in Rust:

pub type Bytes = Vec<u8>;

/// Xor two byte sequences
pub fn xor(a: Bytes, b: Bytes) -> Bytes {
    a.iter().zip(b.iter())
        .map(|(&x, &y)| x^y).collect()
}

One slight issue is that if the inputs don’t have the same length, we just use what we can (and don’t throw an error or use Result). It’s OK for now, but would be nice to tighten it a bit later on.

But I’m not sure about the correct, idiomatic way do to this.

I think that passing Vec is more idiomatic than passing &Vec, but I can’t really justify it to myself, other than the fact that the code is “cleaner” in a way (very vague).

But there are two other options I can think of:

  1. Passing slices. A bit more generic than a vector, because it can also be part of a vector or part of an array, and insisting on a vector may require building extra objects.
  2. Passing iterators. Again a bit more generic, an iterator doesn’t have to have everything in memory, it could even generate data on the fly (e.g. encrypting a stream of data that is read from a file).

For now I’ll keep it simple. It’s easy enough to change.