feat: fix clippy

This commit is contained in:
2023-10-13 21:49:59 +08:00
parent d1cc03cf2f
commit 7fa49bdf05
9 changed files with 35 additions and 26 deletions

View File

@@ -7,7 +7,7 @@ use tabled::{Table, Tabled};
use tabled::settings::Style; use tabled::settings::Style;
use crate::config::TinyEncryptConfig; use crate::config::TinyEncryptConfig;
use crate::util::TINY_ENC_CONFIG_FILE; use crate::consts::TINY_ENC_CONFIG_FILE;
#[derive(Tabled, Eq)] #[derive(Tabled, Eq)]
struct ConfigProfile { struct ConfigProfile {

View File

@@ -22,7 +22,7 @@ use crate::compress::GzStreamDecoder;
use crate::config::TinyEncryptConfig; use crate::config::TinyEncryptConfig;
use crate::crypto_aes::{aes_gcm_decrypt, try_aes_gcm_decrypt_with_salt}; use crate::crypto_aes::{aes_gcm_decrypt, try_aes_gcm_decrypt_with_salt};
use crate::spec::{EncEncryptedMeta, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta}; 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; use crate::wrap_key::WrapKey;
#[derive(Debug, Args)] #[derive(Debug, Args)]

View File

@@ -16,7 +16,7 @@ use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
use crate::crypto_aes::{aes_gcm_encrypt, aes_gcm_encrypt_with_salt}; use crate::crypto_aes::{aes_gcm_encrypt, aes_gcm_encrypt_with_salt};
use crate::crypto_rsa::parse_spki; use crate::crypto_rsa::parse_spki;
use crate::spec::{EncEncryptedMeta, EncMetadata, TINY_ENCRYPT_VERSION_10, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta}; 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}; use crate::wrap_key::{WrapKey, WrapKeyHeader};
#[derive(Debug, Args)] #[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<u64> { fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_encrypt: &CmdEncrypt) -> XResult<u64> {
let path_display = format!("{}", path.display()); 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); information!("Tiny enc file skipped: {}", path_display);
return Ok(0); 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 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())?; util::require_file_not_exists(path_out.as_str())?;
let (key, nonce) = util::make_key256_and_nonce(); let (key, nonce) = util::make_key256_and_nonce();

View File

@@ -8,8 +8,8 @@ use clap::Args;
use rust_util::{iff, opt_result, simple_error, success, util_time, warning, XResult}; use rust_util::{iff, opt_result, simple_error, success, util_time, warning, XResult};
use simpledateformat::format_human2; use simpledateformat::format_human2;
use crate::{file, util}; use crate::file;
use crate::util::TINY_ENC_FILE_EXT; use crate::consts::{TINY_ENC_AES_GCM, TINY_ENC_FILE_EXT};
#[derive(Debug, Args)] #[derive(Debug, Args)]
pub struct CmdInfo { 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 { let encryption_algorithm = if let Some(encryption_algorithm) = &meta.encryption_algorithm {
encryption_algorithm.to_string() encryption_algorithm.to_string()
} else { } else {
format!("{} (default)", util::TINY_ENC_AES_GCM) format!("{} (default)", TINY_ENC_AES_GCM)
}; };
infos.push(format!("{}: {}", header("Encryption algorithm"), encryption_algorithm)); infos.push(format!("{}: {}", header("Encryption algorithm"), encryption_algorithm));

18
src/consts.rs Normal file
View File

@@ -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";

View File

@@ -3,11 +3,12 @@ use std::io::{Read, Write};
use flate2::Compression; use flate2::Compression;
use rust_util::{debugging, iff, opt_result, simple_error, XResult}; 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; use crate::spec::TinyEncryptMeta;
pub fn write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta, compress_meta: bool) -> XResult<usize> { pub fn write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta, compress_meta: bool) -> XResult<usize> {
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: {}"); 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: {}"); let mut encrypted_meta_bytes = opt_result!(serde_json::to_vec(&meta), "Generate meta json bytes failed: {}");
if compress_meta { if compress_meta {
@@ -32,8 +33,8 @@ pub fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
let mut tag_buff = [0_u8; 2]; let mut tag_buff = [0_u8; 2];
opt_result!(r.read_exact(&mut tag_buff), "Read tag failed: {}"); opt_result!(r.read_exact(&mut tag_buff), "Read tag failed: {}");
let tag = u16::from_be_bytes(tag_buff); let tag = u16::from_be_bytes(tag_buff);
let is_normal_tiny_enc = tag == util::TINY_ENC_MAGIC_TAG; let is_normal_tiny_enc = tag == TINY_ENC_MAGIC_TAG;
let is_compressed_tiny_enc = tag == util::TINY_ENC_COMPRESSED_MAGIC_TAG; let is_compressed_tiny_enc = tag == TINY_ENC_COMPRESSED_MAGIC_TAG;
if !is_normal_tiny_enc && !is_compressed_tiny_enc { if !is_normal_tiny_enc && !is_compressed_tiny_enc {
return simple_error!("Tag is not 0x01 or 0x02, but is: 0x{:x}", tag); return simple_error!("Tag is not 0x01 or 0x02, but is: 0x{:x}", tag);
} }

View File

@@ -9,6 +9,7 @@ use crate::cmd_encrypt::CmdEncrypt;
use crate::cmd_info::CmdInfo; use crate::cmd_info::CmdInfo;
use crate::cmd_version::CmdVersion; use crate::cmd_version::CmdVersion;
mod consts;
mod util; mod util;
mod util_piv; mod util_piv;
mod util_ecdh; mod util_ecdh;

View File

@@ -6,7 +6,8 @@ use rust_util::util_time::get_millis;
use serde::{Deserialize, Serialize}; 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}; 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_10: &str = "1.0";
pub const TINY_ENCRYPT_VERSION_11: &str = "1.1"; pub const TINY_ENCRYPT_VERSION_11: &str = "1.1";

View File

@@ -8,19 +8,7 @@ use rand::random;
use rust_util::{simple_error, warning, XResult}; use rust_util::{simple_error, warning, XResult};
use zeroize::Zeroize; use zeroize::Zeroize;
pub const ENC_AES256_GCM_P256: &str = "aes256-gcm-p256"; use crate::consts::TINY_ENC_FILE_EXT;
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";
pub fn get_file_name(path: &Path) -> String { pub fn get_file_name(path: &Path) -> String {
let path_display = format!("{}", path.display()); let path_display = format!("{}", path.display());