pub struct GnuPG { /* private fields */ }Expand description
A struct representing a GnuPG instance.
Implementations§
Source§impl GnuPG
impl GnuPG
Sourcepub fn new() -> Option<Self>
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.
Sourcepub fn with_executable(exec: &str) -> Self
pub fn with_executable(exec: &str) -> Self
Returns a new GnuPG instance with the specified executable and no specified home directory.
Sourcepub fn set_homedir(self, dir: &str) -> Self
pub fn set_homedir(self, dir: &str) -> Self
Returns a new GnuPG instance with the specified home directory.
Sourcepub fn import_key(&self, key_data: &str) -> Result<Key, GPGError>
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);Sourcepub fn export_key(&self, key: &Key) -> String
pub fn export_key(&self, key: &Key) -> String
Exports the specified key in ASCII-armored format.
§Arguments
key- TheKeystruct 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);Sourcepub fn export_secret_key(&self, key: &Key) -> Result<String, GPGError>
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- TheKeystruct 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 asNone(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);Sourcepub fn list_keys(&self) -> Result<Vec<Key>, GPGError>
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());Sourcepub fn generate_key(
&self,
name: &str,
mail: &str,
pw: Option<&str>,
) -> Result<Key, GPGError>
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 keymail- the email to be associated with the keypw- an optional password to encrypt the key
§Examples
let gpg = GnuPG::new().unwrap();
let key = gpg.generate_key("Alice", "alice@example.com", None).unwrap();Sourcepub fn sign(&self, key: &Key, msg: &str) -> Result<String, GPGError>
pub fn sign(&self, key: &Key, msg: &str) -> Result<String, GPGError>
Signs a message using the specified key.
§Arguments
key- TheKeystruct 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);Sourcepub fn encrypt(&self, receiver: &Key, msg: &str) -> Result<String, GPGError>
pub fn encrypt(&self, receiver: &Key, msg: &str) -> Result<String, GPGError>
Encrypts a message for the specified key.
§Arguments
receiver- TheKeystruct 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);Sourcepub fn pw_encrypt(&self, pw: &str, msg: &str) -> Result<String, GPGError>
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);Sourcepub fn pw_decrypt(&self, pw: &str, msg: &str) -> Result<String, GPGError>
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);Sourcepub fn decrypt(
&self,
key: &Key,
msg: &str,
) -> Result<DecryptedMessage, GPGError>
pub fn decrypt( &self, key: &Key, msg: &str, ) -> Result<DecryptedMessage, GPGError>
Decrypts a message using the specified key.
§Arguments
key- TheKeystruct 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);Sourcepub fn verify(&self, key: &Key, msg: &str) -> Result<Verify, GPGError>
pub fn verify(&self, key: &Key, msg: &str) -> Result<Verify, GPGError>
Verifies the signature of a message using the specified key.
§Arguments
key- TheKeystruct 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);