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
use crate::consts::SIGNATURE_VALUE_LENGTH;
use sha2::{Digest, Sha256};

use crate::protocol::signature::Sign;
use crate::protocol::SignatureValue;

/// <sup>`sha2`</sup>
/// Signs MAVLink packages with [`sha2`](https://crates.io/crates/sha2) library.
///
/// <sup>Available with `sha2` Cargo feature</sup>
///
/// Implements `sha256_48`, a `MAVLink 2` specific hashing algorithm similar to regular `sha256` except that only first
/// 48 bits are considered.
///
/// # Links
///
/// * [Signature specification](https://mavlink.io/en/guide/message_signing.html#signature) format in MAVLink docs.
#[derive(Debug, Default)]
pub struct MavSha256 {
    hasher: Sha256,
}

impl Sign for MavSha256 {
    fn reset(&mut self) {
        self.hasher.reset();
    }

    /// Consume data as a slice of bytes.
    fn digest(&mut self, bytes: &[u8]) {
        self.hasher.update(bytes)
    }

    /// Calculates signature from digested data.
    fn produce(&self) -> SignatureValue {
        let sha256 = self.hasher.clone().finalize();
        let mut bytes = [0; SIGNATURE_VALUE_LENGTH];
        bytes.copy_from_slice(&sha256[0..SIGNATURE_VALUE_LENGTH]);
        bytes
    }
}