feat: update
This commit is contained in:
39
src/util.rs
39
src/util.rs
@@ -1,14 +1,42 @@
|
||||
use std::io;
|
||||
use std::{fs, io};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose;
|
||||
use rust_util::{warning, XResult};
|
||||
use rust_util::{opt_result, simple_error, warning, XResult};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
pub const ENC_AES256_GCM_P256: &str = "aes256-gcm-p256";
|
||||
pub const TINY_ENC_FILE_EXT: &str = ".tinyenc";
|
||||
pub const TINY_ENC_CONFIG_FILE: &str = "~/.tinyencrypt/config-rs.json";
|
||||
|
||||
pub fn require_tiny_enc_file_and_exists(path: impl AsRef<Path>) -> XResult<()> {
|
||||
let path = path.as_ref();
|
||||
let path_display = format!("{}", path.display());
|
||||
if !path_display.ends_with(TINY_ENC_FILE_EXT) {
|
||||
return simple_error!("File is not tiny encrypt file: {}", &path_display);
|
||||
}
|
||||
require_file_exists(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn require_file_exists(path: impl AsRef<Path>) -> XResult<()> {
|
||||
let path = path.as_ref();
|
||||
match fs::metadata(path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => simple_error!("File: {} not exists", path.display()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn require_file_not_exists(path: impl AsRef<Path>) -> XResult<()> {
|
||||
let path = path.as_ref();
|
||||
match fs::metadata(path) {
|
||||
Ok(_) => simple_error!("File: {} exists", path.display()),
|
||||
Err(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn simple_kdf(input: &[u8]) -> Vec<u8> {
|
||||
let input = hex::decode(sha256::digest(input)).unwrap();
|
||||
let input = hex::decode(sha256::digest(input)).unwrap();
|
||||
@@ -79,4 +107,9 @@ pub fn get_user_agent() -> String {
|
||||
panic!("Unsupported OS!");
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn zeroize(object: impl Zeroize) {
|
||||
let mut object = object;
|
||||
object.zeroize();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user