diff --git a/script-meta-v2.json b/script-meta-v2.json index cb91887..6df4d23 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -90,11 +90,11 @@ }, "script-sign.rs": { "script_name": "script-sign.rs", - "script_length": 16506, - "script_sha256": "d8dc82553a5300ecee3364ed04596163295139f8b18e0d1a95b89ea6540ebfe5", + "script_length": 4457, + "script_sha256": "9b688c4446a13d0d0013220a41984fdb4288c99bc5d8c80b641ec7ac04f7a6c0", "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-sign-rs/src/main.rs", "publish_time": 1737272563311, - "update_time": 1737272563311 + "update_time": 1737481447995 }, "sync.rs": { "script_name": "sync.rs", diff --git a/script-meta.json b/script-meta.json index 379506f..a27d9a0 100644 --- a/script-meta.json +++ b/script-meta.json @@ -51,8 +51,8 @@ }, "script-sign-rs": { "script_name": "script-sign-rs", - "script_length": 16506, - "script_sha256": "d8dc82553a5300ecee3364ed04596163295139f8b18e0d1a95b89ea6540ebfe5" + "script_length": 4457, + "script_sha256": "9b688c4446a13d0d0013220a41984fdb4288c99bc5d8c80b641ec7ac04f7a6c0" }, "sync-rs": { "script_name": "sync-rs", diff --git a/script-sign-rs/Cargo.lock b/script-sign-rs/Cargo.lock index b9bbf16..33a98a3 100644 --- a/script-sign-rs/Cargo.lock +++ b/script-sign-rs/Cargo.lock @@ -644,11 +644,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] -name = "script-sign-rs" +name = "script-sign" version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187af8146515de8a5cde0828910084742ebd4e8e5898a51aea83eeb3099d482e" dependencies = [ "base64", - "clap", "digest", "ecdsa", "hex", @@ -662,6 +663,15 @@ dependencies = [ "simpledateformat", ] +[[package]] +name = "script-sign-rs" +version = "0.1.0" +dependencies = [ + "clap", + "rust_util", + "script-sign", +] + [[package]] name = "sec1" version = "0.7.3" diff --git a/script-sign-rs/Cargo.toml b/script-sign-rs/Cargo.toml index e7485bd..06b288b 100644 --- a/script-sign-rs/Cargo.toml +++ b/script-sign-rs/Cargo.toml @@ -4,16 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -base64 = "0.22.1" clap = { version = "4.5.23", features = ["derive"] } -digest = "0.10.7" -ecdsa = "0.16.9" -hex = "0.4.3" -p256 = "0.13.2" -p384 = "0.13.0" -regex = "1.11.1" rust_util = "0.6.47" -serde = { version = "1.0.217", features = ["derive"] } -serde_json = "1.0.134" -sha2 = "0.10.8" -simpledateformat = "0.1.4" +script-sign = "0.1" diff --git a/script-sign-rs/src/main.rs b/script-sign-rs/src/main.rs index acbb557..cf4c927 100755 --- a/script-sign-rs/src/main.rs +++ b/script-sign-rs/src/main.rs @@ -2,43 +2,17 @@ //! ```cargo //! [dependencies] -//! base64 = "0.22.1" //! clap = { version = "4.5.23", features = ["derive"] } -//! digest = "0.10.7" -//! ecdsa = "0.16.9" -//! hex = "0.4.3" -//! p256 = "0.13.2" -//! p384 = "0.13.0" -//! regex = "1.11.1" //! rust_util = "0.6.47" -//! serde = { version = "1.0.217", features = ["derive"] } -//! serde_json = "1.0.134" -//! sha2 = "0.10.8" -//! simpledateformat = "0.1.4" +//! script-sign = "0.1" //! ``` -use base64::engine::general_purpose::STANDARD as standard_base64; -use base64::Engine; use clap::Parser; -use digest::Digest; -use ecdsa::Signature; -use ecdsa::VerifyingKey; -use p256::ecdsa::signature::hazmat::PrehashVerifier; -use p256::NistP256; -use p384::NistP384; -use regex::Regex; -use rust_util::{ - debugging, failure, failure_and_exit, information, opt_result, opt_value_result, simple_error, - success, util_cmd, warning, XResult, -}; -use serde::{Deserialize, Serialize}; -use sha2::Sha256; -use std::collections::HashMap; +use rust_util::{failure, failure_and_exit, information, success, warning}; +use script_sign::{KeyMap, Script}; use std::fs; use std::path::PathBuf; -const SIGNATURE_PREFIX: &str = "// @SCRIPT-SIGNATURE-"; - /// Script signing tool #[derive(Parser, Debug)] #[command(version, about, long_about = None, bin_name = "script-sign.rs")] @@ -62,7 +36,7 @@ fn main() { failure_and_exit!("Argument --verify or --sign must assigned."); } - let key_map = KeyMap::default().unwrap(); + let key_map = KeyMap::system().unwrap(); let total_scripts = args.scripts.len(); for (i, script_path) in args.scripts.iter().enumerate() { information!( @@ -159,338 +133,3 @@ fn main() { } } } - -#[derive(Debug, Eq, PartialEq)] -enum ScriptSignatureAlgorithm { - RS256, - ES256, - ES384, - ES521, -} - -#[derive(Debug)] -struct ScriptSignature { - key_id: String, - algorithm: ScriptSignatureAlgorithm, - time: String, - signature: Vec, -} - -#[derive(Debug)] -struct Script { - content_lines: Vec, - signature: Option, -} - -#[derive(Debug, Serialize, Deserialize)] -struct KeyMap { - key_map: HashMap, -} - -#[allow(dead_code)] -#[derive(Debug, Deserialize)] -struct CardEcSignResult { - pub algorithm: String, - pub hash_hex: String, - pub signed_data_base64: String, - pub signed_data_hex: String, - pub slot: String, -} - -impl KeyMap { - fn default() -> XResult { - let signing_keys = r##" -{ - "yk-r1": "04dd3eebd906c9cf00b08ec29f7ed61804d1cc1d1352d9257b628191e08fc3717c4fae3298cd5c4829cec8bf3a946e7db60b7857e1287f6a0bae6b3f2342f007d0" -} -"##; - let key_map: HashMap = serde_json::from_str(signing_keys).unwrap(); - Ok(KeyMap { key_map }) - } - - fn find(&self, key_id: &str) -> Option<&String> { - self.key_map.get(key_id) - } -} - -impl ScriptSignatureAlgorithm { - fn try_from(algo: &str) -> XResult { - let upper_algo = algo.to_uppercase(); - Ok(match upper_algo.as_str() { - "RS256" => Self::RS256, - "ES256" => Self::ES256, - "ES384" => Self::ES384, - "ES521" => Self::ES521, - _ => return simple_error!("Not valid algorithm: {}", algo), - }) - } - - fn as_str(&self) -> &'static str { - match self { - ScriptSignatureAlgorithm::RS256 => "RS256", - ScriptSignatureAlgorithm::ES256 => "ES256", - ScriptSignatureAlgorithm::ES384 => "ES384", - ScriptSignatureAlgorithm::ES521 => "ES521", - } - } -} - -impl ScriptSignature { - fn parse(script_signature_line: &str) -> XResult { - // e.g. // @SCRIPT-SIGNATURE-V1: ..