Cleaned up message padding function (thx @roh)

This commit is contained in:
_N0x 2024-10-31 22:35:05 +01:00
parent b644455842
commit a01fd584fc

View File

@ -29,32 +29,28 @@ fn ssig1(x: u32) -> u32 {
return ret;
}
fn pad_message(message: String) -> Vec<u32> {
let mut msg_bytes = message.as_bytes().to_vec();
let l = (msg_bytes.len() as u64) * 8;
fn pad_message(msg: String) -> Vec<u32> {
let l = (msg.len() as u64) << 3;
let k = ((448u64.wrapping_sub((l + 8) % 512) % 512) >> 3) as usize;
msg_bytes.push(0x80);
let mut result_u8 = Vec::with_capacity(msg.len() + 1 + k + std::mem::size_of::<u64>());
let k = (448u64.wrapping_sub((l + 8) % 512)) % 512;
let k_bytes = k / 8;
result_u8.extend(msg.as_bytes());
result_u8.push(0x80);
result_u8.resize(result_u8.len() + k, 0u8);
result_u8.extend(&l.to_be_bytes());
let padding = vec![0u8; k_bytes as usize];
let mut result_u32 = Vec::with_capacity(result_u8.len() / 4); // Allocate the right size for u32 vector
msg_bytes.extend(padding);
msg_bytes.extend(&l.to_be_bytes());
let mut u32_vec = Vec::with_capacity(msg_bytes.len() / 4); // Allocate the right size for u32 vector
for chunk in msg_bytes.chunks(4) {
for chunk in result_u8.chunks(4) {
let value = (chunk[0] as u32) << 24
| (chunk[1] as u32) << 16
| (chunk[2] as u32) << 8
| (chunk[3] as u32);
u32_vec.push(value);
result_u32.push(value);
}
return u32_vec;
return result_u32;
}
//////////// END HELPER FUNCTIONS /////////////