From 461744c8a426d818c5001f1bea236356497e0def Mon Sep 17 00:00:00 2001 From: Kilerd Chan Date: Sun, 14 Oct 2018 00:55:20 +0800 Subject: [PATCH] feat: use rust-crypto to encrypt password with sha3 hash alg --- Cargo.toml | 3 ++- src/main.rs | 2 ++ src/models.rs | 16 +++++++++++++--- src/routers/admin.rs | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86dad84..84149a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ serde = "1.0" serde_derive = "1.0" tera = "*" pulldown-cmark = { version = "0.1.2", default-features = false } -chrono = { version = "*", features = ["serde"] } \ No newline at end of file +chrono = { version = "*", features = ["serde"] } +rust-crypto = "^0.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fdd437b..2fb86c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ extern crate serde; extern crate serde_derive; extern crate tera; +extern crate crypto; + use dotenv::dotenv; use rocket_contrib::Template; diff --git a/src/models.rs b/src/models.rs index 031e1d5..e28cd60 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,10 +3,12 @@ use chrono::NaiveDate; use chrono::NaiveTime; use serde::Serialize; use chrono::NaiveDateTime; +use crypto::sha3::Sha3; +use crypto::digest::Digest; #[derive(Queryable, Debug, Serialize)] #[belongs_to(User)] -#[table_name= "posts"] +#[table_name = "posts"] pub struct Post { pub id: i32, pub title: String, @@ -28,8 +30,16 @@ pub struct User { } impl User { - pub fn authenticated(&self, password: &str) -> bool { - true + let mut hasher = Sha3::sha3_256(); + hasher.input_str(password); + let result = hasher.result_str(); + + if self.password.eq(&result) { + true + }else { + false + } + } } diff --git a/src/routers/admin.rs b/src/routers/admin.rs index bc20e37..2a559fe 100644 --- a/src/routers/admin.rs +++ b/src/routers/admin.rs @@ -12,6 +12,7 @@ use schema::{users, users::dsl::*}; use rocket::http::Status; use pg_pool::DbConn; use rocket::http::Cookie; +use crypto::sha3::Sha3; #[get("/login")]