| 1 |
From: Justin Turner Arthur <[email protected]> |
| 2 |
Date: Mon, 9 Apr 2018 03:20:00 -0500 |
| 3 |
Origin: https://github.com/dlitz/python-pbkdf2/pull/5/commits/d9b45bcedd4ce414bfb9403fda3e6868a791b717 |
| 4 |
Forwarded: no (PR in Github) |
| 5 |
Subject: [PATCH] Use faster all-at-once xor on Python 3.2+. |
| 6 |
|
| 7 |
--- |
| 8 |
pbkdf2.py | 12 ++++++++++-- |
| 9 |
1 file changed, 10 insertions(+), 2 deletions(-) |
| 10 |
|
| 11 |
diff --git a/pbkdf2.py b/pbkdf2.py |
| 12 |
index 937a99a..6adc96f 100644 |
| 13 |
--- a/pbkdf2.py |
| 14 |
+++ b/pbkdf2.py |
| 15 |
@@ -101,8 +101,16 @@ def callable(obj): |
| 16 |
return hasattr(obj, '__call__') |
| 17 |
def b(s): |
| 18 |
return s.encode("latin-1") |
| 19 |
|
| 20 |
|
| 21 |
+ if hasattr(int, 'to_bytes') and hasattr(int, 'from_bytes'): |
| 22 |
+ _sys_byteorder = sys.byteorder |
| 23 |
+ _b2i = int.from_bytes |
| 24 |
+ def binxor(a, b): |
| 25 |
+ a_int = _b2i(a, _sys_byteorder) |
| 26 |
+ b_int = _b2i(b, _sys_byteorder) |
| 27 |
+ return (a_int ^ b_int).to_bytes(len(a), _sys_byteorder) |
| 28 |
+ else: |
| 29 |
+ def binxor(a, b): |
| 30 |
+ return bytes([x ^ y for (x, y) in zip(a, b)]) |
| 31 |
from base64 import b64encode as _b64encode |
| 32 |
def b64encode(data, chars="+/"): |
| 33 |
if isunicode(chars): |