pgp/
verify.rs

1//! # Verify
2//!
3//! Module dedicated to PGP verification. This module exposes a simple
4//! function [`verify`] and its associated [`Error`]s.
5
6use crate::{
7    native::{SignedPublicKey, StandaloneSignature},
8    utils::spawn_blocking,
9    Error, Result,
10};
11
12/// Verifies given standalone signature using the given public key.
13pub async fn verify(
14    pkey: SignedPublicKey,
15    signature: StandaloneSignature,
16    signed_bytes: Vec<u8>,
17) -> Result<()> {
18    spawn_blocking(move || {
19        signature
20            .verify(&pkey, &signed_bytes)
21            .map_err(Error::VerifySignatureError)?;
22        Ok(())
23    })
24    .await?
25}
26
27#[cfg(test)]
28mod tests {
29    #[cfg(feature = "async-std")]
30    use async_std::test;
31    #[cfg(feature = "tokio")]
32    use tokio::test;
33
34    use crate::{gen_key_pair, read_sig_from_bytes, sign, verify};
35
36    #[test_log::test(test)]
37    async fn sign_then_verify() {
38        let (skey, pkey) = gen_key_pair("test@localhost", "").await.unwrap();
39        let msg = b"signed message".to_vec();
40        let raw_sig = sign(skey, "", msg.clone()).await.unwrap();
41        let sig = read_sig_from_bytes(raw_sig).await.unwrap();
42
43        verify(pkey, sig, msg).await.unwrap();
44    }
45}