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" smallvec = "1.0"
# example # example
clap = { version = "2.33", optional = true } clap = { version = "3", optional = true, features = ["derive"] }
serde = { version = "1.0", features = ["derive"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", 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::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use clap::{App, Arg}; use clap::Parser;
use serde::{Deserialize, Serialize}; 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)] #[derive(Debug, Serialize, Deserialize)]
struct Keys { struct Keys {
public: box_::PublicKey, public: box_::PublicKey,
@ -47,35 +60,11 @@ impl Keys {
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("Decrypt a gstsodium10 file.") let args = Args::parse();
.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();
gst::init()?; gst::init()?;
gstsodium::plugin_register_static().expect("Failed to register sodium plugin"); 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 receiver_keys = {
let mut r = PathBuf::new(); let mut r = PathBuf::new();
r.push(env!("CARGO_MANIFEST_DIR")); 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 typefind = gst::ElementFactory::make("typefind", None).unwrap();
let filesink = gst::ElementFactory::make("filesink", None).unwrap(); let filesink = gst::ElementFactory::make("filesink", None).unwrap();
filesrc.set_property("location", &input_loc); filesrc.set_property("location", &args.input);
filesink.set_property("location", &out_loc); filesink.set_property("location", &args.output);
decrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.private.0)); decrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.private.0));
decrypter.set_property("sender-key", glib::Bytes::from_owned(sender.public)); 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::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use clap::{App, Arg}; use clap::Parser;
use serde::{Deserialize, Serialize}; 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)] #[derive(Debug, Serialize, Deserialize)]
struct Keys { struct Keys {
public: box_::PublicKey, public: box_::PublicKey,
@ -47,35 +60,11 @@ impl Keys {
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("Encrypt a file with in the gstsodium10 format") let args = Args::parse();
.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();
gst::init()?; gst::init()?;
gstsodium::plugin_register_static().expect("Failed to register sodium plugin"); 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 receiver_keys = {
let mut r = PathBuf::new(); let mut r = PathBuf::new();
r.push(env!("CARGO_MANIFEST_DIR")); 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 encrypter = gst::ElementFactory::make("sodiumencrypter", None).unwrap();
let filesink = gst::ElementFactory::make("filesink", None).unwrap(); let filesink = gst::ElementFactory::make("filesink", None).unwrap();
filesrc.set_property("location", &input_loc); filesrc.set_property("location", &args.input);
filesink.set_property("location", &out_loc); filesink.set_property("location", &args.output);
encrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.public)); encrypter.set_property("receiver-key", glib::Bytes::from_owned(receiver.public));
encrypter.set_property("sender-key", glib::Bytes::from_owned(sender.private.0)); encrypter.set_property("sender-key", glib::Bytes::from_owned(sender.private.0));

View file

@ -22,10 +22,24 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use clap::{App, Arg}; use clap::Parser;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sodiumoxide::crypto::box_; use sodiumoxide::crypto::box_;
use std::fs::File; 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)] #[derive(Debug, Serialize, Deserialize)]
struct Keys { struct Keys {
@ -39,30 +53,27 @@ impl Keys {
Keys { public, private } Keys { public, private }
} }
fn write_to_file(&self, path: &str, json: bool) { fn write_to_file(&self, mut path: PathBuf, json: bool) {
if json { if json {
let path = if !path.ends_with(".json") { if !path.ends_with(".json") {
format!("{}.json", path) path.set_extension("json");
} else { }
path.into()
};
let file = let file = File::create(&path)
File::create(&path).unwrap_or_else(|_| panic!("Failed to create file at {}", path)); .unwrap_or_else(|_| panic!("Failed to create file at {}", path.display()));
serde_json::to_writer(file, &self) 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 { } else {
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
let mut private = PathBuf::from(path); let mut private = path.clone();
private.set_extension("prv"); private.set_extension("prv");
let mut file = File::create(&private) let mut file = File::create(&private)
.unwrap_or_else(|_| panic!("Failed to create file at {}", private.display())); .unwrap_or_else(|_| panic!("Failed to create file at {}", private.display()));
file.write_all(&self.private.0) file.write_all(&self.private.0)
.unwrap_or_else(|_| panic!("Failed to write to file at {}", private.display())); .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"); public.set_extension("pub");
let mut file = File::create(&public) let mut file = File::create(&public)
.unwrap_or_else(|_| panic!("Failed to create file at {}", public.display())); .unwrap_or_else(|_| panic!("Failed to create file at {}", public.display()));
@ -73,29 +84,9 @@ impl Keys {
} }
fn main() { fn main() {
let matches = App::new("Generate the keys to be used with the sodium element") let args = Args::parse();
.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 keys = Keys::new(); let keys = Keys::new();
let path = matches.value_of("path").unwrap(); keys.write_to_file(args.path, args.json);
keys.write_to_file(path, matches.is_present("json"));
} }