Mercurial > p > roundup > code
comparison roundup/password.py @ 6981:aa629aebac41
flake8 - import order, spacing
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 14 Sep 2022 20:23:42 -0400 |
| parents | 469ad03e6cb8 |
| children | cfdcaf8b5936 |
comparison
equal
deleted
inserted
replaced
| 6980:63c9680eed20 | 6981:aa629aebac41 |
|---|---|
| 17 # | 17 # |
| 18 """Password handling (encoding, decoding). | 18 """Password handling (encoding, decoding). |
| 19 """ | 19 """ |
| 20 __docformat__ = 'restructuredtext' | 20 __docformat__ = 'restructuredtext' |
| 21 | 21 |
| 22 import re, string | 22 import re |
| 23 import string | |
| 24 import warnings | |
| 25 | |
| 23 from base64 import b64encode, b64decode | 26 from base64 import b64encode, b64decode |
| 24 from hashlib import md5, sha1 | 27 from hashlib import md5, sha1 |
| 25 | 28 |
| 29 import roundup.anypy.random_ as random_ | |
| 30 | |
| 26 from roundup.anypy.strings import us2s, b2s, s2b | 31 from roundup.anypy.strings import us2s, b2s, s2b |
| 27 import roundup.anypy.random_ as random_ | 32 |
| 28 | |
| 29 import warnings | |
| 30 | 33 |
| 31 try: | 34 try: |
| 32 with warnings.catch_warnings(): | 35 with warnings.catch_warnings(): |
| 33 warnings.filterwarnings("ignore", category=DeprecationWarning) | 36 warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 34 import crypt | 37 import crypt |
| 88 from struct import pack | 91 from struct import pack |
| 89 from hmac import HMAC | 92 from hmac import HMAC |
| 90 | 93 |
| 91 def xor_bytes(left, right): | 94 def xor_bytes(left, right): |
| 92 "perform bitwise-xor of two byte-strings" | 95 "perform bitwise-xor of two byte-strings" |
| 93 return _bjoin(bchr(bord(l) ^ bord(r)) for l, r in zip(left, right)) | 96 return _bjoin(bchr(bord(l) ^ bord(r)) |
| 97 for l, r in zip(left, right)) # noqa: E741 | |
| 94 | 98 |
| 95 def _pbkdf2(password, salt, rounds, keylen): | 99 def _pbkdf2(password, salt, rounds, keylen): |
| 96 digest_size = 20 # sha1 generates 20-byte blocks | 100 digest_size = 20 # sha1 generates 20-byte blocks |
| 97 total_blocks = int((keylen+digest_size-1)/digest_size) | 101 total_blocks = int((keylen+digest_size-1)/digest_size) |
| 98 hmac_template = HMAC(password, None, sha1) | 102 hmac_template = HMAC(password, None, sha1) |
| 360 """Stringify the encrypted password for database storage.""" | 364 """Stringify the encrypted password for database storage.""" |
| 361 if self.password is None: | 365 if self.password is None: |
| 362 raise ValueError('Password not set') | 366 raise ValueError('Password not set') |
| 363 return '{%s}%s' % (self.scheme, self.password) | 367 return '{%s}%s' % (self.scheme, self.password) |
| 364 | 368 |
| 369 | |
| 365 def test_missing_crypt(): | 370 def test_missing_crypt(): |
| 366 p = encodePassword('sekrit', 'crypt') | 371 p = encodePassword('sekrit', 'crypt') # noqa: F841 - test only |
| 372 | |
| 367 | 373 |
| 368 def test(): | 374 def test(): |
| 369 # SHA | 375 # SHA |
| 370 p = Password('sekrit') | 376 p = Password('sekrit') |
| 371 assert Password(encrypted=str(p)) == 'sekrit' | 377 assert Password(encrypted=str(p)) == 'sekrit' |
