From 7fa49bdf05c95bcbce5fb5d66b92123bc2beafeb Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 13 Oct 2023 21:49:59 +0800 Subject: [PATCH] feat: fix clippy --- src/cmd_config.rs | 2 +- src/cmd_decrypt.rs | 2 +- src/cmd_encrypt.rs | 6 +++--- src/cmd_info.rs | 6 +++--- src/consts.rs | 18 ++++++++++++++++++ src/file.rs | 9 +++++---- src/main.rs | 1 + src/spec.rs | 3 ++- src/util.rs | 14 +------------- 9 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 src/consts.rs diff --git a/src/cmd_config.rs b/src/cmd_config.rs index 80fda28..fd0f9c9 100644 --- a/src/cmd_config.rs +++ b/src/cmd_config.rs @@ -7,7 +7,7 @@ use tabled::{Table, Tabled}; use tabled::settings::Style; use crate::config::TinyEncryptConfig; -use crate::util::TINY_ENC_CONFIG_FILE; +use crate::consts::TINY_ENC_CONFIG_FILE; #[derive(Tabled, Eq)] struct ConfigProfile { diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index 5e6f594..5e67f80 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -22,7 +22,7 @@ use crate::compress::GzStreamDecoder; use crate::config::TinyEncryptConfig; use crate::crypto_aes::{aes_gcm_decrypt, try_aes_gcm_decrypt_with_salt}; use crate::spec::{EncEncryptedMeta, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta}; -use crate::util::{ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT}; +use crate::consts::{ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT}; use crate::wrap_key::WrapKey; #[derive(Debug, Args)] diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index 745739f..16ff511 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -16,7 +16,7 @@ use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop}; use crate::crypto_aes::{aes_gcm_encrypt, aes_gcm_encrypt_with_salt}; use crate::crypto_rsa::parse_spki; use crate::spec::{EncEncryptedMeta, EncMetadata, TINY_ENCRYPT_VERSION_10, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta}; -use crate::util::{ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE}; +use crate::consts::{ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT}; use crate::wrap_key::{WrapKey, WrapKeyHeader}; #[derive(Debug, Args)] @@ -100,7 +100,7 @@ pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> { fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult { let path_display = format!("{}", path.display()); - if path_display.ends_with(util::TINY_ENC_FILE_EXT) { + if path_display.ends_with(TINY_ENC_FILE_EXT) { information!("Tiny enc file skipped: {}", path_display); return Ok(0); } @@ -109,7 +109,7 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display); - let path_out = format!("{}{}", path_display, util::TINY_ENC_FILE_EXT); + let path_out = format!("{}{}", path_display, TINY_ENC_FILE_EXT); util::require_file_not_exists(path_out.as_str())?; let (key, nonce) = util::make_key256_and_nonce(); diff --git a/src/cmd_info.rs b/src/cmd_info.rs index 3e1dd90..44e18e1 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -8,8 +8,8 @@ use clap::Args; use rust_util::{iff, opt_result, simple_error, success, util_time, warning, XResult}; use simpledateformat::format_human2; -use crate::{file, util}; -use crate::util::TINY_ENC_FILE_EXT; +use crate::file; +use crate::consts::{TINY_ENC_AES_GCM, TINY_ENC_FILE_EXT}; #[derive(Debug, Args)] pub struct CmdInfo { @@ -93,7 +93,7 @@ pub fn info_single(path: &PathBuf, cmd_info: &CmdInfo) -> XResult<()> { let encryption_algorithm = if let Some(encryption_algorithm) = &meta.encryption_algorithm { encryption_algorithm.to_string() } else { - format!("{} (default)", util::TINY_ENC_AES_GCM) + format!("{} (default)", TINY_ENC_AES_GCM) }; infos.push(format!("{}: {}", header("Encryption algorithm"), encryption_algorithm)); diff --git a/src/consts.rs b/src/consts.rs new file mode 100644 index 0000000..81f67f8 --- /dev/null +++ b/src/consts.rs @@ -0,0 +1,18 @@ +// AES-GCM-ECDH Algorithms +pub const ENC_AES256_GCM_P256: &str = "aes256-gcm-p256"; +pub const ENC_AES256_GCM_P384: &str = "aes256-gcm-p384"; +pub const ENC_AES256_GCM_X25519: &str = "aes256-gcm-x25519"; + +// Extend and config file +pub const TINY_ENC_FILE_EXT: &str = ".tinyenc"; +pub const TINY_ENC_CONFIG_FILE: &str = "~/.tinyencrypt/config-rs.json"; + +pub const TINY_ENC_AES_GCM: &str = "AES/GCM"; + +// Tiny enc magic tag +pub const TINY_ENC_MAGIC_TAG: u16 = 0x01; +pub const TINY_ENC_COMPRESSED_MAGIC_TAG: u16 = 0x02; + +// Encryption nonce salt +pub const SALT_COMMENT: &[u8] = b"salt:comment"; +pub const SALT_META: &[u8] = b"salt:meta"; diff --git a/src/file.rs b/src/file.rs index b286a2c..f27b1c9 100644 --- a/src/file.rs +++ b/src/file.rs @@ -3,11 +3,12 @@ use std::io::{Read, Write}; use flate2::Compression; use rust_util::{debugging, iff, opt_result, simple_error, XResult}; -use crate::{compress, util}; +use crate::compress; +use crate::consts::{TINY_ENC_COMPRESSED_MAGIC_TAG, TINY_ENC_MAGIC_TAG}; use crate::spec::TinyEncryptMeta; pub fn write_tiny_encrypt_meta(w: &mut W, meta: &TinyEncryptMeta, compress_meta: bool) -> XResult { - let tag = iff!(compress_meta, util::TINY_ENC_COMPRESSED_MAGIC_TAG, util::TINY_ENC_MAGIC_TAG); + let tag = iff!(compress_meta, TINY_ENC_COMPRESSED_MAGIC_TAG, TINY_ENC_MAGIC_TAG); opt_result!(w.write_all(&tag.to_be_bytes()), "Write tag failed: {}"); let mut encrypted_meta_bytes = opt_result!(serde_json::to_vec(&meta), "Generate meta json bytes failed: {}"); if compress_meta { @@ -32,8 +33,8 @@ pub fn read_tiny_encrypt_meta(r: &mut R) -> XResult { let mut tag_buff = [0_u8; 2]; opt_result!(r.read_exact(&mut tag_buff), "Read tag failed: {}"); let tag = u16::from_be_bytes(tag_buff); - let is_normal_tiny_enc = tag == util::TINY_ENC_MAGIC_TAG; - let is_compressed_tiny_enc = tag == util::TINY_ENC_COMPRESSED_MAGIC_TAG; + let is_normal_tiny_enc = tag == TINY_ENC_MAGIC_TAG; + let is_compressed_tiny_enc = tag == TINY_ENC_COMPRESSED_MAGIC_TAG; if !is_normal_tiny_enc && !is_compressed_tiny_enc { return simple_error!("Tag is not 0x01 or 0x02, but is: 0x{:x}", tag); } diff --git a/src/main.rs b/src/main.rs index 4edee69..e954323 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use crate::cmd_encrypt::CmdEncrypt; use crate::cmd_info::CmdInfo; use crate::cmd_version::CmdVersion; +mod consts; mod util; mod util_piv; mod util_ecdh; diff --git a/src/spec.rs b/src/spec.rs index a46df17..bcf04d6 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -6,7 +6,8 @@ use rust_util::util_time::get_millis; use serde::{Deserialize, Serialize}; use crate::{compress, crypto_aes}; -use crate::util::{encode_base64, get_user_agent, SALT_META, TINY_ENC_AES_GCM}; +use crate::consts::{SALT_META, TINY_ENC_AES_GCM}; +use crate::util::{encode_base64, get_user_agent}; pub const TINY_ENCRYPT_VERSION_10: &str = "1.0"; pub const TINY_ENCRYPT_VERSION_11: &str = "1.1"; diff --git a/src/util.rs b/src/util.rs index 3193905..2956914 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,19 +8,7 @@ use rand::random; use rust_util::{simple_error, warning, XResult}; use zeroize::Zeroize; -pub const ENC_AES256_GCM_P256: &str = "aes256-gcm-p256"; -pub const ENC_AES256_GCM_P384: &str = "aes256-gcm-p384"; -pub const ENC_AES256_GCM_X25519: &str = "aes256-gcm-x25519"; -pub const TINY_ENC_FILE_EXT: &str = ".tinyenc"; -pub const TINY_ENC_CONFIG_FILE: &str = "~/.tinyencrypt/config-rs.json"; - -pub const TINY_ENC_AES_GCM: &str = "AES/GCM"; - -pub const TINY_ENC_MAGIC_TAG: u16 = 0x01; -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"; +use crate::consts::TINY_ENC_FILE_EXT; pub fn get_file_name(path: &Path) -> String { let path_display = format!("{}", path.display());