2022-11-23 05:59:54 -05:00
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-11-25 07:07:04 -05:00
|
|
|
use std::process::Command;
|
2022-11-23 05:59:54 -05:00
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
pub struct Signer {
|
|
|
|
pub private_key: PathBuf,
|
|
|
|
pub public_key: PathBuf,
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
impl Signer {
|
|
|
|
pub fn new(public_key: &Path, private_key: &Path) -> Self {
|
2022-11-23 05:59:54 -05:00
|
|
|
Self {
|
2022-11-25 07:07:04 -05:00
|
|
|
public_key: public_key.into(),
|
|
|
|
private_key: private_key.into(),
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sign_file(&self, filepath: &Path) -> Result<()> {
|
|
|
|
let args = vec![
|
|
|
|
String::from("--key"),
|
|
|
|
String::from(self.private_key.to_str().unwrap()),
|
|
|
|
String::from("--cert"),
|
|
|
|
String::from(self.public_key.to_str().unwrap()),
|
2022-11-24 21:04:44 -05:00
|
|
|
String::from(filepath.to_str().unwrap()),
|
|
|
|
String::from("--output"),
|
2022-11-25 07:07:04 -05:00
|
|
|
String::from(filepath.to_str().unwrap()),
|
2022-11-23 05:59:54 -05:00
|
|
|
];
|
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
let status = Command::new("sbsign").args(&args).status()?;
|
2022-11-23 05:59:54 -05:00
|
|
|
|
|
|
|
if !status.success() {
|
2022-11-25 07:07:04 -05:00
|
|
|
return Err(
|
|
|
|
anyhow::anyhow!("Failed to sign with sbsign with args `{:?}`", &args).into(),
|
|
|
|
);
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|