2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2019-07-25 18:49:57 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module cipher
|
|
|
|
|
|
|
|
// NOTE: Implement other versions (joe-c)
|
|
|
|
// xor_bytes xors the bytes in a and b. The destination should have enough
|
|
|
|
// space, otherwise xor_bytes will panic. Returns the number of bytes xor'd.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn xor_bytes(mut dst []u8, a []u8, b []u8) int {
|
2019-07-25 18:49:57 +03:00
|
|
|
mut n := a.len
|
|
|
|
if b.len < n {
|
|
|
|
n = b.len
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-08-07 09:19:27 +03:00
|
|
|
safe_xor_bytes(mut dst, a, b, n)
|
2019-07-25 18:49:57 +03:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// safe_xor_bytes XORs the bytes in `a` and `b` into `dst` it does so `n` times.
|
|
|
|
// Please note: `n` needs to be smaller or equal than the length of `a` and `b`.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn safe_xor_bytes(mut dst []u8, a []u8, b []u8, n int) {
|
2020-10-21 12:23:03 +03:00
|
|
|
for i in 0 .. n {
|
2019-07-25 18:49:57 +03:00
|
|
|
dst[i] = a[i] ^ b[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// xor_words XORs multiples of 4 or 8 bytes (depending on architecture.)
|
|
|
|
// The slice arguments `a` and `b` are assumed to be of equal length.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn xor_words(mut dst []u8, a []u8, b []u8) {
|
2019-08-07 09:19:27 +03:00
|
|
|
safe_xor_bytes(mut dst, a, b, b.len)
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|