| 18 | | def check_password(raw_password, enc_password): |
|---|
| 19 | | """ |
|---|
| 20 | | Returns a boolean of whether the raw_password was correct. Handles |
|---|
| 21 | | encryption formats behind the scenes. |
|---|
| 22 | | """ |
|---|
| 23 | | algo, salt, hsh = enc_password.split('$') |
|---|
| 24 | | if algo == 'md5': |
|---|
| 25 | | import md5 |
|---|
| 26 | | return hsh == md5.new(smart_str(salt + raw_password)).hexdigest() |
|---|
| 27 | | elif algo == 'sha1': |
|---|
| 28 | | import sha |
|---|
| 29 | | return hsh == sha.new(smart_str(salt + raw_password)).hexdigest() |
|---|
| 30 | | elif algo == 'crypt': |
|---|
| | 18 | def get_hexdigest(algorithm, salt, raw_password): |
|---|
| | 19 | """ |
|---|
| | 20 | Returns a string of the hexdigest of the given plaintext password and salt |
|---|
| | 21 | using the given algorithm ('md5', 'sha1' or 'crypt'). |
|---|
| | 22 | """ |
|---|
| | 23 | raw_password, salt = smart_str(raw_password), smart_str(salt) |
|---|
| | 24 | if algorithm == 'crypt': |
|---|
| 34 | | raise ValueError, "Crypt password algorithm not supported in this environment." |
|---|
| 35 | | return hsh == crypt.crypt(smart_str(raw_password), smart_str(salt)) |
|---|
| 36 | | raise ValueError, "Got unknown password algorithm type in password." |
|---|
| | 28 | raise ValueError('"crypt" password algorithm not supported in this environment') |
|---|
| | 29 | return crypt.crypt(raw_password, salt) |
|---|
| | 30 | # The rest of the supported algorithms are supported by hashlib, but |
|---|
| | 31 | # hashlib is only available in Python 2.5. |
|---|
| | 32 | try: |
|---|
| | 33 | import hashlib |
|---|
| | 34 | except ImportError: |
|---|
| | 35 | if algorithm == 'md5': |
|---|
| | 36 | import md5 |
|---|
| | 37 | return md5.new(salt + raw_password).hexdigest() |
|---|
| | 38 | elif algorithm == 'sha1': |
|---|
| | 39 | import sha |
|---|
| | 40 | return sha.new(salt + raw_password).hexdigest() |
|---|
| | 41 | else: |
|---|
| | 42 | if algorithm == 'md5': |
|---|
| | 43 | return hashlib.md5(salt + raw_password).hexdigest() |
|---|
| | 44 | elif algorithm == 'sha1': |
|---|
| | 45 | return hashlib.sha1(salt + raw_password).hexdigest() |
|---|
| | 46 | raise ValueError("Got unknown password algorithm type in password.") |
|---|
| | 47 | |
|---|
| | 48 | def check_password(raw_password, enc_password): |
|---|
| | 49 | """ |
|---|
| | 50 | Returns a boolean of whether the raw_password was correct. Handles |
|---|
| | 51 | encryption formats behind the scenes. |
|---|
| | 52 | """ |
|---|
| | 53 | algo, salt, hsh = enc_password.split('$') |
|---|
| | 54 | return hsh == get_hexdigest(algo, salt, raw_password) |
|---|