1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{Error, Word};
pub fn slice_data(data: &[u8]) -> Result<Vec<Word>, Error> {
if data.len() % 32 != 0 {
return Err(Error::InvalidData);
}
let times = data.len() / 32;
let mut result = Vec::with_capacity(times);
for i in 0..times {
let mut slice = [0u8; 32];
let offset = 32 * i;
slice.copy_from_slice(&data[offset..offset + 32]);
result.push(slice);
}
Ok(result)
}
pub fn pad_u32(value: u32) -> Word {
let mut padded = [0u8; 32];
padded[28..32].copy_from_slice(&value.to_be_bytes());
padded
}
#[cfg(test)]
mod tests {
use super::pad_u32;
use hex_literal::hex;
#[test]
fn test_pad_u32() {
assert_eq!(
pad_u32(0).to_vec(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").to_vec()
);
assert_eq!(
pad_u32(1).to_vec(),
hex!("0000000000000000000000000000000000000000000000000000000000000001").to_vec()
);
assert_eq!(
pad_u32(0x100).to_vec(),
hex!("0000000000000000000000000000000000000000000000000000000000000100").to_vec()
);
assert_eq!(
pad_u32(0xffffffff).to_vec(),
hex!("00000000000000000000000000000000000000000000000000000000ffffffff").to_vec()
);
}
}