Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60eb00f
commit c100f40
Showing
7 changed files
with
111 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,19 @@ | ||
use rand::{OsRng, RngCore}; | ||
use sha2::{Digest, Sha512}; | ||
use std::io; | ||
use sha_crypt::{errors::CryptError, sha512_simple, Sha512Params}; | ||
use std::result::Result; | ||
|
||
pub enum PasswordScheme { | ||
SHA512_CRYPT, | ||
Sha512Crypt, | ||
// TODO add some more and let the user decide, for now SHA512_CRYPT should | ||
// be alright | ||
} | ||
|
||
pub fn hash(scheme: PasswordScheme, value: String) -> io::Result<String> { | ||
let mut hasher = match scheme { | ||
SHA512_CRYPT => Sha512::default(), | ||
}; | ||
|
||
let mut rng = OsRng::new()?; | ||
let mut salt = [0u8; 16]; | ||
rng.try_fill_bytes(&mut salt)?; | ||
|
||
hasher.input(value.as_bytes()); | ||
|
||
let out = format!("{:?}", hasher.result().as_slice()); | ||
//let out = format!("out: {:?}", out); | ||
|
||
let out = format!("{{SHA512_CRYPT}}{}", out); | ||
|
||
Ok(out) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::{hash, PasswordScheme}; | ||
|
||
#[test] | ||
fn test_sha512() { | ||
// {SHA512-CRYPT}$6$KSlmgPC2Vch4xRCg$y05xTawj81BeVY0a8P00qxVwBP9kCAxaXiyGg02Aj2fdgQS24dhCT07Ud0/z5Aotzqodc6hzBUJkOIskvqHFr1 | ||
println!( | ||
"{}", | ||
hash(PasswordScheme::SHA512_CRYPT, "foobar".to_string()).unwrap() | ||
); | ||
//assert_eq!( | ||
//None, | ||
//hash(PasswordScheme::SHA512_CRYPT, "foobar".to_string()) | ||
//); | ||
pub fn hash(scheme: PasswordScheme, value: String) -> Result<String, CryptError> { | ||
match scheme { | ||
PasswordScheme::Sha512Crypt => { | ||
let params = Sha512Params::default(); | ||
let mut pass = "{SHA512-CRYPT}".to_string(); | ||
pass += &sha512_simple(&value, ¶ms)?; | ||
Ok(pass) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters