#encryption #pgp #decryption

pgp-lib

High-level, asynchronous API for rPGP, a pure Rust implementation of OpenPGP

1 stable release

1.0.0 Oct 27, 2024
0.3.0 Oct 22, 2024
0.2.0 Apr 6, 2024
0.1.0 Aug 27, 2023
0.0.1 Aug 23, 2023

#1195 in Asynchronous

Download history 167/week @ 2025-12-18 111/week @ 2025-12-25 122/week @ 2026-01-01 180/week @ 2026-01-08 170/week @ 2026-01-15 824/week @ 2026-01-22 1489/week @ 2026-01-29 1137/week @ 2026-02-05 1140/week @ 2026-02-12 1387/week @ 2026-02-19 1491/week @ 2026-02-26 2036/week @ 2026-03-05 1623/week @ 2026-03-12 1813/week @ 2026-03-19 1339/week @ 2026-03-26 1241/week @ 2026-04-02

6,321 downloads per month
Used in 8 crates (4 directly)

MIT license

51KB
894 lines

🔐 pgp-lib

High-level, asynchronous API for rPGP, a pure Rust implementation of OpenPGP.

Features

  • Exports basic PGP operations: encrypt, decrypt, sign, verify
  • Exposes PGP helpers: generate a key pair, read secret/public keys from path, read signature from bytes etc
  • Proposes HTTP public key discovery via WKD and HKP
  • Supports tokio and async-std async runtimes
  • Supports rustls and native-tls crypto libs

The library comes with 6 cargo features, including 2 default ones:

  • tokio: enables the tokio async runtime
  • async-std: enables the async-std async runtime
  • rustls: enables the rustls crypto
  • native-tls: enables the native-tls crypto
  • key-discovery: enables public key discovery mechanisms
  • vendored: compiles and statically link to a copy of non-Rust vendors like OpenSSL

Example

use pgp::{decrypt, encrypt, gen_key_pair, read_sig_from_bytes, sign, verify};

#[tokio::main]
async fn main() {
    let (alice_skey, alice_pkey) = gen_key_pair("alice@localhost", "").await.unwrap();
    let (bob_skey, bob_pkey) = gen_key_pair("bob@localhost", "").await.unwrap();

    let msg = b"message".to_vec();
	
	// encrypt message with multiple recipients
	
    let encrypted_msg = encrypt(vec![alice_pkey.clone(), bob_pkey], msg.clone())
        .await
        .unwrap();
	
	// decrypt message
	
    assert_eq!(msg, decrypt(alice_skey.clone(), "", encrypted_msg.clone()).await.unwrap());
    assert_eq!(msg, decrypt(bob_skey, "", encrypted_msg.clone()).await.unwrap());

    // sign message
	
    let raw_sig = sign(alice_skey, "", msg.clone()).await.unwrap();
    let sig = read_sig_from_bytes(raw_sig).await.unwrap();
	
	// verify message
	
    assert!(verify(alice_pkey, sig, msg).await.is_ok());
}

See the full API documentation on docs.rs.

Sponsoring

nlnet

Special thanks to the NLnet foundation and the European Commission that helped the project to receive financial support from various programs:

If you appreciate the project, feel free to donate using one of the following providers:

GitHub Ko-fi Buy Me a Coffee Liberapay thanks.dev PayPal

Dependencies

~21–41MB
~549K SLoC