Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/_bcrypt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ fn hashpw<'p>(
))
}


#[pyo3::pyfunction]
fn hashpw_from_string<'p>(
py: pyo3::Python<'p>,
password: String,
salt: &[u8],
) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
hashpw(py, password.as_bytes(), salt)
}



#[pyo3::pyfunction]
fn checkpw(py: pyo3::Python<'_>, password: &[u8], hashed_password: &[u8]) -> pyo3::PyResult<bool> {
Ok(hashpw(py, password, hashed_password)?
Expand All @@ -144,6 +156,16 @@ fn checkpw(py: pyo3::Python<'_>, password: &[u8], hashed_password: &[u8]) -> pyo
.into())
}

#[pyo3::pyfunction]
fn checkpw_from_string<'p>(
py: pyo3::Python<'p>,
password: String,
hashed_password: String
) -> pyo3::PyResult<bool> {
checkpw(py, password.as_bytes(), hashed_password.as_bytes())
}


#[pyo3::pyfunction]
#[pyo3(signature = (password, salt, desired_key_bytes, rounds, ignore_few_rounds=false))]
fn kdf<'p>(
Expand Down Expand Up @@ -197,7 +219,14 @@ mod _bcrypt {
use pyo3::types::PyModuleMethods;

#[pymodule_export]
use super::{checkpw, gensalt, hashpw, kdf};
use super::{
checkpw,
checkpw_from_string,
gensalt,
hashpw,
hashpw_from_string,
kdf
};

// Not yet possible to add constants declaratively.
#[pymodule_init]
Expand Down
2 changes: 2 additions & 0 deletions src/bcrypt/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
def gensalt(rounds: int = 12, prefix: bytes = b"2b") -> bytes: ...
def hashpw(password: bytes, salt: bytes) -> bytes: ...
def hashpw_from_string(password:str, salt: bytes) -> bytes:...
def checkpw(password: bytes, hashed_password: bytes) -> bool: ...
def checkpw_from_string(password:str, hashed_password:str) -> bool:...
def kdf(
password: bytes,
salt: bytes,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_bcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def test_invalid_params(password, salt, desired_key_bytes, rounds, error):
with pytest.raises(error):
bcrypt.kdf(password, salt, desired_key_bytes, rounds)

def test_unicode():
hash_ = bcrypt.hashpw_from_string("password", bcrypt.gensalt())
assert bcrypt.checkpw_from_string("password", hash_.decode('utf-8'))


def test_multithreading():
def create_user(pw):
Expand Down
Loading