File: sign.rs

package info (click to toggle)
rust-gpgme 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 552 kB
  • sloc: makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,315 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use gpgme;
use structopt;

use gpgme::{Context, Protocol};
use std::{
    error::Error,
    fs::File,
    io::{self, prelude::*},
    path::PathBuf,
};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Cli {
    #[structopt(long)]
    /// Use the CMS protocol
    cms: bool,
    #[structopt(long)]
    #[structopt(long, conflicts_with = "normal")]
    /// Create a detached signature
    detach: bool,
    #[structopt(long, conflicts_with = "normal", conflicts_with = "detach")]
    /// Create a clear text signature
    clear: bool,
    #[structopt(long)]
    /// Key to use for signing. Default key is used otherwise
    key: Option<String>,
    #[structopt(parse(from_os_str))]
    /// File to sign
    filename: PathBuf,
}

fn main() -> Result<(), Box<dyn Error>> {
    let args = Cli::from_args();
    let proto = if args.cms {
        Protocol::Cms
    } else {
        Protocol::OpenPgp
    };

    let mode = if args.detach {
        gpgme::SignMode::Detached
    } else if args.clear {
        gpgme::SignMode::Clear
    } else {
        gpgme::SignMode::Normal
    };

    let mut ctx = Context::from_protocol(proto)?;
    ctx.set_armor(true);

    if let Some(key) = args.key {
        let key = ctx
            .get_secret_key(key)
            .map_err(|e| format!("unable to find signing key: {:?}", e))?;
        ctx.add_signer(&key)
            .map_err(|e| format!("add_signer() failed: {:?}", e))?;
    }

    let filename = &args.filename;
    let mut input = File::open(filename)
        .map_err(|e| format!("can't open file `{}': {:?}", filename.display(), e))?;
    let mut output = Vec::new();
    let result = ctx
        .sign(mode, &mut input, &mut output)
        .map_err(|e| format!("signing failed {:?}", e))?;
    print_result(&result);

    println!("Begin Output:");
    io::stdout().write_all(&output)?;
    println!("End Output.");
    Ok(())
}

fn print_result(result: &gpgme::SigningResult) {
    for sig in result.new_signatures() {
        println!("Key fingerprint: {}", sig.fingerprint().unwrap_or("[none]"));
        println!("Signature type : {:?}", sig.mode());
        println!("Public key algo: {}", sig.key_algorithm());
        println!("Hash algo .....: {}", sig.hash_algorithm());
        println!("Creation time .: {:?}", sig.creation_time());
    }
}