GnuPG

Struct GnuPG 

Source
pub struct GnuPG { /* private fields */ }
Expand description

A struct representing a GnuPG instance.

Implementations§

Source§

impl GnuPG

Source

pub fn new() -> Option<Self>

Returns a new GnuPG instance with the default executable and default home directory. If the default executable is not found, returns None.

Source

pub fn with_executable(exec: &str) -> Self

Returns a new GnuPG instance with the specified executable and no specified home directory.

Source

pub fn set_homedir(self, dir: &str) -> Self

Returns a new GnuPG instance with the specified home directory.

Source

pub fn import_key(&self, key_data: &str) -> Result<Key, GPGError>

Imports a public key and returns the Key struct representing it.

§Arguments
  • key_data - A string containing the key data to be imported.
§Returns

A Result containing either the imported Key or a GPGError variant indicating the error that occurred.

§Example
use gnupg::GnuPG;
let gnupg = GnuPG::new().unwrap();
let key_data = "-----BEGIN PGP PUBLIC KEY BLOCK-----
                ...
                -----END PGP PUBLIC KEY BLOCK-----";
let key = gnupg.import_key(key_data).unwrap();
println!("Imported key with fingerprint: {}", key.fingerprint);
Source

pub fn export_key(&self, key: &Key) -> String

Exports the specified key in ASCII-armored format.

§Arguments
  • key - The Key struct representing the key to export.
§Returns

The exported key as a string in ASCII-armored format.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let exported_key = gnupg.export_key(key);
println!("Exported key:\n{}", exported_key);
Source

pub fn export_secret_key(&self, key: &Key) -> Result<String, GPGError>

Exports the private key of the specified key pair in ASCII-armored format.

§Arguments
  • key - The Key struct representing the key pair whose private key to export.
  • pw - The password for the private key. If the private key is not password-protected, this argument can be left as None (default value: None).
§Returns

The exported private key as a string in ASCII-armored format.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let exported_key = gnupg.export_secret_key(key);
println!("Exported key:\n{}", exported_key);
Source

pub fn list_keys(&self) -> Result<Vec<Key>, GPGError>

Returns a list of all public keys in the keyring.

§Returns

A Result containing either a vector of Key structs representing the keys, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let keys = gnupg.list_keys().unwrap();
println!("Number of keys: {}", keys.len());
Source

pub fn generate_key( &self, name: &str, mail: &str, pw: Option<&str>, ) -> Result<Key, GPGError>

Generates a new GPG key with the given name and email. If a password is provided, the key will be encrypted with the password. If the key already exists, returns a KeyAlreadyExists error.

§Arguments
  • name - the name to be associated with the key
  • mail - the email to be associated with the key
  • pw - an optional password to encrypt the key
§Examples
let gpg = GnuPG::new().unwrap();
let key = gpg.generate_key("Alice", "alice@example.com", None).unwrap();
Source

pub fn sign(&self, key: &Key, msg: &str) -> Result<String, GPGError>

Signs a message using the specified key.

§Arguments
  • key - The Key struct representing the key to use for signing.
  • msg - The message to sign.
§Returns

A Result containing either the signed message as a string, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let signed_msg = gnupg.sign(key, "Hello, world!").unwrap();
println!("Signed message:\n{}", signed_msg);
Source

pub fn encrypt(&self, receiver: &Key, msg: &str) -> Result<String, GPGError>

Encrypts a message for the specified key.

§Arguments
  • receiver - The Key struct representing the key to encrypt the message for.
  • msg - The message to encrypt.
§Returns

A Result containing either the encrypted message as a string, or a GPGError variant indicating an error occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let encrypted_msg = gnupg.encrypt(key, "Hello, world!").unwrap();
println!("Encrypted message:\n{}", encrypted_msg);
Source

pub fn pw_encrypt(&self, pw: &str, msg: &str) -> Result<String, GPGError>

Encrypts a message using a password.

§Arguments
  • pw - The password to use for encryption.
  • msg - The message to encrypt.
§Returns

A Result containing either the encrypted message as a string, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let encrypted_msg = gnupg.pw_encrypt("mypassword", "Hello, world!").unwrap();
println!("Encrypted message:\n{}", encrypted_msg);
Source

pub fn pw_decrypt(&self, pw: &str, msg: &str) -> Result<String, GPGError>

Decrypts a message using a password.

§Arguments
  • pw - The password to use for decryption.
  • msg - The message to decrypt.
§Returns

A Result containing either the decrypted message as a string, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let encrypted_msg = "-----BEGIN PGP MESSAGE-----
                    ...
                    -----END PGP MESSAGE-----";
let decrypted_msg = gnupg.pw_decrypt("mypassword", encrypted_msg).unwrap();
println!("Decrypted message:\n{}", decrypted_msg);
Source

pub fn decrypt( &self, key: &Key, msg: &str, ) -> Result<DecryptedMessage, GPGError>

Decrypts a message using the specified key.

§Arguments
  • key - The Key struct representing the key to use for decryption.
  • msg - The message to decrypt.
§Returns

A Result containing either a DecryptedMessage struct with the decrypted message text and date of creation, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let encrypted_msg = "-----BEGIN PGP MESSAGE-----
                    ...
                    -----END PGP MESSAGE-----";
let decrypted_msg = gnupg.decrypt(key, encrypted_msg).unwrap();
println!("Decrypted message:\n{}\nDate: {}", decrypted_msg.text, decrypted_msg.date);
Source

pub fn verify(&self, key: &Key, msg: &str) -> Result<Verify, GPGError>

Verifies the signature of a message using the specified key.

§Arguments
  • key - The Key struct representing the key to use for verification.
  • msg - The message with a signature to verify.
§Returns

A Result containing either a Verify enum with more information on the signature, or a GPGError variant indicating an error that occurred.

§Example
let gnupg = GnuPG::new().unwrap();
let key = gnupg.list_keys().unwrap().first().unwrap();
let signed_msg = "-----BEGIN PGP MESSAGE-----
                 ...
                 -----END PGP MESSAGE-----";
let is_valid = gnupg.verify(key, signed_msg).unwrap();
println!("Signature is valid: {:?}", is_valid);

Auto Trait Implementations§

§

impl Freeze for GnuPG

§

impl RefUnwindSafe for GnuPG

§

impl Send for GnuPG

§

impl Sync for GnuPG

§

impl Unpin for GnuPG

§

impl UnwindSafe for GnuPG

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.