diff --git a/src/cmd_config.rs b/src/cmd_config.rs index d7b64fb..8e2894c 100644 --- a/src/cmd_config.rs +++ b/src/cmd_config.rs @@ -74,7 +74,7 @@ pub fn config(_cmd_version: CmdConfig) -> XResult<()> { let mut table = Table::new(config_profiles); table.with(Style::modern()); - println!("{}", table.to_string()); + println!("{}", table); Ok(()) } \ No newline at end of file diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index 2672bef..5e6f594 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -160,7 +160,7 @@ fn decrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8 let mut total_len = 0; let mut buffer = [0u8; 1024 * 8]; let key = opt_result!(key.try_into(), "Key is not 32 bytes: {}"); - let mut decryptor = aes_gcm_stream::Aes256GcmStreamDecryptor::new(key, &nonce); + let mut decryptor = aes_gcm_stream::Aes256GcmStreamDecryptor::new(key, nonce); let mut gz_decoder = GzStreamDecoder::new(); loop { let len = opt_result!(file_in.read(&mut buffer), "Read file failed: {}"); @@ -202,9 +202,7 @@ fn try_decrypt_key(config: &Option, TinyEncryptEnvelopType::PgpX25519 => try_decrypt_key_ecdh_pgp_x25519(envelop, pin), TinyEncryptEnvelopType::Ecdh => try_decrypt_key_ecdh(config, envelop, pin, ENC_AES256_GCM_P256, slot), TinyEncryptEnvelopType::EcdhP384 => try_decrypt_key_ecdh(config, envelop, pin, ENC_AES256_GCM_P384, slot), - unknown_type => { - return simple_error!("Unknown or not supported type: {}", unknown_type.get_name()); - } + unknown_type => simple_error!("Unknown or not supported type: {}", unknown_type.get_name()), } } @@ -233,7 +231,7 @@ fn try_decrypt_key_ecdh(config: &Option, ); let shared_secret = opt_result!(decrypt_data( &mut yk, - &epk_bytes, + epk_bytes, algo_id, slot_id, ), "Decrypt via PIV card failed: {}"); @@ -274,7 +272,7 @@ fn try_decrypt_key_pgp(envelop: &TinyEncryptEnvelop, pin: &Option) -> XR let pgp_envelop = &envelop.encrypted_key; debugging!("PGP envelop: {}", &pgp_envelop); - let pgp_envelop_bytes = opt_result!(util::decode_base64(&pgp_envelop), "Decode PGP envelop failed: {}"); + let pgp_envelop_bytes = opt_result!(util::decode_base64(pgp_envelop), "Decode PGP envelop failed: {}"); let key = trans.decipher(Cryptogram::RSA(&pgp_envelop_bytes))?; Ok(key) diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index 2e985eb..745739f 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -113,7 +113,7 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en util::require_file_not_exists(path_out.as_str())?; let (key, nonce) = util::make_key256_and_nonce(); - let envelops = encrypt_envelops(&key, &envelops)?; + let envelops = encrypt_envelops(&key, envelops)?; let encrypted_comment = match &cmd_encrypt.encrypted_comment { None => None, @@ -200,7 +200,7 @@ fn encrypt_file(file_in: &mut File, file_out: &mut File, key: &[u8], nonce: &[u8 GzStreamEncoder::new(Compression::new(*compress_level)) } }; - let mut encryptor = aes_gcm_stream::Aes256GcmStreamEncryptor::new(key, &nonce); + let mut encryptor = aes_gcm_stream::Aes256GcmStreamEncryptor::new(key, nonce); loop { let len = opt_result!(file_in.read(&mut buffer), "Read file failed: {}"); if len == 0 { @@ -292,7 +292,7 @@ fn encrypt_envelop_shared_secret(key: &[u8], header: WrapKeyHeader { kid: None, // Some(envelop.kid.clone()), enc: enc_type.to_string(), - e_pub_key: util::encode_base64_url_no_pad(&ephemeral_spki), + e_pub_key: util::encode_base64_url_no_pad(ephemeral_spki), }, nonce, encrypted_data: encrypted_key, diff --git a/src/cmd_info.rs b/src/cmd_info.rs index de8e306..28d840f 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -81,12 +81,12 @@ pub fn info_single(path: &PathBuf, cmd_info: &CmdInfo) -> XResult<()> { )); }) ); - meta.pgp_fingerprint.map(|fingerprint| { + if let Some(fingerprint) = meta.pgp_fingerprint { infos.push(format!("{}: {}", header("PGP fingerprint"), fingerprint)); - }); - meta.comment.map(|comment| { + } + if let Some(comment) = meta.comment { infos.push(format!("{}: {}", header("Comment"), comment)); - }); + } infos.push(format!("{}: {}", header("Encrypted comment"), to_yes_or_no(&meta.encrypted_comment))); infos.push(format!("{}: {}", header("Encrypted meta"), to_yes_or_no(&meta.encrypted_meta))); let encryption_algorithm = if let Some(encryption_algorithm) = &meta.encryption_algorithm { diff --git a/src/config.rs b/src/config.rs index 0e2b6db..7605a4c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,12 +57,12 @@ impl TinyEncryptConfig { serde_json::from_str(&config_contents),"Parse file: {}, failed: {}", file); let mut splitted_profiles = HashMap::new(); for (k, v) in config.profiles.into_iter() { - if !k.contains(",") { + if !k.contains(',') { splitted_profiles.insert(k, v); } else { - k.split(",") + k.split(',') .map(|k| k.trim()) - .filter(|k| k.len() > 0) + .filter(|k| !k.is_empty()) .for_each(|k| { splitted_profiles.insert(k.to_string(), v.clone()); }); @@ -73,11 +73,11 @@ impl TinyEncryptConfig { } pub fn find_first_arg_by_kid(&self, kid: &str) -> Option<&String> { - self.find_args_by_kid(kid).map(|a| a.iter().next()).flatten() + self.find_args_by_kid(kid).and_then(|a| a.iter().next()) } pub fn find_args_by_kid(&self, kid: &str) -> Option<&Vec> { - self.find_by_kid(kid).map(|e| e.args.as_ref()).flatten() + self.find_by_kid(kid).and_then(|e| e.args.as_ref()) } pub fn find_by_kid(&self, kid: &str) -> Option<&TinyEncryptConfigEnvelop> { @@ -103,7 +103,7 @@ impl TinyEncryptConfig { } } let mut envelops: Vec<_> = matched_envelops_map.values() - .map(|envelop| *envelop) + .copied() .collect(); if envelops.is_empty() { return simple_error!("Profile: {} has no valid envelopes found", profile); diff --git a/src/spec.rs b/src/spec.rs index 3ff19a0..a46df17 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -1,15 +1,15 @@ use std::fs::Metadata; -use flate2::Compression; +use flate2::Compression; use rust_util::{opt_result, util_time, XResult}; use rust_util::util_time::get_millis; use serde::{Deserialize, Serialize}; -use crate::{compress, crypto_aes}; +use crate::{compress, crypto_aes}; use crate::util::{encode_base64, get_user_agent, SALT_META, TINY_ENC_AES_GCM}; -pub const TINY_ENCRYPT_VERSION_10: &'static str = "1.0"; -pub const TINY_ENCRYPT_VERSION_11: &'static str = "1.1"; +pub const TINY_ENCRYPT_VERSION_10: &str = "1.0"; +pub const TINY_ENCRYPT_VERSION_11: &str = "1.1"; /// Specification: [Tiny Encrypt Spec V1.1](https://git.hatter.ink/hatter/tiny-encrypt-java/src/branch/master/TinyEncryptSpecV1.1.md) #[derive(Clone, Debug, Serialize, Deserialize)] @@ -105,7 +105,7 @@ impl EncEncryptedMeta { decrypted = opt_result!(compress::decompress(&decrypted), "Decode faield: {}"); let meta = opt_result!( serde_json::from_slice::(&decrypted), "Parse failed: {}"); - return Ok(meta); + Ok(meta) } pub fn seal(&self, key: &[u8], nonce: &[u8]) -> XResult> { diff --git a/src/util.rs b/src/util.rs index 802fb29..3193905 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,6 @@ use std::{fs, io}; use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::Path; use base64::Engine; use base64::engine::general_purpose; @@ -22,10 +22,10 @@ pub const TINY_ENC_COMPRESSED_MAGIC_TAG: u16 = 0x02; pub const SALT_COMMENT: &[u8] = b"salt:comment"; pub const SALT_META: &[u8] = b"salt:meta"; -pub fn get_file_name(path: &PathBuf) -> String { +pub fn get_file_name(path: &Path) -> String { let path_display = format!("{}", path.display()); - if path_display.contains("/") { - if let Some(p) = path_display.split("/").last() { + if path_display.contains('/') { + if let Some(p) = path_display.split('/').last() { return p.to_string(); } } @@ -80,8 +80,7 @@ pub fn simple_kdf(input: &[u8]) -> Vec { let input = hex::decode(sha256::digest(input)).unwrap(); let input = hex::decode(sha256::digest(input)).unwrap(); let input = hex::decode(sha256::digest(input)).unwrap(); - let input = hex::decode(sha256::digest(input)).unwrap(); - input + hex::decode(sha256::digest(input)).unwrap() } pub fn decode_base64(input: &str) -> XResult> { diff --git a/src/util_ecdh.rs b/src/util_ecdh.rs index 5898699..deab2bd 100644 --- a/src/util_ecdh.rs +++ b/src/util_ecdh.rs @@ -24,7 +24,7 @@ use p256::elliptic_curve::sec1::FromEncodedPoint; pub fn compute_shared_secret(public_key_point_hex: &str) -> XResult<(Vec, Vec)> { let public_key_point_bytes = opt_result!(hex::decode(public_key_point_hex), "Parse public key point hex failed: {}"); - let encoded_point = opt_result!(EncodedPoint::from_bytes(&public_key_point_bytes), "Parse public key point failed: {}"); + let encoded_point = opt_result!(EncodedPoint::from_bytes(public_key_point_bytes), "Parse public key point failed: {}"); let public_key = PublicKey::from_encoded_point(&encoded_point).unwrap(); let esk = EphemeralSecret::random(&mut OsRng); diff --git a/src/util_p384.rs b/src/util_p384.rs index 73b0209..aaf2ad2 100644 --- a/src/util_p384.rs +++ b/src/util_p384.rs @@ -8,7 +8,7 @@ use p384::elliptic_curve::sec1::FromEncodedPoint; pub fn compute_p384_shared_secret(public_key_point_hex: &str) -> XResult<(Vec, Vec)> { let public_key_point_bytes = opt_result!(hex::decode(public_key_point_hex), "Parse public key point hex failed: {}"); - let encoded_point = opt_result!(EncodedPoint::from_bytes(&public_key_point_bytes), "Parse public key point failed: {}"); + let encoded_point = opt_result!(EncodedPoint::from_bytes(public_key_point_bytes), "Parse public key point failed: {}"); let public_key = PublicKey::from_encoded_point(&encoded_point).unwrap(); let esk = EphemeralSecret::random(&mut OsRng); diff --git a/src/wrap_key.rs b/src/wrap_key.rs index ed2f543..0ce392f 100644 --- a/src/wrap_key.rs +++ b/src/wrap_key.rs @@ -37,7 +37,7 @@ impl WrapKey { if !wk.starts_with("WK:") { return simple_error!("Wrap key string must starts with WK:"); } - let wks = wk.split(".").collect::>(); + let wks = wk.split('.').collect::>(); if wks.len() != 3 { return simple_error!("Invalid wrap key."); }