|
32 | 32 | PostbackEvent, |
33 | 33 | BeaconEvent |
34 | 34 | ) |
35 | | -from .utils import LOGGER, PY3 |
| 35 | +from .utils import LOGGER, PY3, safe_compare_digest |
| 36 | + |
| 37 | + |
| 38 | +if hasattr(hmac, "compare_digest"): |
| 39 | + def compare_digest(val1, val2): |
| 40 | + """compare_digest method. |
| 41 | +
|
| 42 | + :param val1: string or bytes for compare |
| 43 | + :type val1: str | bytes |
| 44 | + :param val2: string or bytes for compare |
| 45 | + :type val2: str | bytes |
| 46 | + :rtype: bool |
| 47 | + :return: result |
| 48 | + """ |
| 49 | + return hmac.compare_digest(val1, val2) |
| 50 | +else: |
| 51 | + def compare_digest(val1, val2): |
| 52 | + """compare_digest method. |
| 53 | +
|
| 54 | + :param val1: string or bytes for compare |
| 55 | + :type val1: str | bytes |
| 56 | + :param val2: string or bytes for compare |
| 57 | + :type val2: str | bytes |
| 58 | + :rtype: bool |
| 59 | + :return: result |
| 60 | + """ |
| 61 | + return safe_compare_digest(val1, val2) |
36 | 62 |
|
37 | 63 |
|
38 | 64 | class SignatureValidator(object): |
@@ -64,8 +90,8 @@ def validate(self, body, signature): |
64 | 90 | hashlib.sha256 |
65 | 91 | ).digest() |
66 | 92 |
|
67 | | - return hmac.compare_digest( |
68 | | - signature.encode('utf-8'), base64.b64encode(gen_signature) |
| 93 | + return compare_digest( |
| 94 | + signature.encode('utf-8'), base64.b64encode(gen_signature) |
69 | 95 | ) |
70 | 96 |
|
71 | 97 |
|
|
0 commit comments