forked from openid/python-openid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdh.py
70 lines (59 loc) · 1.63 KB
/
dh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os.path
from openid.dh import DiffieHellman, strxor
def test_strxor():
NUL = '\x00'
cases = [
(NUL, NUL, NUL),
('\x01', NUL, '\x01'),
('a', 'a', NUL),
('a', NUL, 'a'),
('abc', NUL * 3, 'abc'),
('x' * 10, NUL * 10, 'x' * 10),
('\x01', '\x02', '\x03'),
('\xf0', '\x0f', '\xff'),
('\xff', '\x0f', '\xf0'),
]
for aa, bb, expected in cases:
actual = strxor(aa, bb)
assert actual == expected, (aa, bb, expected, actual)
exc_cases = [
('', 'a'),
('foo', 'ba'),
(NUL * 3, NUL * 4),
(''.join(map(chr, xrange(256))),
''.join(map(chr, xrange(128)))),
]
for aa, bb in exc_cases:
try:
unexpected = strxor(aa, bb)
except ValueError:
pass
else:
assert False, 'Expected ValueError, got %r' % (unexpected,)
def test1():
dh1 = DiffieHellman.fromDefaults()
dh2 = DiffieHellman.fromDefaults()
secret1 = dh1.getSharedSecret(dh2.public)
secret2 = dh2.getSharedSecret(dh1.public)
assert secret1 == secret2
return secret1
def test_exchange():
s1 = test1()
s2 = test1()
assert s1 != s2
def test_public():
f = file(os.path.join(os.path.dirname(__file__), 'dhpriv'))
dh = DiffieHellman.fromDefaults()
try:
for line in f:
parts = line.strip().split(' ')
dh._setPrivate(long(parts[0]))
assert dh.public == long(parts[1])
finally:
f.close()
def test():
test_exchange()
test_public()
test_strxor()
if __name__ == '__main__':
test()