mirror of
https://github.com/atuinsh/atuin.git
synced 2024-12-28 18:09:25 +01:00
fe45787474
* Re-add macro_use to atuin-common When build as a dependency, the macro is available from another crate. When you try to build common by itself, the macro is not found. Magic, huh? * chore: remove unneeded use - clippy is confused Co-authored-by: Conrad Ludgate <conradludgate@gmail.com>
94 lines
2.5 KiB
Rust
94 lines
2.5 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use crypto::digest::Digest;
|
|
use crypto::sha2::Sha256;
|
|
use sodiumoxide::crypto::pwhash::argon2id13;
|
|
use uuid::Uuid;
|
|
|
|
pub fn hash_secret(secret: &str) -> String {
|
|
sodiumoxide::init().unwrap();
|
|
let hash = argon2id13::pwhash(
|
|
secret.as_bytes(),
|
|
argon2id13::OPSLIMIT_INTERACTIVE,
|
|
argon2id13::MEMLIMIT_INTERACTIVE,
|
|
)
|
|
.unwrap();
|
|
let texthash = std::str::from_utf8(&hash.0).unwrap().to_string();
|
|
|
|
// postgres hates null chars. don't do that to postgres
|
|
texthash.trim_end_matches('\u{0}').to_string()
|
|
}
|
|
|
|
pub fn hash_str(string: &str) -> String {
|
|
let mut hasher = Sha256::new();
|
|
hasher.input_str(string);
|
|
|
|
hasher.result_str()
|
|
}
|
|
|
|
pub fn uuid_v4() -> String {
|
|
Uuid::new_v4().to_simple().to_string()
|
|
}
|
|
|
|
// TODO: more reliable, more tested
|
|
// I don't want to use ProjectDirs, it puts config in awkward places on
|
|
// mac. Data too. Seems to be more intended for GUI apps.
|
|
pub fn home_dir() -> PathBuf {
|
|
let home = std::env::var("HOME").expect("$HOME not found");
|
|
PathBuf::from(home)
|
|
}
|
|
|
|
pub fn config_dir() -> PathBuf {
|
|
let config_dir =
|
|
std::env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from);
|
|
config_dir.join("atuin")
|
|
}
|
|
|
|
pub fn data_dir() -> PathBuf {
|
|
let data_dir = std::env::var("XDG_DATA_HOME")
|
|
.map_or_else(|_| home_dir().join(".local").join("share"), PathBuf::from);
|
|
|
|
data_dir.join("atuin")
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::env;
|
|
|
|
#[test]
|
|
fn test_config_dir_xdg() {
|
|
env::remove_var("HOME");
|
|
env::set_var("XDG_CONFIG_HOME", "/home/user/custom_config");
|
|
assert_eq!(
|
|
config_dir(),
|
|
PathBuf::from("/home/user/custom_config/atuin")
|
|
);
|
|
env::remove_var("XDG_CONFIG_HOME");
|
|
}
|
|
|
|
#[test]
|
|
fn test_config_dir() {
|
|
env::set_var("HOME", "/home/user");
|
|
env::remove_var("XDG_CONFIG_HOME");
|
|
assert_eq!(config_dir(), PathBuf::from("/home/user/.config/atuin"));
|
|
env::remove_var("HOME");
|
|
}
|
|
|
|
#[test]
|
|
fn test_data_dir_xdg() {
|
|
env::remove_var("HOME");
|
|
env::set_var("XDG_DATA_HOME", "/home/user/custom_data");
|
|
assert_eq!(data_dir(), PathBuf::from("/home/user/custom_data/atuin"));
|
|
env::remove_var("XDG_DATA_HOME");
|
|
}
|
|
|
|
#[test]
|
|
fn test_data_dir() {
|
|
env::set_var("HOME", "/home/user");
|
|
env::remove_var("XDG_DATA_HOME");
|
|
assert_eq!(data_dir(), PathBuf::from("/home/user/.local/share/atuin"));
|
|
env::remove_var("HOME");
|
|
}
|
|
}
|