diff --git a/Cargo.lock b/Cargo.lock index 31a0ce8..841e351 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -515,6 +515,7 @@ dependencies = [ "mysql", "percent-encoding", "serde", + "sha1", "yaml-rust", ] diff --git a/Cargo.toml b/Cargo.toml index 5ee8e40..76595ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ serde = "1.0.110" futures = "0.3.5" encoding_rs = "0.8.23" percent-encoding = "2.1.0" -mailchecker = "3.3.6" \ No newline at end of file +mailchecker = "3.3.6" +sha1 = "0.6.0" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 0b18cbf..b0092f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,5 +2,6 @@ pub mod data; pub mod helpers; pub mod controllers; pub mod api_data; +pub mod utils; pub mod database_structure; \ No newline at end of file diff --git a/src/utils/crypt_utils.rs b/src/utils/crypt_utils.rs new file mode 100644 index 0000000..021c5f7 --- /dev/null +++ b/src/utils/crypt_utils.rs @@ -0,0 +1,47 @@ +//! # Cryptographic utilities +//! +//! Author: Pierre Hubert + +extern crate sha1; + +use crate::data::error::{ResultBoxError, ExecError}; + +/// Generate a new sha1 string +/// +/// ``` +/// use comunic_server::utils::crypt_utils::sha1_str; +/// +/// let res = sha1_str("secret"); +/// assert_eq!(res, "e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4"); +/// ``` +pub fn sha1_str(input: &str) -> String { + sha1::Sha1::from(input).digest().to_string() +} + +/// One-way user password crypt +/// +/// This method will have to be replaced by a stronger algorithm once this implementation is +/// completely built. +/// +/// It currently depends on PHP +/// +/// ``` +/// use comunic_server::utils::crypt_utils::crypt_pass; +/// +/// let res = crypt_pass("secret").unwrap(); +/// assert_eq!(res, "e5GUe5kdeUMGs"); +/// ``` +pub fn crypt_pass(pass: &str) -> ResultBoxError { + let hash = sha1_str(pass); + + let result = std::process::Command::new("/usr/bin/php") + .arg("-r") + .arg(format!("echo crypt('{}', '{}');", hash, hash)) + .output()?; + + if !result.status.success() { + return Err(ExecError::boxed_new("Failed to crypt password!")); + } + + Ok(String::from_utf8(result.stdout)?) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..632812e --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,5 @@ +//! # Utilities +//! +//! This module contains utilities that can be used anywhere in the code + +pub mod crypt_utils; \ No newline at end of file