rework basically everything

This commit is contained in:
jfmonty2 committed 2026-11-12 11:28:18 -05:00
1 parent 2bd81a66a4
commit a1e5daba59
12 files changed
+450 -157

No files matched your search

+26 -45
View File
@@ -1,62 +1,43 @@
use argon2::{Algorithm, Argon2, ParamsBuilder, Result as Argon2Result, Version};
use gpui_kit::Global;
use crate::config::AppConfig;
use crate::error::CreddyError;
use crate::db::Db;
// tests take forever with production parameters, so we turn them down in dev
#[cfg(not(debug_assertions))]
const MEM_COST: u32 = 128 * 1024;
#[cfg(not(debug_assertions))]
const TIME_COST: u32 = 8;
#[cfg(not(debug_assertions))]
const PARALLELISM_COST: u32 = 4;
#[cfg(debug_assertions)]
const MEM_COST: u32 = 48 * 1024;
#[cfg(debug_assertions)]
const TIME_COST: u32 = 1;
#[cfg(debug_assertions)]
const PARALLELISM_COST: u32 = 1;
#[derive(Clone, Debug)]
#[derive(Debug)]
pub enum AppSession {
Empty,
Locked,
Unlocked { key: [u8; 32] },
Unlocked(Db),
}
impl AppSession {
pub fn new(passphrase: &str, salt: &[u8]) -> Argon2Result<Self> {
let params = ParamsBuilder::new()
.m_cost(MEM_COST)
.t_cost(TIME_COST)
.p_cost(PARALLELISM_COST)
.build()
.unwrap(); // only errors if the given params are invalid
let hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
let mut key = [0; 32]; // todo: replace this with a `zeroize` or `secrecy` type
hasher.hash_password_into(passphrase.as_bytes(), &salt, &mut key)?;
Ok(Self::Unlocked { key })
pub fn load(config: &AppConfig) -> AppSession {
if config.db_path.exists() {
AppSession::Locked
} else {
AppSession::Empty
}
}
pub fn salt() -> Result<[u8; 32], getrandom::Error> {
let mut salt = [0; 32];
getrandom::fill(&mut salt)?;
Ok(salt)
pub fn unlock(&mut self, passphrase: &str) -> Result<AppSession, CreddyError> {
todo!()
}
}
impl Global for AppSession {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let passphrase = "correct horse battery staple";
let salt = AppSession::salt().expect("salt generation failed.");
let session = AppSession::new(passphrase, &salt).expect("password KDF failed.");
match session {
AppSession::Unlocked { .. } => (),
_ => panic!("AppSession::new() should return an unlocked session"),
}
}
// #[test]
// fn test_new() {
// let passphrase = "correct horse battery staple";
// let salt = AppSession::salt().expect("salt generation failed.");
// let session = AppSession::new(passphrase, &salt).expect("password KDF failed.");
// match session {
// AppSession::Unlocked { .. } => (),
// _ => panic!("AppSession::new() should return an unlocked session"),
// }
// }
}