return Result for errors

master
rasul 4 years ago
parent 45046af201
commit 961b01ffd6

@ -8,6 +8,8 @@ use rusqlite::Row;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use crate::hash::{hash, salt}; use crate::hash::{hash, salt};
use crate::result::RudeResult;
use crate::try_log;
/// Containing object for the password /// Containing object for the password
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@ -21,19 +23,22 @@ pub struct Password {
impl Password { impl Password {
/// Create a new password object from a string password. /// Create a new password object from a string password.
pub fn new<S: Into<String>>(s: S) -> Self { pub fn new<S: Into<String>>(s: S) -> RudeResult<Self> {
let salt = salt(); let salt = salt();
let hash = hash(s, &salt); let hash = try_log!(hash(s, &salt), "Unable to create hash");
Self { salt, hash } Ok(Self { salt, hash })
} }
/// Check the password against the provided password. /// Check the password against the provided password.
pub fn check<S: Into<String>>(&self, s: S) -> bool { pub fn check<S: Into<String>>(&self, s: S) -> RudeResult<bool> {
let s = s.into(); let s = s.into();
let hash = hash(s, &self.salt); let hash = try_log!(
hash(s, &self.salt),
"Unable to check hash",
);
self.hash == hash Ok(self.hash == hash)
} }
} }

Loading…
Cancel
Save