Skip to content

Commit d221dc6

Browse files
committed
Import v1.2
- Compatibility release (v1.2) - Add support for older versions of Python (2.2 and 2.3).
1 parent 55d5768 commit d221dc6

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

PBKDF2.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
###########################################################################
44
# PBKDF2.py - PKCS#5 v2.0 Password-Based Key Derivation
55
#
6-
# Copyright (C) 2007 Dwayne C. Litzenberger <[email protected]>
6+
# Copyright (C) 2007, 2008 Dwayne C. Litzenberger <[email protected]>
77
# All rights reserved.
88
#
99
# Permission to use, copy, modify, and distribute this software and its
@@ -61,15 +61,19 @@
6161
# 64 bytes (so SHA1, SHA256 and SHA512 are fine), but I don't like
6262
# anything that silently reduces the security margin from what is
6363
# expected.
64+
#
65+
# 2008-06-17 Dwayne C. Litzenberger <[email protected]>
66+
# - Compatibility release (v1.2)
67+
# - Add support for older versions of Python (2.2 and 2.3).
6468
#
6569
###########################################################################
6670

67-
__version__ = "1.1"
71+
__version__ = "1.2"
6872

6973
from struct import pack
7074
from binascii import b2a_hex
71-
from base64 import b64encode
7275
from random import randint
76+
import string
7377

7478
try:
7579
# Use PyCrypto (if available)
@@ -83,6 +87,10 @@
8387
def strxor(a, b):
8488
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b)])
8589

90+
def b64encode(data, chars="+/"):
91+
tt = string.maketrans("+/", chars)
92+
return data.encode('base64').replace("\n", "").translate(tt)
93+
8694
class PBKDF2(object):
8795
"""PBKDF2.py : PKCS#5 v2.0 Password-Based Key Derivation
8896
@@ -124,7 +132,7 @@ def read(self, bytes):
124132
i = self.__blockNum
125133
while size < bytes:
126134
i += 1
127-
if i > 0xffffffff:
135+
if i > 0xffffffffL or i < 1:
128136
# We could return "" here, but
129137
raise OverflowError("derived key too long")
130138
block = self.__f(i)
@@ -138,7 +146,7 @@ def read(self, bytes):
138146

139147
def __f(self, i):
140148
# i must fit within 32 bits
141-
assert (1 <= i and i <= 0xffffffff)
149+
assert 1 <= i <= 0xffffffffL
142150
U = self.__prf(self.__passphrase, self.__salt + pack("!L", i))
143151
result = U
144152
for j in xrange(2, 1+self.__iterations):

0 commit comments

Comments
 (0)