summaryrefslogtreecommitdiff
path: root/dev/pbkdf2.py
diff options
context:
space:
mode:
Diffstat (limited to 'dev/pbkdf2.py')
-rw-r--r--dev/pbkdf2.py103
1 files changed, 66 insertions, 37 deletions
diff --git a/dev/pbkdf2.py b/dev/pbkdf2.py
index 6a297ef85..4416f8632 100644
--- a/dev/pbkdf2.py
+++ b/dev/pbkdf2.py
@@ -59,10 +59,10 @@ else:
text_type = unicode
-_pack_int = Struct('>I').pack
+_pack_int = Struct(">I").pack
-def bytes_(s, encoding='utf8', errors='strict'):
+def bytes_(s, encoding="utf8", errors="strict"):
if isinstance(s, text_type):
return s.encode(encoding, errors)
return s
@@ -72,7 +72,7 @@ def hexlify_(s):
if PY3:
return str(hexlify(s), encoding="utf8")
else:
- return s.encode('hex')
+ return s.encode("hex")
def range_(*args):
@@ -103,6 +103,7 @@ def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
return [x for x in h.digest()]
else:
return map(ord, h.digest())
+
buf = []
for block in range_(1, -(-keylen // mac.digest_size) + 1):
rv = u = _pseudorandom(bytes_(salt) + _pack_int(block))
@@ -110,13 +111,13 @@ def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
if PY3:
u = _pseudorandom(bytes(u))
else:
- u = _pseudorandom(''.join(map(chr, u)))
+ u = _pseudorandom("".join(map(chr, u)))
rv = starmap(xor, zip(rv, u))
buf.extend(rv)
if PY3:
return bytes(buf)[:keylen]
else:
- return ''.join(map(chr, buf))[:keylen]
+ return "".join(map(chr, buf))[:keylen]
def test():
@@ -125,48 +126,76 @@ def test():
def check(data, salt, iterations, keylen, expected):
rv = pbkdf2_hex(data, salt, iterations, keylen)
if rv != expected:
- print('Test failed:')
- print(' Expected: %s' % expected)
- print(' Got: %s' % rv)
- print(' Parameters:')
- print(' data=%s' % data)
- print(' salt=%s' % salt)
- print(' iterations=%d' % iterations)
+ print("Test failed:")
+ print(" Expected: %s" % expected)
+ print(" Got: %s" % rv)
+ print(" Parameters:")
+ print(" data=%s" % data)
+ print(" salt=%s" % salt)
+ print(" iterations=%d" % iterations)
failed.append(1)
# From RFC 6070
- check('password', 'salt', 1, 20,
- '0c60c80f961f0e71f3a9b524af6012062fe037a6')
- check('password', 'salt', 2, 20,
- 'ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957')
- check('password', 'salt', 4096, 20,
- '4b007901b765489abead49d926f721d065a429c1')
- check('passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt',
- 4096, 25, '3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038')
- check('pass\x00word', 'sa\x00lt', 4096, 16,
- '56fa6aa75548099dcc37d7f03425e0c3')
+ check("password", "salt", 1, 20, "0c60c80f961f0e71f3a9b524af6012062fe037a6")
+ check("password", "salt", 2, 20, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957")
+ check("password", "salt", 4096, 20, "4b007901b765489abead49d926f721d065a429c1")
+ check(
+ "passwordPASSWORDpassword",
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt",
+ 4096,
+ 25,
+ "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
+ )
+ check("pass\x00word", "sa\x00lt", 4096, 16, "56fa6aa75548099dcc37d7f03425e0c3")
# This one is from the RFC but it just takes for ages
##check('password', 'salt', 16777216, 20,
## 'eefe3d61cd4da4e4e9945b3d6ba2158c2634e984')
# From Crypt-PBKDF2
- check('password', 'ATHENA.MIT.EDUraeburn', 1, 16,
- 'cdedb5281bb2f801565a1122b2563515')
- check('password', 'ATHENA.MIT.EDUraeburn', 1, 32,
- 'cdedb5281bb2f801565a1122b25635150ad1f7a04bb9f3a333ecc0e2e1f70837')
- check('password', 'ATHENA.MIT.EDUraeburn', 2, 16,
- '01dbee7f4a9e243e988b62c73cda935d')
- check('password', 'ATHENA.MIT.EDUraeburn', 2, 32,
- '01dbee7f4a9e243e988b62c73cda935da05378b93244ec8f48a99e61ad799d86')
- check('password', 'ATHENA.MIT.EDUraeburn', 1200, 32,
- '5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13')
- check('X' * 64, 'pass phrase equals block size', 1200, 32,
- '139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1')
- check('X' * 65, 'pass phrase exceeds block size', 1200, 32,
- '9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82a')
+ check(
+ "password", "ATHENA.MIT.EDUraeburn", 1, 16, "cdedb5281bb2f801565a1122b2563515"
+ )
+ check(
+ "password",
+ "ATHENA.MIT.EDUraeburn",
+ 1,
+ 32,
+ "cdedb5281bb2f801565a1122b25635150ad1f7a04bb9f3a333ecc0e2e1f70837",
+ )
+ check(
+ "password", "ATHENA.MIT.EDUraeburn", 2, 16, "01dbee7f4a9e243e988b62c73cda935d"
+ )
+ check(
+ "password",
+ "ATHENA.MIT.EDUraeburn",
+ 2,
+ 32,
+ "01dbee7f4a9e243e988b62c73cda935da05378b93244ec8f48a99e61ad799d86",
+ )
+ check(
+ "password",
+ "ATHENA.MIT.EDUraeburn",
+ 1200,
+ 32,
+ "5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13",
+ )
+ check(
+ "X" * 64,
+ "pass phrase equals block size",
+ 1200,
+ 32,
+ "139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1",
+ )
+ check(
+ "X" * 65,
+ "pass phrase exceeds block size",
+ 1200,
+ 32,
+ "9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82a",
+ )
raise SystemExit(bool(failed))
-if __name__ == '__main__':
+if __name__ == "__main__":
test()