1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Can crypt user password

This commit is contained in:
Pierre HUBERT 2020-05-24 14:23:03 +02:00
parent fc24b8f747
commit 6f6cd550ea
5 changed files with 56 additions and 1 deletions

1
Cargo.lock generated
View File

@ -515,6 +515,7 @@ dependencies = [
"mysql",
"percent-encoding",
"serde",
"sha1",
"yaml-rust",
]

View File

@ -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"
mailchecker = "3.3.6"
sha1 = "0.6.0"

View File

@ -2,5 +2,6 @@ pub mod data;
pub mod helpers;
pub mod controllers;
pub mod api_data;
pub mod utils;
pub mod database_structure;

47
src/utils/crypt_utils.rs Normal file
View File

@ -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<String> {
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)?)
}

5
src/utils/mod.rs Normal file
View File

@ -0,0 +1,5 @@
//! # Utilities
//!
//! This module contains utilities that can be used anywhere in the code
pub mod crypt_utils;