|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from asv_runner.benchmarks.mark import skip_benchmark_if |
| 4 | +from django import conf |
| 5 | +from django.contrib.auth.hashers import acheck_password, make_password |
| 6 | + |
| 7 | +from ...utils import bench_setup |
| 8 | + |
| 9 | +try: |
| 10 | + import argon2 |
| 11 | +except ImportError: |
| 12 | + argon2 = None |
| 13 | + |
| 14 | +try: |
| 15 | + import bcrypt |
| 16 | +except ImportError: |
| 17 | + bcrypt = None |
| 18 | + |
| 19 | +# scrypt requires OpenSSL 1.1+ |
| 20 | +try: |
| 21 | + import hashlib |
| 22 | + |
| 23 | + scrypt = hashlib.scrypt |
| 24 | +except ImportError: |
| 25 | + scrypt = None |
| 26 | + |
| 27 | + |
| 28 | +PASSWORDS = [ |
| 29 | + "password123", |
| 30 | + "qwerty123", |
| 31 | + "helloworld123", |
| 32 | +] |
| 33 | + |
| 34 | + |
| 35 | +# async func aren't automatically benchmarked. |
| 36 | +async def op(encoded_passwords): |
| 37 | + await asyncio.gather( |
| 38 | + *[ |
| 39 | + acheck_password(password, encoded) |
| 40 | + for password, encoded in zip(PASSWORDS, encoded_passwords) |
| 41 | + ] |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class Hasher: |
| 46 | + params = [ |
| 47 | + "PBKDF2PasswordHasher", |
| 48 | + "PBKDF2SHA1PasswordHasher", |
| 49 | + ] |
| 50 | + param_names = ["hasher"] |
| 51 | + |
| 52 | + def setup(self, hasher): |
| 53 | + bench_setup() |
| 54 | + conf.settings.PASSWORD_HASHERS = [f"django.contrib.auth.hashers.{hasher}"] |
| 55 | + self.encoded_passwords = [make_password(password) for password in PASSWORDS] |
| 56 | + |
| 57 | + def time_acheck_password(self, _): |
| 58 | + asyncio.run(op(self.encoded_passwords)) |
| 59 | + |
| 60 | + |
| 61 | +class HasherArgon: |
| 62 | + def setup(self): |
| 63 | + bench_setup() |
| 64 | + conf.settings.PASSWORD_HASHERS = [ |
| 65 | + "django.contrib.auth.hashers.Argon2PasswordHasher" |
| 66 | + ] |
| 67 | + self.encoded_passwords = [make_password(password) for password in PASSWORDS] |
| 68 | + |
| 69 | + @skip_benchmark_if(argon2 is None) |
| 70 | + def time_acheck_password(self): |
| 71 | + asyncio.run(op(self.encoded_passwords)) |
| 72 | + |
| 73 | + |
| 74 | +class HasherBcrypt: |
| 75 | + def setup(self): |
| 76 | + bench_setup() |
| 77 | + conf.settings.PASSWORD_HASHERS = [ |
| 78 | + "django.contrib.auth.hashers.BCryptSHA256PasswordHasher" |
| 79 | + ] |
| 80 | + self.encoded_passwords = [make_password(password) for password in PASSWORDS] |
| 81 | + |
| 82 | + @skip_benchmark_if(bcrypt is None) |
| 83 | + def time_acheck_password(self): |
| 84 | + asyncio.run(op(self.encoded_passwords)) |
| 85 | + |
| 86 | + |
| 87 | +class HasherScypt: |
| 88 | + def setup(self): |
| 89 | + bench_setup() |
| 90 | + conf.settings.PASSWORD_HASHERS = [ |
| 91 | + "django.contrib.auth.hashers.ScryptPasswordHasher" |
| 92 | + ] |
| 93 | + self.encoded_passwords = [make_password(password) for password in PASSWORDS] |
| 94 | + |
| 95 | + @skip_benchmark_if(scrypt is None) |
| 96 | + def time_acheck_password(self): |
| 97 | + asyncio.run(op(self.encoded_passwords)) |
0 commit comments