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
//! Module dedicated to PGP verification.
//!
//! This module exposes a simple function [`verify`] and its
//! associated [`Error`]s.

use pgp_native::{SignedPublicKey, StandaloneSignature};
use tokio::task;

use crate::{Error, Result};

/// Verifies given standalone signature using the given public key.
pub async fn verify(
    pkey: SignedPublicKey,
    signature: StandaloneSignature,
    signed_bytes: Vec<u8>,
) -> Result<()> {
    task::spawn_blocking(move || {
        signature
            .verify(&pkey, &signed_bytes)
            .map_err(Error::VerifySignatureError)?;
        Ok(())
    })
    .await?
}

#[cfg(test)]
mod tests {
    use crate::{gen_key_pair, read_sig_from_bytes, sign, verify};

    #[tokio::test]
    async fn sign_then_verify() {
        let (skey, pkey) = gen_key_pair("test@localhost", "").await.unwrap();
        let msg = b"signed message".to_vec();
        let raw_sig = sign(skey, "", msg.clone()).await.unwrap();
        let sig = read_sig_from_bytes(raw_sig).await.unwrap();

        verify(pkey, sig, msg).await.unwrap();
    }
}