rok-core 0.6.1

Core primitives for the rok ecosystem — errors, crypto, i18n, config, DI, and more
Documentation
use crate::crypto::hash::{config::BcryptConfig, driver::HashDriver, HashError};

pub(crate) struct BcryptDriver {
    config: BcryptConfig,
}

impl BcryptDriver {
    pub(crate) fn new(config: BcryptConfig) -> Self {
        Self { config }
    }
}

impl HashDriver for BcryptDriver {
    fn hash(&self, password: &str) -> Result<String, HashError> {
        bcrypt::hash(password, self.config.cost).map_err(|e| HashError::HashFailed(e.to_string()))
    }

    fn verify(&self, password: &str, hash: &str) -> Result<bool, HashError> {
        bcrypt::verify(password, hash).map_err(|e| HashError::HashFailed(e.to_string()))
    }

    fn needs_rehash(&self, hash: &str) -> bool {
        let mut parts = hash.splitn(4, '$');
        parts.next();
        parts.next();
        let stored_cost: u32 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
        stored_cost < self.config.cost
    }
}