fn _hash_password(password: &[u8], cost: u32, salt: &[u8]) -> BcryptResult<HashParts> {
    if cost > MAX_COST || cost < MIN_COST {
        return Err(BcryptError::CostNotAllowed(cost));
    }
    if password.contains(&0u8) {
        return Err(BcryptError::InvalidPassword);
    }

    // Output is 24
    let mut output = [0u8; 24];
    // Passwords need to be null terminated
    let mut vec: Vec<u8> = Vec::new();
    vec.extend_from_slice(password);
    vec.push(0);
    // We only consider the first 72 chars; truncate if necessary.
    // `bcrypt` below will panic if len > 72
    let truncated = if vec.len() > 72 { &vec[..72] } else { &vec };

    bcrypt::bcrypt(cost, salt, truncated, &mut output);

    Ok(HashParts {
        cost,
        salt: b64::encode(salt),
        hash: b64::encode(&output[..23]), // remember to remove the last byte
    })
}
/// The main meat: actually does the hashing and does some verification with
/// the cost to ensure it's a correct one
The main meat: actually does the hashing and does some verification with
the cost to ensure it's a correct one
The main meat : actually does the hashing and does some verification with the cost to ensure it ' s a correct one
fn _hash_password(password: &[u8], cost: u32, salt: &[u8]) -> BcryptResult<HashParts> {
    if cost > MAX_COST || cost < MIN_COST {
        return Err(BcryptError::CostNotAllowed(cost));
    }
    if password.contains(&0u8) {
        return Err(BcryptError::InvalidPassword);
    }
    // Output is 24
    let mut output = [0u8; 24];
    // Passwords need to be null terminated
    let mut vec: Vec<u8> = Vec::new();
    vec.extend_from_slice(password);
    vec.push(0);
    // We only consider the first 72 chars; truncate if necessary.
    // `bcrypt` below will panic if len > 72
    let truncated = if vec.len() > 72 { &vec[..72] } else { &vec };
    bcrypt::bcrypt(cost, salt, truncated, &mut output);
    Ok(HashParts {
        cost,
        salt: b64::encode(salt),
        hash: b64::encode(&output[..23]), // remember to remove the last byte
    })
}
fn _hash_password ( password : & [ u8 ] , cost : u32 , salt : & [ u8 ] ) -> BcryptResult < HashParts > { if cost > MAX_COST || cost < MIN_COST { return Err ( BcryptError :: CostNotAllowed ( cost ) ) ; } if password . contains ( & 0u8 ) { return Err ( BcryptError :: InvalidPassword ) ; } // Output is 24 let mut output = [ 0u8 ; 24 ] ; // Passwords need to be null terminated let mut vec : Vec < u8 > = Vec :: new ( ) ; vec . extend_from_slice ( password ) ; vec . push ( 0 ) ; // We only consider the first 72 chars; truncate if necessary. // `bcrypt` below will panic if len > 72 let truncated = if vec . len ( ) > 72 { & vec [ .. 72 ] } else { & vec } ; bcrypt :: bcrypt ( cost , salt , truncated , & mut output ) ; Ok ( HashParts { cost , salt : b64 :: encode ( salt ) , hash : b64 :: encode ( & output [ .. 23 ] ) , // remember to remove the last byte } ) }
The main meat: actually does the hashing and does some verification with
the cost to ensure it's a correct one
The main meat : actually does the hashing and does some verification with the cost to ensure it ' s a correct one
password
&[u8]
cost
u32
salt
&[u8]