use sha2::{Digest, Sha256};

pub fn auth_fingerprint(
    signature: &[u8],
    dex: &[u8],
    shared_object: &[u8],
    calls_seed: i64,
    device_id: &str,
) -> Vec<u8> {
    let seed = calls_seed.to_be_bytes();
    let mut output = Vec::with_capacity(96);
    for digest in [signature, dex, shared_object] {
        let mut hasher = Sha256::new();
        hasher.update(digest);
        hasher.update(seed);
        hasher.update(device_id.as_bytes());
        output.extend_from_slice(&hasher.finalize());
    }
    output
}

#[cfg(test)]
mod tests {
    use super::auth_fingerprint;

    #[test]
    fn fingerprint_has_three_sha256_blocks() {
        let fingerprint = auth_fingerprint(b"signature", b"dex", b"shared", 42, "device-test");
        assert_eq!(fingerprint.len(), 96);
        assert_ne!(&fingerprint[0..32], &fingerprint[32..64]);
        assert_ne!(&fingerprint[32..64], &fingerprint[64..96]);
    }
}
