Skip to content

Commit

Permalink
Allow using "plain" passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Nov 22, 2019
1 parent bab99d3 commit 4e51660
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 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
@@ -1,6 +1,6 @@
[package]
name = "hat"
version = "0.5.0"
version = "0.5.1"
authors = ["Jens Reimann <[email protected]>"]
edition = "2018"

Expand Down
11 changes: 1 addition & 10 deletions src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,9 @@ fn new_credential(type_name: &str, auth_id: &str) -> Value {
fn new_secret(plain_password: &str, hash_function: &HashFunction) -> Result<Value> {
let mut new_pair = Map::new();

// hash it

let hash = hash_function.hash(&plain_password)?;

// put to result

new_pair.insert("hash-function".into(), hash_function.name().into());
new_pair.insert("pwd-hash".into(), hash.0.into());

if let Some(salt) = hash.1 {
new_pair.insert("salt".into(), salt.into());
}
hash_function.insert(&mut new_pair, &plain_password)?;

// return as value

Expand Down
44 changes: 40 additions & 4 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use rand::RngCore;

use std::fmt;

use serde_json::value::{Map, Value};

pub enum HashFunction {
Plain,
Sha256,
Sha512,
Bcrypt(u8),
Expand All @@ -33,6 +36,7 @@ impl std::str::FromStr for HashFunction {

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"plain" => Ok(HashFunction::Plain),
"sha-256" => Ok(HashFunction::Sha256),
"sha-512" => Ok(HashFunction::Sha512),
"bcrypt" => Ok(HashFunction::Bcrypt(10)),
Expand All @@ -44,6 +48,7 @@ impl std::str::FromStr for HashFunction {
impl fmt::Display for HashFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
HashFunction::Plain => write!(f, "plain"),
HashFunction::Sha256 => write!(f, "sha-256"),
HashFunction::Sha512 => write!(f, "sha-512"),
HashFunction::Bcrypt(i) => write!(f, "bcrypt:{}", i),
Expand Down Expand Up @@ -80,6 +85,7 @@ fn gen_salt(size: usize) -> Vec<u8> {
impl HashFunction {
pub fn name(&self) -> &str {
match self {
HashFunction::Plain => "plain",
HashFunction::Sha256 => "sha-256",
HashFunction::Sha512 => "sha-512",
HashFunction::Bcrypt(_) => "bcrypt", // we omit the iterations here
Expand All @@ -103,11 +109,41 @@ impl HashFunction {
}
}

pub fn hash(&self, password: &str) -> Result<(String, Option<String>)> {
fn insert_hash<D: Digest + Default>(
&self,
new_pair: &mut Map<String, Value>,
password: &str,
) -> Result<()> {
new_pair.insert("hash-function".into(), self.name().into());
let r = do_hash::<D>(gen_salt(16).as_slice(), password);
new_pair.insert("pwd-hash".into(), r.0.into());
if let Some(salt) = r.1 {
new_pair.insert("salt".into(), salt.into());
}
Ok(())
}

fn insert_bcrypt(
&self,
new_pair: &mut Map<String, Value>,
password: &str,
i: u8,
) -> Result<()> {
new_pair.insert("hash-function".into(), self.name().into());
let r = do_bcrypt(password, i)?;
new_pair.insert("pwd-hash".into(), r.0.into());
Ok(())
}

pub fn insert(&self, new_pair: &mut Map<String, Value>, password: &str) -> Result<()> {
match self {
HashFunction::Sha256 => Ok(do_hash::<Sha256>(gen_salt(16).as_slice(), password)),
HashFunction::Sha512 => Ok(do_hash::<Sha512>(gen_salt(16).as_slice(), password)),
HashFunction::Bcrypt(i) => do_bcrypt(password, *i),
HashFunction::Plain => {
new_pair.insert("pwd-plain".into(), password.into());
Ok(())
}
HashFunction::Sha256 => self.insert_hash::<Sha256>(new_pair, password),
HashFunction::Sha512 => self.insert_hash::<Sha512>(new_pair, password),
HashFunction::Bcrypt(i) => self.insert_bcrypt(new_pair, password, *i),
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn app() -> App<'static, 'static> {
.long("hash")
.required(true)
.takes_value(true)
.help("Password hash function [possible values: sha-256, sha-512, bcrypt<:iterations>]")
.help("Password hash function [possible values: plain, sha-256, sha-512, bcrypt<:iterations>]")
.default_value("bcrypt");

// overrides
Expand Down

0 comments on commit 4e51660

Please sign in to comment.