sign.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::path::{Path, PathBuf};
  5. use crate::{
  6. helpers::updater_signature::{read_key_from_file, secret_key, sign_file},
  7. Result,
  8. };
  9. use anyhow::Context;
  10. use base64::Engine;
  11. use clap::Parser;
  12. use tauri_utils::display_path;
  13. #[derive(Debug, Parser)]
  14. #[clap(about = "Sign a file")]
  15. pub struct Options {
  16. /// Load the private key from a file
  17. #[clap(short = 'k', long, conflicts_with("private_key_path"))]
  18. private_key: Option<String>,
  19. /// Load the private key from a string
  20. #[clap(short = 'f', long, conflicts_with("private_key"))]
  21. private_key_path: Option<PathBuf>,
  22. /// Set private key password when signing
  23. #[clap(short, long)]
  24. password: Option<String>,
  25. /// Sign the specified file
  26. file: PathBuf,
  27. }
  28. pub fn command(mut options: Options) -> Result<()> {
  29. options.private_key = if let Some(private_key) = options.private_key_path {
  30. Some(read_key_from_file(Path::new(&private_key)).expect("Unable to extract private key"))
  31. } else {
  32. options.private_key
  33. };
  34. let private_key = if let Some(pk) = options.private_key {
  35. pk
  36. } else {
  37. return Err(anyhow::anyhow!(
  38. "Key generation aborted: Unable to find the private key".to_string(),
  39. ));
  40. };
  41. if options.password.is_none() {
  42. println!("Signing without password.");
  43. }
  44. let (manifest_dir, signature) =
  45. sign_file(&secret_key(private_key, options.password)?, options.file)
  46. .with_context(|| "failed to sign file")?;
  47. println!(
  48. "\nYour file was signed successfully, You can find the signature here:\n{}\n\nPublic signature:\n{}\n\nMake sure to include this into the signature field of your update server.",
  49. display_path(manifest_dir),
  50. base64::engine::general_purpose::STANDARD.encode(signature.to_string())
  51. );
  52. Ok(())
  53. }