From c3102882a9612f7d0963eb8a59a12c237201fcbc Mon Sep 17 00:00:00 2001 From: Alex Page Date: Sat, 24 Jun 2023 17:42:09 -0400 Subject: [PATCH] Use log crate to filter verbose messages --- Cargo.lock | 101 ++++++++++++++++++++++++++++++++++++++++------ umskt/src/key.rs | 2 - xpkey/Cargo.toml | 3 ++ xpkey/src/keys.rs | 9 +---- xpkey/src/main.rs | 68 +++++++++++++------------------ 5 files changed, 122 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e139075..015dce4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -140,7 +140,7 @@ checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -188,7 +188,7 @@ checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ "hermit-abi", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -200,7 +200,7 @@ dependencies = [ "hermit-abi", "io-lifetimes", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -221,6 +221,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" +[[package]] +name = "log" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" + [[package]] name = "once_cell" version = "1.18.0" @@ -297,7 +303,7 @@ dependencies = [ "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -337,6 +343,16 @@ dependencies = [ "serde", ] +[[package]] +name = "simple_logger" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230cd5c29b815c9b699fb610b49a5ed65588f3509d9f0108be3a885da629333" +dependencies = [ + "log", + "windows-sys 0.42.0", +] + [[package]] name = "strsim" version = "0.10.0" @@ -403,6 +419,21 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -418,51 +449,93 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.0" @@ -475,7 +548,9 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "log", "serde", "serde_json", + "simple_logger", "umskt", ] diff --git a/umskt/src/key.rs b/umskt/src/key.rs index 978ade9..4dea89b 100644 --- a/umskt/src/key.rs +++ b/umskt/src/key.rs @@ -59,9 +59,7 @@ mod tests { fn test_base24() { let input = "JTW3TJ7PFJ7V9CCMX84V9PFT8"; let unbase24 = super::base24_decode(input).unwrap(); - println!("{:?}", unbase24); let base24 = super::base24_encode(&unbase24).unwrap(); - println!("{}", base24); assert_eq!(input, base24); } } diff --git a/xpkey/Cargo.toml b/xpkey/Cargo.toml index 6708d07..16d7f27 100644 --- a/xpkey/Cargo.toml +++ b/xpkey/Cargo.toml @@ -15,3 +15,6 @@ anyhow = "1.0.71" clap = { version = "4.3.4", features = ["derive"] } serde = { version = "1.0.164", features = ["derive"] } serde_json = "1.0" +log = "0.4.19" +simple_logger = { version = "4.2.0", default-features = false } + diff --git a/xpkey/src/keys.rs b/xpkey/src/keys.rs index d0c7bdf..02ecfff 100644 --- a/xpkey/src/keys.rs +++ b/xpkey/src/keys.rs @@ -4,19 +4,14 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; use serde_json::{from_reader, from_str}; -pub fn load_keys + std::fmt::Display>( - path: Option

, - verbose: bool, -) -> Result { +pub fn load_keys + std::fmt::Display>(path: Option

) -> Result { let keys = { if let Some(path) = path { let file = File::open(&path)?; let reader = BufReader::new(file); let keys: Keys = from_reader(reader)?; - if verbose { - println!("Loaded keys from {}", path); - } + log::info!("Loaded keys from {}", path); keys } else { diff --git a/xpkey/src/main.rs b/xpkey/src/main.rs index 9a23592..f67906e 100644 --- a/xpkey/src/main.rs +++ b/xpkey/src/main.rs @@ -13,18 +13,23 @@ use crate::{cli::*, keys::load_keys}; fn main() -> Result<()> { let args = Cli::parse(); - let verbose = args.verbose; + + if args.verbose { + simple_logger::init_with_level(log::Level::Info)?; + } else { + simple_logger::init_with_level(log::Level::Warn)?; + } match &args.command { - Commands::List(args) => list(args, verbose), - Commands::Generate(args) => generate(args, verbose), - Commands::Validate(args) => validate(args, verbose), + Commands::List(args) => list(args), + Commands::Generate(args) => generate(args), + Commands::Validate(args) => validate(args), Commands::ConfirmationId(args) => confirmation_id(&args.instid), } } -fn list(args: &ListArgs, verbose: bool) -> Result<()> { - let keys = load_keys(args.keys_path.as_ref(), verbose)?; +fn list(args: &ListArgs) -> Result<()> { + let keys = load_keys(args.keys_path.as_ref())?; for (key, value) in keys.products.iter() { println!("{}: {:?}", key, value.bink); } @@ -33,12 +38,12 @@ fn list(args: &ListArgs, verbose: bool) -> Result<()> { Ok(()) } -fn generate(args: &GenerateArgs, verbose: bool) -> Result<()> { +fn generate(args: &GenerateArgs) -> Result<()> { if args.channel_id > 999 { bail!("Channel ID must be 3 digits or fewer"); } - let keys = load_keys(args.keys_path.as_ref(), verbose)?; + let keys = load_keys(args.keys_path.as_ref())?; let bink_id = args.bink_id.to_ascii_uppercase(); let bink = &keys.bink[&bink_id]; @@ -50,36 +55,36 @@ fn generate(args: &GenerateArgs, verbose: bool) -> Result<()> { // we need the result of the function K(x; y) = kG(x; y). let private_key = &bink.private; - let curve = initialize_curve(bink, &bink_id, verbose)?; + let curve = initialize_curve(bink, &bink_id)?; let private_key = PrivateKey::new(gen_order, private_key)?; if u32::from_str_radix(&bink_id, 16)? < 0x40 { - bink1998_generate(&curve, &private_key, args.channel_id, args.count, verbose)?; + bink1998_generate(&curve, &private_key, args.channel_id, args.count)?; } else { - bink2002_generate(&curve, &private_key, args.channel_id, args.count, verbose)?; + bink2002_generate(&curve, &private_key, args.channel_id, args.count)?; } Ok(()) } -fn validate(args: &ValidateArgs, verbose: bool) -> Result<()> { +fn validate(args: &ValidateArgs) -> Result<()> { // We can validate any given key using the available public key: {p, a, b, G, K}. // No private key or gen_order is required. - let keys = load_keys(args.keys_path.as_ref(), verbose)?; + let keys = load_keys(args.keys_path.as_ref())?; let bink_id = args.bink_id.to_ascii_uppercase(); let bink = &keys.bink[&bink_id]; - let curve = initialize_curve(bink, &bink_id, verbose)?; + let curve = initialize_curve(bink, &bink_id)?; if u32::from_str_radix(&bink_id, 16)? < 0x40 { - bink1998_validate(&curve, &args.key_to_check, verbose)?; + bink1998_validate(&curve, &args.key_to_check)?; } else { - bink2002_validate(&curve, &args.key_to_check, verbose)?; + bink2002_validate(&curve, &args.key_to_check)?; } Ok(()) } -fn initialize_curve(bink: &Bink, bink_id: &str, verbose: bool) -> Result { +fn initialize_curve(bink: &Bink, bink_id: &str) -> Result { let p = &bink.p; let a = &bink.a; let b = &bink.b; @@ -88,12 +93,7 @@ fn initialize_curve(bink: &Bink, bink_id: &str, verbose: bool) -> Result Result<()> { for _ in 0..count { let product_key = bink1998::ProductKey::new(curve, private_key, channel_id, None, None)?; - if verbose { - println!("{:?}", product_key); - } + log::info!("{:?}", product_key); println!("{product_key}"); } Ok(()) @@ -120,33 +117,26 @@ fn bink2002_generate( private_key: &PrivateKey, channel_id: u32, count: u64, - verbose: bool, ) -> Result<()> { for _ in 0..count { let product_key = bink2002::ProductKey::new(curve, private_key, channel_id, None, None)?; - if verbose { - println!("{:?}", product_key); - } + log::info!("{:?}", product_key); println!("{product_key}"); } Ok(()) } -fn bink1998_validate(curve: &EllipticCurve, key: &str, verbose: bool) -> Result<()> { +fn bink1998_validate(curve: &EllipticCurve, key: &str) -> Result<()> { let product_key = bink1998::ProductKey::from_key(curve, key)?; - if verbose { - println!("{:?}", product_key); - } + log::info!("{:?}", product_key); println!("{product_key}"); println!("Key validated successfully!"); Ok(()) } -fn bink2002_validate(curve: &EllipticCurve, key: &str, verbose: bool) -> Result<()> { +fn bink2002_validate(curve: &EllipticCurve, key: &str) -> Result<()> { let product_key = bink2002::ProductKey::from_key(curve, key)?; - if verbose { - println!("{:?}", product_key); - } + log::info!("{:?}", product_key); println!("{product_key}"); println!("Key validated successfully!"); Ok(())