diff --git a/src/_bcrypt/src/lib.rs b/src/_bcrypt/src/lib.rs index 7cd16658..20e2a1e7 100644 --- a/src/_bcrypt/src/lib.rs +++ b/src/_bcrypt/src/lib.rs @@ -136,6 +136,18 @@ fn hashpw<'p>( )) } + +#[pyo3::pyfunction] +fn hashpw_from_string<'p>( + py: pyo3::Python<'p>, + password: String, + salt: &[u8], +) -> pyo3::PyResult> { + hashpw(py, password.as_bytes(), salt) +} + + + #[pyo3::pyfunction] fn checkpw(py: pyo3::Python<'_>, password: &[u8], hashed_password: &[u8]) -> pyo3::PyResult { Ok(hashpw(py, password, hashed_password)? @@ -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 { + 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>( @@ -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] diff --git a/src/bcrypt/__init__.pyi b/src/bcrypt/__init__.pyi index 12e4a2ef..bceb51a7 100644 --- a/src/bcrypt/__init__.pyi +++ b/src/bcrypt/__init__.pyi @@ -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, diff --git a/tests/test_bcrypt.py b/tests/test_bcrypt.py index c995bcb5..fd4651ee 100644 --- a/tests/test_bcrypt.py +++ b/tests/test_bcrypt.py @@ -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):