Skip to content

Commit

Permalink
Replace rust-crypto with sodiumoxide
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Feb 21, 2016
1 parent 3ee1fc4 commit 0abb67d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ unicase = "*"
atomicwrites = "*"
url = "*"
clap = "*"
rust-crypto = "*"
rand = "*"
persistent = "*"
urlencoded = "*"
Expand All @@ -44,6 +43,7 @@ itertools = "*"
jsonwebtoken = "*"
uuid = "*"
clippy = {git = "https://github.com/Manishearth/rust-clippy", version = "*", optional = true}
sodiumoxide = "*"
webicon = "*"

[dev-dependencies]
Expand Down
35 changes: 27 additions & 8 deletions src/mysteryshack/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub fn main() {
.index(1)))
.subcommand(SubCommand::with_name("delete")
.about("Delete a user")
.arg(Arg::with_name("USERNAME")
.help("The username")
.required(true)
.index(1)))
.subcommand(SubCommand::with_name("setpass")
.about("Change the password for a user")
.arg(Arg::with_name("USERNAME")
.help("The username")
.required(true)
Expand All @@ -83,14 +89,7 @@ pub fn main() {
serve(_,) => web::run_server(config),
user(user_matches,) => clap_dispatch!(user_matches; {
create(_, USERNAME as username) => {
let password_hash = match models::PasswordHash::from_password(
utils::double_prompt("Password for new user: ")) {
Ok(x) => x,
Err(e) => {
println!("Failed to hash password: {}", e);
process::exit(1);
}
};
let password_hash = models::PasswordHash::from_password(utils::double_prompt("Password for new user: "));

match models::User::create(&config.data_path, username).map(|user| {
user.set_password_hash(password_hash)
Expand All @@ -104,6 +103,26 @@ pub fn main() {

println!("Successfully created user {}", username);
},
setpass(_, USERNAME as username) => {
let user = match models::User::get(&config.data_path, username) {
Some(x) => x,
None => {
println!("User does not exist: {}", username);
process::exit(1);
}
};

let password_hash = models::PasswordHash::from_password(utils::double_prompt("New password: "));
match user.set_password_hash(password_hash) {
Ok(_) => (),
Err(e) => {
println!("Failed to set password for user {}: {}", username, e);
process::exit(1);
}
};

println!("Changed password for user {}", username);
},
delete(_, USERNAME as username) => {
let user = match models::User::get(&config.data_path, username) {
Some(x) => x,
Expand Down
2 changes: 1 addition & 1 deletion src/mysteryshack/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern crate atomicwrites;
extern crate url;
extern crate urlencoded;
extern crate clap;
extern crate crypto;
extern crate sodiumoxide;
extern crate rand;
extern crate persistent;
extern crate iron_login;
Expand Down
39 changes: 10 additions & 29 deletions src/mysteryshack/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use uuid;
use itertools::Itertools;
use regex;

use crypto::bcrypt;
use sodiumoxide::crypto::pwhash;
use rand::{Rng, StdRng};

use atomicwrites;
Expand Down Expand Up @@ -324,39 +324,20 @@ impl Token {

#[derive(RustcDecodable, RustcEncodable, Debug)]
pub struct PasswordHash {
cost: u32,
salt: Vec<u8>,
hash: Vec<u8>
content: pwhash::HashedPassword
}

impl PasswordHash {
pub fn from_password(pwd: String) -> io::Result<PasswordHash> {
const DEFAULT_COST: u32 = 10;
const MAX_SALT_SIZE: usize = 16;
const OUTPUT_SIZE: usize = 24;

let salt = {
let mut rv = [0u8; MAX_SALT_SIZE];
let mut rng = try!(StdRng::new());
rng.fill_bytes(&mut rv);
rv
};

let mut hash = [0u8; OUTPUT_SIZE];
bcrypt::bcrypt(DEFAULT_COST, &salt, pwd.as_bytes(), &mut hash);
Ok(PasswordHash {
cost: DEFAULT_COST,
salt: salt.to_vec(),
hash: hash.to_vec()
})
pub fn from_password(pwd: String) -> PasswordHash {
PasswordHash {
content: pwhash::pwhash(pwd.as_bytes(),
pwhash::OPSLIMIT_INTERACTIVE,
pwhash::MEMLIMIT_INTERACTIVE).unwrap()
}
}

pub fn equals_password<T: AsRef<str>>(&self, pwd: T) -> bool {
let mut hash = Vec::with_capacity(self.hash.len());
for _ in 0..self.hash.len() { hash.push(0u8); }

bcrypt::bcrypt(self.cost, &self.salt, pwd.as_ref().as_bytes(), &mut hash);
hash == self.hash
pub fn equals_password<T: AsRef<[u8]>>(&self, pwd: T) -> bool {
pwhash::pwhash_verify(&self.content, pwd.as_ref())
}
}

Expand Down

0 comments on commit 0abb67d

Please sign in to comment.