Skip to content

Commit 7939bef

Browse files
authored
Everything is Rust (#670)
1 parent db93a85 commit 7939bef

11 files changed

+57
-116
lines changed

MANIFEST.in

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ recursive-include tests *.py
1111
exclude requirements.txt release.py mypy.ini
1212

1313
recursive-exclude .github *
14-
recursive-exclude .circleci *
1514

1615
exclude src/_bcrypt/target
1716
recursive-exclude src/_bcrypt/target *

mypy.ini

-36
This file was deleted.

pyproject.toml

+9
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ line-length = 79
1616

1717
[tool.ruff.isort]
1818
known-first-party = ["bcrypt", "tests"]
19+
20+
[tool.mypy]
21+
show_error_codes = true
22+
check_untyped_defs = true
23+
no_implicit_reexport = true
24+
warn_redundant_casts = true
25+
warn_unused_ignores = true
26+
warn_unused_configs = true
27+
strict_equality = true

setup.cfg

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[metadata]
22
name = bcrypt
3-
version = attr: bcrypt.__about__.__version__
3+
# When updating this, also update lib.rs
4+
version = 4.0.1
45
description = Modern password hashing for your software and your servers
56
long_description = file: README.rst
67
long_description_content_type = text/x-rst
@@ -24,14 +25,13 @@ classifiers =
2425
Programming Language :: Python :: 3.12
2526

2627
[options]
27-
python_requires = >=3.6
28+
python_requires = >=3.7
2829
include_package_data = True
2930
zip_safe = False
3031
package_dir =
3132
=src
3233
packages =
33-
bcrypt
34-
ext_package = bcrypt
34+
bcrypt
3535

3636
[options.extras_require]
3737
tests =

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
import platform
32
import re
43
import shutil
@@ -35,7 +34,7 @@
3534
setup(
3635
rust_extensions=[
3736
RustExtension(
38-
"_bcrypt",
37+
"bcrypt._bcrypt",
3938
"src/_bcrypt/Cargo.toml",
4039
py_limited_api=True,
4140
# Enable abi3 mode if we're not using PyPy.

src/_bcrypt/src/lib.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn gensalt<'p>(
5656
}
5757

5858
#[pyo3::prelude::pyfunction]
59-
fn hashpass<'p>(
59+
fn hashpw<'p>(
6060
py: pyo3::Python<'p>,
6161
password: &[u8],
6262
salt: &[u8],
@@ -111,19 +111,19 @@ fn hashpass<'p>(
111111
}
112112

113113
#[pyo3::prelude::pyfunction]
114-
fn checkpass(
114+
fn checkpw(
115115
py: pyo3::Python<'_>,
116116
password: &[u8],
117117
hashed_password: &[u8],
118118
) -> pyo3::PyResult<bool> {
119-
Ok(hashpass(py, password, hashed_password)?
119+
Ok(hashpw(py, password, hashed_password)?
120120
.as_bytes()
121121
.ct_eq(hashed_password)
122122
.into())
123123
}
124124

125125
#[pyo3::prelude::pyfunction]
126-
fn pbkdf<'p>(
126+
fn kdf<'p>(
127127
py: pyo3::Python<'p>,
128128
password: &[u8],
129129
salt: &[u8],
@@ -174,9 +174,23 @@ fn pbkdf<'p>(
174174
#[pyo3::prelude::pymodule]
175175
fn _bcrypt(_py: pyo3::Python<'_>, m: &pyo3::types::PyModule) -> pyo3::PyResult<()> {
176176
m.add_function(pyo3::wrap_pyfunction!(gensalt, m)?)?;
177-
m.add_function(pyo3::wrap_pyfunction!(hashpass, m)?)?;
178-
m.add_function(pyo3::wrap_pyfunction!(checkpass, m)?)?;
179-
m.add_function(pyo3::wrap_pyfunction!(pbkdf, m)?)?;
177+
m.add_function(pyo3::wrap_pyfunction!(hashpw, m)?)?;
178+
m.add_function(pyo3::wrap_pyfunction!(checkpw, m)?)?;
179+
m.add_function(pyo3::wrap_pyfunction!(kdf, m)?)?;
180+
181+
m.add("__title__", "bcrypt")?;
182+
m.add("__summary__", "Modern(-ish) password hashing for your software and your servers")?;
183+
m.add("__uri__", "https://github.com/pyca/bcrypt/")?;
184+
185+
// When updating this, also update setup.cfg
186+
m.add("__version__", "4.0.1")?;
187+
188+
let author = "The Python Cryptographic Authority developers";
189+
m.add("__author__", author)?;
190+
m.add("__email__", "[email protected]")?;
191+
192+
m.add("__license__", "Apache License, Version 2.0")?;
193+
m.add("__copyright__", format!("Copyright 2013-2023 {author}"))?;
180194

181195
Ok(())
182196
}

src/bcrypt/__about__.py

-38
This file was deleted.

src/bcrypt/__init__.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Author:: Donald Stufft (<[email protected]>)
2-
# Copyright:: Copyright (c) 2013 Donald Stufft
3-
# License:: Apache License, Version 2.0
4-
#
51
# Licensed under the Apache License, Version 2.0 (the "License");
62
# you may not use this file except in compliance with the License.
73
# You may obtain a copy of the License at
@@ -14,8 +10,7 @@
1410
# See the License for the specific language governing permissions and
1511
# limitations under the License.
1612

17-
from . import _bcrypt
18-
from .__about__ import (
13+
from ._bcrypt import (
1914
__author__,
2015
__copyright__,
2116
__email__,
@@ -24,9 +19,17 @@
2419
__title__,
2520
__uri__,
2621
__version__,
22+
checkpw,
23+
gensalt,
24+
hashpw,
25+
kdf,
2726
)
2827

2928
__all__ = [
29+
"gensalt",
30+
"hashpw",
31+
"checkpw",
32+
"kdf",
3033
"__title__",
3134
"__summary__",
3235
"__uri__",
@@ -35,14 +38,4 @@
3538
"__email__",
3639
"__license__",
3740
"__copyright__",
38-
"gensalt",
39-
"hashpw",
40-
"kdf",
41-
"checkpw",
4241
]
43-
44-
45-
gensalt = _bcrypt.gensalt
46-
hashpw = _bcrypt.hashpass
47-
checkpw = _bcrypt.checkpass
48-
kdf = _bcrypt.pbkdf

src/bcrypt/_bcrypt.pyi src/bcrypt/__init__.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def gensalt(rounds: int = 12, prefix: bytes = b"2b") -> bytes: ...
2-
def hashpass(password: bytes, salt: bytes) -> bytes: ...
3-
def checkpass(password: bytes, hashed_password: bytes) -> bool: ...
4-
def pbkdf(
2+
def hashpw(password: bytes, salt: bytes) -> bytes: ...
3+
def checkpw(password: bytes, hashed_password: bytes) -> bool: ...
4+
def kdf(
55
password: bytes,
66
salt: bytes,
77
rounds: int,

tests/test_bcrypt.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -279,22 +279,22 @@ def test_checkpw_bad_salt():
279279

280280
def test_checkpw_str_password():
281281
with pytest.raises(TypeError):
282-
bcrypt.checkpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO")
282+
bcrypt.checkpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type]
283283

284284

285285
def test_checkpw_str_salt():
286286
with pytest.raises(TypeError):
287-
bcrypt.checkpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO")
287+
bcrypt.checkpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type]
288288

289289

290290
def test_hashpw_str_password():
291291
with pytest.raises(TypeError):
292-
bcrypt.hashpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO")
292+
bcrypt.hashpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type]
293293

294294

295295
def test_hashpw_str_salt():
296296
with pytest.raises(TypeError):
297-
bcrypt.hashpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO")
297+
bcrypt.hashpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type]
298298

299299

300300
def test_checkpw_nul_byte():
@@ -434,8 +434,7 @@ def test_checkpw_extra_data():
434434
[
435435
# UTF-8 Greek characters "odysseus" / "telemachos"
436436
8,
437-
b"\xe1\xbd\x88\xce\xb4\xcf\x85\xcf\x83\xcf\x83\xce\xb5\xcf\x8d\xcf"
438-
b"\x82",
437+
b"\xe1\xbd\x88\xce\xb4\xcf\x85\xcf\x83\xcf\x83\xce\xb5\xcf\x8d\xcf\x82",
439438
b"\xce\xa4\xce\xb7\xce\xbb\xce\xad\xce\xbc\xce\xb1\xcf\x87\xce\xbf"
440439
b"\xcf\x82",
441440
b"\x43\x66\x6c\x9b\x09\xef\x33\xed\x8c\x27\xe8\xe8\xf3\xe2\xd8\xe6",
@@ -451,12 +450,12 @@ def test_kdf(rounds, password, salt, expected):
451450

452451
def test_kdf_str_password():
453452
with pytest.raises(TypeError):
454-
bcrypt.kdf("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 10, 10)
453+
bcrypt.kdf("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 10, 10) # type: ignore[arg-type]
455454

456455

457456
def test_kdf_str_salt():
458457
with pytest.raises(TypeError):
459-
bcrypt.kdf(b"password", "salt", 10, 10)
458+
bcrypt.kdf(b"password", "salt", 10, 10) # type: ignore[arg-type]
460459

461460

462461
def test_kdf_no_warn_rounds():

tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ commands =
2121
ruff format --check .
2222

2323
[testenv:mypy]
24+
extras =
25+
tests
2426
deps =
2527
mypy
2628
commands =
27-
mypy src/bcrypt
29+
mypy tests/
2830

2931
[testenv:packaging]
3032
deps =

0 commit comments

Comments
 (0)