neo_decompiler/nef/parser/checksum.rs
1use sha2::{Digest, Sha256};
2
3use super::NefParser;
4
5fn checksum_prefix(bytes: &[u8]) -> u32 {
6 let mut array = [0u8; 4];
7 array.copy_from_slice(&bytes[..4]);
8 u32::from_le_bytes(array)
9}
10
11impl NefParser {
12 /// Calculate the NEF checksum over the payload bytes.
13 ///
14 /// This implements the double-SHA256 checksum used by the NEF container and
15 /// returns the first 4 bytes of the resulting digest as a little-endian
16 /// `u32`.
17 #[must_use]
18 pub fn calculate_checksum(payload: &[u8]) -> u32 {
19 let first = Sha256::digest(payload);
20 let second = Sha256::digest(first.as_slice());
21 checksum_prefix(second.as_slice())
22 }
23}