sodium: Port examples to clap 3

This commit is contained in:
Sebastian Dröge 2022-01-02 11:12:41 +02:00 committed by Sebastian Dröge
parent 9858eeeb00
commit e2ecd77654
4 changed files with 61 additions and 92 deletions

View file

@ -17,7 +17,7 @@ hex = "0.4"
smallvec = "1.0"
# example
clap = { version = "2.33", optional = true }
clap = { version = "3", optional = true, features = ["derive"] }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }

View file

@ -30,9 +30,22 @@ use std::error::Error;
use std::fs::File;
use std::path::{Path, PathBuf};
use clap::{App, Arg};
use clap::Parser;
use serde::{Deserialize, Serialize};
#[derive(Parser, Debug)]
#[clap(version, author)]
#[clap(about = "Decrypt a gstsodium10 file")]
struct Args {
/// File to encrypt
#[clap(short, long)]
input: String,
/// File to decrypt
#[clap(short, long)]
output: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Keys {
public: box_::PublicKey,
@ -47,35 +60,11 @@ impl Keys {
}
fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("Decrypt a gstsodium10 file.")
.version("1.0")
.author("Jordan Petridis <jordan@centricular.com>")
.arg(
Arg::with_name("input")
.short("i")
.long("input")
.value_name("FILE")
.help("File to encrypt")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("output")
.short("o")
.long("output")
.value_name("FILE")
.help("File to decrypt")
.required(true)
.takes_value(true),
)
.get_matches();
let args = Args::parse();
gst::init()?;
gstsodium::plugin_register_static().expect("Failed to register sodium plugin");
let input_loc = matches.value_of("input").unwrap();
let out_loc = matches.value_of("output").unwrap();
let receiver_keys = {
let mut r = PathBuf::new();
r.push(env!("CARGO_MANIFEST_DIR"));
@ -102,8 +91,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let typefind = gst::ElementFactory::make("typefind", None).unwrap();
let filesink = gst::ElementFactory::make("filesink", None).unwrap();
filesrc.set_property("location", &input_loc);
filesink.set_property("location", &out_loc);
filesrc.set_property("location", &args.input);
filesink.set_property("location", &args.output);
decrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.private.0));
decrypter.set_property("sender-key", glib::Bytes::from_owned(sender.public));

View file

@ -30,9 +30,22 @@ use std::error::Error;
use std::fs::File;
use std::path::{Path, PathBuf};
use clap::{App, Arg};
use clap::Parser;
use serde::{Deserialize, Serialize};
#[derive(Parser, Debug)]
#[clap(version, author)]
#[clap(about = "Encrypt a file with in the gstsodium10 format")]
struct Args {
/// File to encrypt
#[clap(short, long)]
input: String,
/// File to decrypt
#[clap(short, long)]
output: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Keys {
public: box_::PublicKey,
@ -47,35 +60,11 @@ impl Keys {
}
fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("Encrypt a file with in the gstsodium10 format")
.version("1.0")
.author("Jordan Petridis <jordan@centricular.com>")
.arg(
Arg::with_name("input")
.short("i")
.long("input")
.value_name("FILE")
.help("File to encrypt")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("output")
.short("o")
.long("output")
.value_name("FILE")
.help("File to decrypt")
.required(true)
.takes_value(true),
)
.get_matches();
let args = Args::parse();
gst::init()?;
gstsodium::plugin_register_static().expect("Failed to register sodium plugin");
let input_loc = matches.value_of("input").unwrap();
let out_loc = matches.value_of("output").unwrap();
let receiver_keys = {
let mut r = PathBuf::new();
r.push(env!("CARGO_MANIFEST_DIR"));
@ -101,8 +90,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let encrypter = gst::ElementFactory::make("sodiumencrypter", None).unwrap();
let filesink = gst::ElementFactory::make("filesink", None).unwrap();
filesrc.set_property("location", &input_loc);
filesink.set_property("location", &out_loc);
filesrc.set_property("location", &args.input);
filesink.set_property("location", &args.output);
encrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.public));
encrypter.set_property("sender-key", glib::Bytes::from_owned(sender.private.0));

View file

@ -22,10 +22,24 @@
//
// SPDX-License-Identifier: MIT
use clap::{App, Arg};
use clap::Parser;
use serde::{Deserialize, Serialize};
use sodiumoxide::crypto::box_;
use std::fs::File;
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[clap(version, author)]
#[clap(about = "Generate a pair of Sodium's crypto_box_curve25519xsalsa20poly1305 keys.")]
struct Args {
/// Path to write the Keys
#[clap(short, long, parse(from_os_str))]
path: PathBuf,
/// Write a JSON file instead of a key.prv/key.pub pair
#[clap(short, long)]
json: bool,
}
#[derive(Debug, Serialize, Deserialize)]
struct Keys {
@ -39,30 +53,27 @@ impl Keys {
Keys { public, private }
}
fn write_to_file(&self, path: &str, json: bool) {
fn write_to_file(&self, mut path: PathBuf, json: bool) {
if json {
let path = if !path.ends_with(".json") {
format!("{}.json", path)
} else {
path.into()
};
if !path.ends_with(".json") {
path.set_extension("json");
}
let file =
File::create(&path).unwrap_or_else(|_| panic!("Failed to create file at {}", path));
let file = File::create(&path)
.unwrap_or_else(|_| panic!("Failed to create file at {}", path.display()));
serde_json::to_writer(file, &self)
.unwrap_or_else(|_| panic!("Failed to write to file at {}", path));
.unwrap_or_else(|_| panic!("Failed to write to file at {}", path.display()));
} else {
use std::io::Write;
use std::path::PathBuf;
let mut private = PathBuf::from(path);
let mut private = path.clone();
private.set_extension("prv");
let mut file = File::create(&private)
.unwrap_or_else(|_| panic!("Failed to create file at {}", private.display()));
file.write_all(&self.private.0)
.unwrap_or_else(|_| panic!("Failed to write to file at {}", private.display()));
let mut public = PathBuf::from(path);
let mut public = path.clone();
public.set_extension("pub");
let mut file = File::create(&public)
.unwrap_or_else(|_| panic!("Failed to create file at {}", public.display()));
@ -73,29 +84,9 @@ impl Keys {
}
fn main() {
let matches = App::new("Generate the keys to be used with the sodium element")
.version("1.0")
.author("Jordan Petridis <jordan@centricular.com>")
.about("Generate a pair of Sodium's crypto_box_curve25519xsalsa20poly1305 keys.")
.arg(
Arg::with_name("path")
.long("path")
.short("p")
.value_name("FILE")
.help("Path to write the Keys")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("json")
.long("json")
.short("j")
.help("Write a JSON file instead of a key.prv/key.pub pair"),
)
.get_matches();
let args = Args::parse();
let keys = Keys::new();
let path = matches.value_of("path").unwrap();
keys.write_to_file(path, matches.is_present("json"));
keys.write_to_file(args.path, args.json);
}