| 1 |
import uuid |
| 2 |
from concurrent.futures import ThreadPoolExecutor |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
import bcrypt |
| 7 |
|
| 8 |
_test_vectors = [ |
| 9 |
( |
| 10 |
b"Kk4DQuMMfZL9o", |
| 11 |
b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", |
| 12 |
b"$2b$04$cVWp4XaNU8a4v1uMRum2SO026BWLIoQMD/TXg5uZV.0P.uO8m3YEm", |
| 13 |
), |
| 14 |
( |
| 15 |
b"9IeRXmnGxMYbs", |
| 16 |
b"$2b$04$pQ7gRO7e6wx/936oXhNjrO", |
| 17 |
b"$2b$04$pQ7gRO7e6wx/936oXhNjrOUNOHL1D0h1N2IDbJZYs.1ppzSof6SPy", |
| 18 |
), |
| 19 |
( |
| 20 |
b"xVQVbwa1S0M8r", |
| 21 |
b"$2b$04$SQe9knOzepOVKoYXo9xTte", |
| 22 |
b"$2b$04$SQe9knOzepOVKoYXo9xTteNYr6MBwVz4tpriJVe3PNgYufGIsgKcW", |
| 23 |
), |
| 24 |
( |
| 25 |
b"Zfgr26LWd22Za", |
| 26 |
b"$2b$04$eH8zX.q5Q.j2hO1NkVYJQO", |
| 27 |
b"$2b$04$eH8zX.q5Q.j2hO1NkVYJQOM6KxntS/ow3.YzVmFrE4t//CoF4fvne", |
| 28 |
), |
| 29 |
( |
| 30 |
b"Tg4daC27epFBE", |
| 31 |
b"$2b$04$ahiTdwRXpUG2JLRcIznxc.", |
| 32 |
b"$2b$04$ahiTdwRXpUG2JLRcIznxc.s1.ydaPGD372bsGs8NqyYjLY1inG5n2", |
| 33 |
), |
| 34 |
( |
| 35 |
b"xhQPMmwh5ALzW", |
| 36 |
b"$2b$04$nQn78dV0hGHf5wUBe0zOFu", |
| 37 |
b"$2b$04$nQn78dV0hGHf5wUBe0zOFu8n07ZbWWOKoGasZKRspZxtt.vBRNMIy", |
| 38 |
), |
| 39 |
( |
| 40 |
b"59je8h5Gj71tg", |
| 41 |
b"$2b$04$cvXudZ5ugTg95W.rOjMITu", |
| 42 |
b"$2b$04$cvXudZ5ugTg95W.rOjMITuM1jC0piCl3zF5cmGhzCibHZrNHkmckG", |
| 43 |
), |
| 44 |
( |
| 45 |
b"wT4fHJa2N9WSW", |
| 46 |
b"$2b$04$YYjtiq4Uh88yUsExO0RNTu", |
| 47 |
b"$2b$04$YYjtiq4Uh88yUsExO0RNTuEJ.tZlsONac16A8OcLHleWFjVawfGvO", |
| 48 |
), |
| 49 |
( |
| 50 |
b"uSgFRnQdOgm4S", |
| 51 |
b"$2b$04$WLTjgY/pZSyqX/fbMbJzf.", |
| 52 |
b"$2b$04$WLTjgY/pZSyqX/fbMbJzf.qxCeTMQOzgL.CimRjMHtMxd/VGKojMu", |
| 53 |
), |
| 54 |
( |
| 55 |
b"tEPtJZXur16Vg", |
| 56 |
b"$2b$04$2moPs/x/wnCfeQ5pCheMcu", |
| 57 |
b"$2b$04$2moPs/x/wnCfeQ5pCheMcuSJQ/KYjOZG780UjA/SiR.KsYWNrC7SG", |
| 58 |
), |
| 59 |
( |
| 60 |
b"vvho8C6nlVf9K", |
| 61 |
b"$2b$04$HrEYC/AQ2HS77G78cQDZQ.", |
| 62 |
b"$2b$04$HrEYC/AQ2HS77G78cQDZQ.r44WGcruKw03KHlnp71yVQEwpsi3xl2", |
| 63 |
), |
| 64 |
( |
| 65 |
b"5auCCY9by0Ruf", |
| 66 |
b"$2b$04$vVYgSTfB8KVbmhbZE/k3R.", |
| 67 |
b"$2b$04$vVYgSTfB8KVbmhbZE/k3R.ux9A0lJUM4CZwCkHI9fifke2.rTF7MG", |
| 68 |
), |
| 69 |
( |
| 70 |
b"GtTkR6qn2QOZW", |
| 71 |
b"$2b$04$JfoNrR8.doieoI8..F.C1O", |
| 72 |
b"$2b$04$JfoNrR8.doieoI8..F.C1OQgwE3uTeuardy6lw0AjALUzOARoyf2m", |
| 73 |
), |
| 74 |
( |
| 75 |
b"zKo8vdFSnjX0f", |
| 76 |
b"$2b$04$HP3I0PUs7KBEzMBNFw7o3O", |
| 77 |
b"$2b$04$HP3I0PUs7KBEzMBNFw7o3O7f/uxaZU7aaDot1quHMgB2yrwBXsgyy", |
| 78 |
), |
| 79 |
( |
| 80 |
b"I9VfYlacJiwiK", |
| 81 |
b"$2b$04$xnFVhJsTzsFBTeP3PpgbMe", |
| 82 |
b"$2b$04$xnFVhJsTzsFBTeP3PpgbMeMREb6rdKV9faW54Sx.yg9plf4jY8qT6", |
| 83 |
), |
| 84 |
( |
| 85 |
b"VFPO7YXnHQbQO", |
| 86 |
b"$2b$04$WQp9.igoLqVr6Qk70mz6xu", |
| 87 |
b"$2b$04$WQp9.igoLqVr6Qk70mz6xuRxE0RttVXXdukpR9N54x17ecad34ZF6", |
| 88 |
), |
| 89 |
( |
| 90 |
b"VDx5BdxfxstYk", |
| 91 |
b"$2b$04$xgZtlonpAHSU/njOCdKztO", |
| 92 |
b"$2b$04$xgZtlonpAHSU/njOCdKztOPuPFzCNVpB4LGicO4/OGgHv.uKHkwsS", |
| 93 |
), |
| 94 |
( |
| 95 |
b"dEe6XfVGrrfSH", |
| 96 |
b"$2b$04$2Siw3Nv3Q/gTOIPetAyPr.", |
| 97 |
b"$2b$04$2Siw3Nv3Q/gTOIPetAyPr.GNj3aO0lb1E5E9UumYGKjP9BYqlNWJe", |
| 98 |
), |
| 99 |
( |
| 100 |
b"cTT0EAFdwJiLn", |
| 101 |
b"$2b$04$7/Qj7Kd8BcSahPO4khB8me", |
| 102 |
b"$2b$04$7/Qj7Kd8BcSahPO4khB8me4ssDJCW3r4OGYqPF87jxtrSyPj5cS5m", |
| 103 |
), |
| 104 |
( |
| 105 |
b"J8eHUDuxBB520", |
| 106 |
b"$2b$04$VvlCUKbTMjaxaYJ.k5juoe", |
| 107 |
b"$2b$04$VvlCUKbTMjaxaYJ.k5juoecpG/7IzcH1AkmqKi.lIZMVIOLClWAk.", |
| 108 |
), |
| 109 |
( |
| 110 |
b"U*U", |
| 111 |
b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.", |
| 112 |
b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW", |
| 113 |
), |
| 114 |
( |
| 115 |
b"U*U*", |
| 116 |
b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.", |
| 117 |
b"$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK", |
| 118 |
), |
| 119 |
( |
| 120 |
b"U*U*U", |
| 121 |
b"$2a$05$XXXXXXXXXXXXXXXXXXXXXO", |
| 122 |
b"$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a", |
| 123 |
), |
| 124 |
( |
| 125 |
b"\xa3", |
| 126 |
b"$2a$05$/OK.fbVrR/bpIqNJ5ianF.", |
| 127 |
b"$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq", |
| 128 |
), |
| 129 |
( |
| 130 |
b"}>\xb3\xfe\xf1\x8b\xa0\xe6(\xa2Lzq\xc3P\x7f\xcc\xc8b{\xf9\x14\xf6" |
| 131 |
b"\xf6`\x81G5\xec\x1d\x87\x10\xbf\xa7\xe1}I7 \x96\xdfc\xf2\xbf\xb3Vh" |
| 132 |
b"\xdfM\x88q\xf7\xff\x1b\x82~z\x13\xdd\xe9\x84\x00\xdd4", |
| 133 |
b"$2b$10$keO.ZZs22YtygVF6BLfhGO", |
| 134 |
b"$2b$10$keO.ZZs22YtygVF6BLfhGOI/JjshJYPp8DZsUtym6mJV2Eha2Hdd.", |
| 135 |
), |
| 136 |
( |
| 137 |
b"g7\r\x01\xf3\xd4\xd0\xa9JB^\x18\x007P\xb2N\xc7\x1c\xee\x87&\x83C" |
| 138 |
b"\x8b\xe8\x18\xc5>\x86\x14/\xd6\xcc\x1cJ\xde\xd7ix\xeb\xdeO\xef" |
| 139 |
b"\xe1i\xac\xcb\x03\x96v1' \[email protected]\xa5!\xa0\xef\xc0(", |
| 140 |
b"$2a$04$tecY.9ylRInW/rAAzXCXPO", |
| 141 |
b"$2a$04$tecY.9ylRInW/rAAzXCXPOOlyYeCNzmNTzPDNSIFztFMKbvs/s5XG", |
| 142 |
), |
| 143 |
] |
| 144 |
|
| 145 |
_2y_test_vectors = [ |
| 146 |
( |
| 147 |
b"\xa3", |
| 148 |
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq", |
| 149 |
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq", |
| 150 |
), |
| 151 |
( |
| 152 |
b"\xff\xff\xa3", |
| 153 |
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e", |
| 154 |
b"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e", |
| 155 |
), |
| 156 |
] |
| 157 |
|
| 158 |
|
| 159 |
def test_gensalt_basic(): |
| 160 |
salt = bcrypt.gensalt() |
| 161 |
assert salt.startswith(b"$2b$12$") |
| 162 |
|
| 163 |
|
| 164 |
@pytest.mark.parametrize( |
| 165 |
("rounds", "expected_prefix"), |
| 166 |
[ |
| 167 |
(4, b"$2b$04$"), |
| 168 |
(5, b"$2b$05$"), |
| 169 |
(6, b"$2b$06$"), |
| 170 |
(7, b"$2b$07$"), |
| 171 |
(8, b"$2b$08$"), |
| 172 |
(9, b"$2b$09$"), |
| 173 |
(10, b"$2b$10$"), |
| 174 |
(11, b"$2b$11$"), |
| 175 |
(12, b"$2b$12$"), |
| 176 |
(13, b"$2b$13$"), |
| 177 |
(14, b"$2b$14$"), |
| 178 |
(15, b"$2b$15$"), |
| 179 |
(16, b"$2b$16$"), |
| 180 |
(17, b"$2b$17$"), |
| 181 |
(18, b"$2b$18$"), |
| 182 |
(19, b"$2b$19$"), |
| 183 |
(20, b"$2b$20$"), |
| 184 |
(21, b"$2b$21$"), |
| 185 |
(22, b"$2b$22$"), |
| 186 |
(23, b"$2b$23$"), |
| 187 |
(24, b"$2b$24$"), |
| 188 |
], |
| 189 |
) |
| 190 |
def test_gensalt_rounds_valid(rounds, expected_prefix): |
| 191 |
salt = bcrypt.gensalt(rounds) |
| 192 |
|
| 193 |
assert salt.startswith(expected_prefix) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.parametrize("rounds", list(range(1, 4))) |
| 197 |
def test_gensalt_rounds_invalid(rounds): |
| 198 |
with pytest.raises(ValueError): |
| 199 |
bcrypt.gensalt(rounds) |
| 200 |
|
| 201 |
|
| 202 |
def test_gensalt_bad_prefix(): |
| 203 |
with pytest.raises(ValueError): |
| 204 |
bcrypt.gensalt(prefix=b"bad") |
| 205 |
|
| 206 |
|
| 207 |
def test_gensalt_2a_prefix(): |
| 208 |
salt = bcrypt.gensalt(prefix=b"2a") |
| 209 |
assert salt.startswith(b"$2a$12$") |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.parametrize(("password", "salt", "hashed"), _test_vectors) |
| 213 |
def test_hashpw_new(password, salt, hashed): |
| 214 |
assert bcrypt.hashpw(password, salt) == hashed |
| 215 |
|
| 216 |
|
| 217 |
@pytest.mark.parametrize(("password", "salt", "hashed"), _test_vectors) |
| 218 |
def test_checkpw(password, salt, hashed): |
| 219 |
assert bcrypt.checkpw(password, hashed) is True |
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.parametrize(("password", "salt", "hashed"), _test_vectors) |
| 223 |
def test_hashpw_existing(password, salt, hashed): |
| 224 |
assert bcrypt.hashpw(password, hashed) == hashed |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.parametrize(("password", "hashed", "expected"), _2y_test_vectors) |
| 228 |
def test_hashpw_2y_prefix(password, hashed, expected): |
| 229 |
assert bcrypt.hashpw(password, hashed) == expected |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.parametrize(("password", "hashed", "expected"), _2y_test_vectors) |
| 233 |
def test_checkpw_2y_prefix(password, hashed, expected): |
| 234 |
assert bcrypt.checkpw(password, hashed) is True |
| 235 |
|
| 236 |
|
| 237 |
@pytest.mark.parametrize( |
| 238 |
("pw_length", "should_raise"), |
| 239 |
[ |
| 240 |
(71, False), |
| 241 |
(72, False), |
| 242 |
(73, True), |
| 243 |
], |
| 244 |
) |
| 245 |
def test_hashpw_raises_correctly_for_long_passwords(pw_length, should_raise): |
| 246 |
password = b"\xaa" * pw_length |
| 247 |
salt = b"$2b$04$xnFVhJsTzsFBTeP3PpgbMe" |
| 248 |
|
| 249 |
if should_raise: |
| 250 |
with pytest.raises(ValueError): |
| 251 |
bcrypt.hashpw(password, salt) |
| 252 |
else: |
| 253 |
bcrypt.hashpw(password, salt) |
| 254 |
|
| 255 |
|
| 256 |
def test_hashpw_invalid(): |
| 257 |
with pytest.raises(ValueError): |
| 258 |
bcrypt.hashpw(b"password", b"$2z$04$cVWp4XaNU8a4v1uMRum2SO") |
| 259 |
|
| 260 |
|
| 261 |
def test_checkpw_wrong_password(): |
| 262 |
assert ( |
| 263 |
bcrypt.checkpw( |
| 264 |
b"badpass", |
| 265 |
b"$2b$04$2Siw3Nv3Q/gTOIPetAyPr.GNj3aO0lb1E5E9UumYGKjP9BYqlNWJe", |
| 266 |
) |
| 267 |
is False |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
def test_checkpw_bad_salt(): |
| 272 |
with pytest.raises(ValueError): |
| 273 |
bcrypt.checkpw( |
| 274 |
b"badpass", |
| 275 |
b"$2b$04$?Siw3Nv3Q/gTOIPetAyPr.GNj3aO0lb1E5E9UumYGKjP9BYqlNWJe", |
| 276 |
) |
| 277 |
with pytest.raises(ValueError): |
| 278 |
bcrypt.checkpw( |
| 279 |
b"password", |
| 280 |
b"$2b$3$mdEQPMOtfPX.WGZNXgF66OhmBlOGKEd66SQ7DyJPGucYYmvTJYviy", |
| 281 |
) |
| 282 |
with pytest.raises(ValueError): |
| 283 |
bcrypt.checkpw(b"password", b"$2b$12$incorrect") |
| 284 |
|
| 285 |
|
| 286 |
def test_checkpw_str_password(): |
| 287 |
with pytest.raises(TypeError): |
| 288 |
bcrypt.checkpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type] |
| 289 |
|
| 290 |
|
| 291 |
def test_checkpw_str_salt(): |
| 292 |
with pytest.raises(TypeError): |
| 293 |
bcrypt.checkpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type] |
| 294 |
|
| 295 |
|
| 296 |
def test_hashpw_str_password(): |
| 297 |
with pytest.raises(TypeError): |
| 298 |
bcrypt.hashpw("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type] |
| 299 |
|
| 300 |
|
| 301 |
def test_hashpw_str_salt(): |
| 302 |
with pytest.raises(TypeError): |
| 303 |
bcrypt.hashpw(b"password", "$2b$04$cVWp4XaNU8a4v1uMRum2SO") # type: ignore[arg-type] |
| 304 |
|
| 305 |
|
| 306 |
def test_checkpw_nul_byte(): |
| 307 |
bcrypt.checkpw( |
| 308 |
b"abc\0def", |
| 309 |
b"$2b$04$2Siw3Nv3Q/gTOIPetAyPr.GNj3aO0lb1E5E9UumYGKjP9BYqlNWJe", |
| 310 |
) |
| 311 |
|
| 312 |
with pytest.raises(ValueError): |
| 313 |
bcrypt.checkpw( |
| 314 |
b"abcdef", |
| 315 |
b"$2b$04$2S\0w3Nv3Q/gTOIPetAyPr.GNj3aO0lb1E5E9UumYGKjP9BYqlNWJe", |
| 316 |
) |
| 317 |
|
| 318 |
|
| 319 |
def test_hashpw_nul_byte(): |
| 320 |
salt = bcrypt.gensalt(4) |
| 321 |
hashed = bcrypt.hashpw(b"abc\0def", salt) |
| 322 |
assert bcrypt.checkpw(b"abc\0def", hashed) |
| 323 |
# assert that we are sensitive to changes in the password after the first |
| 324 |
# null byte: |
| 325 |
assert not bcrypt.checkpw(b"abc\0deg", hashed) |
| 326 |
assert not bcrypt.checkpw(b"abc\0def\0", hashed) |
| 327 |
assert not bcrypt.checkpw(b"abc\0def\0\0", hashed) |
| 328 |
|
| 329 |
|
| 330 |
def test_checkpw_extra_data(): |
| 331 |
salt = bcrypt.gensalt(4) |
| 332 |
hashed = bcrypt.hashpw(b"abc", salt) |
| 333 |
|
| 334 |
assert bcrypt.checkpw(b"abc", hashed) |
| 335 |
assert bcrypt.checkpw(b"abc", hashed + b"extra") is False |
| 336 |
assert bcrypt.checkpw(b"abc", hashed[:-10]) is False |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.parametrize( |
| 340 |
("rounds", "password", "salt", "expected"), |
| 341 |
[ |
| 342 |
[ |
| 343 |
4, |
| 344 |
b"password", |
| 345 |
b"salt", |
| 346 |
b"\x5b\xbf\x0c\xc2\x93\x58\x7f\x1c\x36\x35\x55\x5c\x27\x79\x65\x98" |
| 347 |
b"\xd4\x7e\x57\x90\x71\xbf\x42\x7e\x9d\x8f\xbe\x84\x2a\xba\x34\xd9", |
| 348 |
], |
| 349 |
[ |
| 350 |
4, |
| 351 |
b"password", |
| 352 |
b"\x00", |
| 353 |
b"\xc1\x2b\x56\x62\x35\xee\xe0\x4c\x21\x25\x98\x97\x0a\x57\x9a\x67", |
| 354 |
], |
| 355 |
[ |
| 356 |
4, |
| 357 |
b"\x00", |
| 358 |
b"salt", |
| 359 |
b"\x60\x51\xbe\x18\xc2\xf4\xf8\x2c\xbf\x0e\xfe\xe5\x47\x1b\x4b\xb9", |
| 360 |
], |
| 361 |
[ |
| 362 |
# nul bytes in password and string |
| 363 |
4, |
| 364 |
b"password\x00", |
| 365 |
b"salt\x00", |
| 366 |
b"\x74\x10\xe4\x4c\xf4\xfa\x07\xbf\xaa\xc8\xa9\x28\xb1\x72\x7f\xac" |
| 367 |
b"\x00\x13\x75\xe7\xbf\x73\x84\x37\x0f\x48\xef\xd1\x21\x74\x30\x50", |
| 368 |
], |
| 369 |
[ |
| 370 |
4, |
| 371 |
b"pass\x00wor", |
| 372 |
b"sa\0l", |
| 373 |
b"\xc2\xbf\xfd\x9d\xb3\x8f\x65\x69\xef\xef\x43\x72\xf4\xde\x83\xc0", |
| 374 |
], |
| 375 |
[ |
| 376 |
4, |
| 377 |
b"pass\x00word", |
| 378 |
b"sa\0lt", |
| 379 |
b"\x4b\xa4\xac\x39\x25\xc0\xe8\xd7\xf0\xcd\xb6\xbb\x16\x84\xa5\x6f", |
| 380 |
], |
| 381 |
[ |
| 382 |
# bigger key |
| 383 |
8, |
| 384 |
b"password", |
| 385 |
b"salt", |
| 386 |
b"\xe1\x36\x7e\xc5\x15\x1a\x33\xfa\xac\x4c\xc1\xc1\x44\xcd\x23\xfa" |
| 387 |
b"\x15\xd5\x54\x84\x93\xec\xc9\x9b\x9b\x5d\x9c\x0d\x3b\x27\xbe\xc7" |
| 388 |
b"\x62\x27\xea\x66\x08\x8b\x84\x9b\x20\xab\x7a\xa4\x78\x01\x02\x46" |
| 389 |
b"\xe7\x4b\xba\x51\x72\x3f\xef\xa9\xf9\x47\x4d\x65\x08\x84\x5e\x8d", |
| 390 |
], |
| 391 |
[ |
| 392 |
# more rounds |
| 393 |
42, |
| 394 |
b"password", |
| 395 |
b"salt", |
| 396 |
b"\x83\x3c\xf0\xdc\xf5\x6d\xb6\x56\x08\xe8\xf0\xdc\x0c\xe8\x82\xbd", |
| 397 |
], |
| 398 |
[ |
| 399 |
# longer password |
| 400 |
8, |
| 401 |
b"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do" |
| 402 |
b" eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " |
| 403 |
b"enim ad minim veniam, quis nostrud exercitation ullamco laboris " |
| 404 |
b"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor " |
| 405 |
b"in reprehenderit in voluptate velit esse cillum dolore eu fugiat" |
| 406 |
b" nulla pariatur. Excepteur sint occaecat cupidatat non proident," |
| 407 |
b" sunt in culpa qui officia deserunt mollit anim id est laborum.", |
| 408 |
b"salis\x00", |
| 409 |
b"\x10\x97\x8b\x07\x25\x3d\xf5\x7f\x71\xa1\x62\xeb\x0e\x8a\xd3\x0a", |
| 410 |
], |
| 411 |
[ |
| 412 |
# "unicode" |
| 413 |
8, |
| 414 |
b"\x0d\xb3\xac\x94\xb3\xee\x53\x28\x4f\x4a\x22\x89\x3b\x3c\x24\xae", |
| 415 |
b"\x3a\x62\xf0\xf0\xdb\xce\xf8\x23\xcf\xcc\x85\x48\x56\xea\x10\x28", |
| 416 |
b"\x20\x44\x38\x17\x5e\xee\x7c\xe1\x36\xc9\x1b\x49\xa6\x79\x23\xff", |
| 417 |
], |
| 418 |
[ |
| 419 |
# very large key |
| 420 |
8, |
| 421 |
b"\x0d\xb3\xac\x94\xb3\xee\x53\x28\x4f\x4a\x22\x89\x3b\x3c\x24\xae", |
| 422 |
b"\x3a\x62\xf0\xf0\xdb\xce\xf8\x23\xcf\xcc\x85\x48\x56\xea\x10\x28", |
| 423 |
b"\x20\x54\xb9\xff\xf3\x4e\x37\x21\x44\x03\x34\x74\x68\x28\xe9\xed" |
| 424 |
b"\x38\xde\x4b\x72\xe0\xa6\x9a\xdc\x17\x0a\x13\xb5\xe8\xd6\x46\x38" |
| 425 |
b"\x5e\xa4\x03\x4a\xe6\xd2\x66\x00\xee\x23\x32\xc5\xed\x40\xad\x55" |
| 426 |
b"\x7c\x86\xe3\x40\x3f\xbb\x30\xe4\xe1\xdc\x1a\xe0\x6b\x99\xa0\x71" |
| 427 |
b"\x36\x8f\x51\x8d\x2c\x42\x66\x51\xc9\xe7\xe4\x37\xfd\x6c\x91\x5b" |
| 428 |
b"\x1b\xbf\xc3\xa4\xce\xa7\x14\x91\x49\x0e\xa7\xaf\xb7\xdd\x02\x90" |
| 429 |
b"\xa6\x78\xa4\xf4\x41\x12\x8d\xb1\x79\x2e\xab\x27\x76\xb2\x1e\xb4" |
| 430 |
b"\x23\x8e\x07\x15\xad\xd4\x12\x7d\xff\x44\xe4\xb3\xe4\xcc\x4c\x4f" |
| 431 |
b"\x99\x70\x08\x3f\x3f\x74\xbd\x69\x88\x73\xfd\xf6\x48\x84\x4f\x75" |
| 432 |
b"\xc9\xbf\x7f\x9e\x0c\x4d\x9e\x5d\x89\xa7\x78\x39\x97\x49\x29\x66" |
| 433 |
b"\x61\x67\x07\x61\x1c\xb9\x01\xde\x31\xa1\x97\x26\xb6\xe0\x8c\x3a" |
| 434 |
b"\x80\x01\x66\x1f\x2d\x5c\x9d\xcc\x33\xb4\xaa\x07\x2f\x90\xdd\x0b" |
| 435 |
b"\x3f\x54\x8d\x5e\xeb\xa4\x21\x13\x97\xe2\xfb\x06\x2e\x52\x6e\x1d" |
| 436 |
b"\x68\xf4\x6a\x4c\xe2\x56\x18\x5b\x4b\xad\xc2\x68\x5f\xbe\x78\xe1" |
| 437 |
b"\xc7\x65\x7b\x59\xf8\x3a\xb9\xab\x80\xcf\x93\x18\xd6\xad\xd1\xf5" |
| 438 |
b"\x93\x3f\x12\xd6\xf3\x61\x82\xc8\xe8\x11\x5f\x68\x03\x0a\x12\x44", |
| 439 |
], |
| 440 |
[ |
| 441 |
# UTF-8 Greek characters "odysseus" / "telemachos" |
| 442 |
8, |
| 443 |
b"\xe1\xbd\x88\xce\xb4\xcf\x85\xcf\x83\xcf\x83\xce\xb5\xcf\x8d\xcf\x82", |
| 444 |
b"\xce\xa4\xce\xb7\xce\xbb\xce\xad\xce\xbc\xce\xb1\xcf\x87\xce\xbf" |
| 445 |
b"\xcf\x82", |
| 446 |
b"\x43\x66\x6c\x9b\x09\xef\x33\xed\x8c\x27\xe8\xe8\xf3\xe2\xd8\xe6", |
| 447 |
], |
| 448 |
], |
| 449 |
) |
| 450 |
def test_kdf(rounds, password, salt, expected): |
| 451 |
derived = bcrypt.kdf( |
| 452 |
password, salt, len(expected), rounds, ignore_few_rounds=True |
| 453 |
) |
| 454 |
assert derived == expected |
| 455 |
|
| 456 |
|
| 457 |
def test_kdf_str_password(): |
| 458 |
with pytest.raises(TypeError): |
| 459 |
bcrypt.kdf("password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 10, 10) # type: ignore[arg-type] |
| 460 |
|
| 461 |
|
| 462 |
def test_kdf_str_salt(): |
| 463 |
with pytest.raises(TypeError): |
| 464 |
bcrypt.kdf(b"password", "salt", 10, 10) # type: ignore[arg-type] |
| 465 |
|
| 466 |
|
| 467 |
def test_kdf_no_warn_rounds(): |
| 468 |
bcrypt.kdf(b"password", b"salt", 10, 10, True) |
| 469 |
|
| 470 |
|
| 471 |
def test_kdf_warn_rounds(): |
| 472 |
with pytest.warns(UserWarning): |
| 473 |
bcrypt.kdf(b"password", b"salt", 10, 10) |
| 474 |
|
| 475 |
|
| 476 |
@pytest.mark.parametrize( |
| 477 |
("password", "salt", "desired_key_bytes", "rounds", "error"), |
| 478 |
[ |
| 479 |
("pass", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 10, 10, TypeError), |
| 480 |
(b"password", "salt", 10, 10, TypeError), |
| 481 |
(b"", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 10, 10, ValueError), |
| 482 |
(b"password", b"", 10, 10, ValueError), |
| 483 |
(b"password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 0, 10, ValueError), |
| 484 |
(b"password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", -3, 10, OverflowError), |
| 485 |
(b"password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 513, 10, ValueError), |
| 486 |
(b"password", b"$2b$04$cVWp4XaNU8a4v1uMRum2SO", 20, 0, ValueError), |
| 487 |
], |
| 488 |
) |
| 489 |
def test_invalid_params(password, salt, desired_key_bytes, rounds, error): |
| 490 |
with pytest.raises(error): |
| 491 |
bcrypt.kdf(password, salt, desired_key_bytes, rounds) |
| 492 |
|
| 493 |
|
| 494 |
def test_multithreading(): |
| 495 |
def create_user(pw): |
| 496 |
salt = bcrypt.gensalt(4) |
| 497 |
hash_ = bcrypt.hashpw(pw, salt) |
| 498 |
key = bcrypt.kdf(pw, salt, 32, 50) |
| 499 |
assert bcrypt.checkpw(pw, hash_) |
| 500 |
return (salt, hash_, key) |
| 501 |
|
| 502 |
user_creator = ThreadPoolExecutor(max_workers=4) |
| 503 |
pws = [uuid.uuid4().bytes for _ in range(50)] |
| 504 |
|
| 505 |
futures = [user_creator.submit(create_user, pw) for pw in pws] |
| 506 |
|
| 507 |
users = [future.result() for future in futures] |
| 508 |
|
| 509 |
for pw, (salt, hash_, key) in zip(pws, users): |
| 510 |
assert bcrypt.hashpw(pw, salt) == hash_ |
| 511 |
assert bcrypt.checkpw(pw, hash_) |
| 512 |
assert bcrypt.kdf(pw, salt, 32, 50) == key |