Package openid :: Module dh
[frames] | no frames]

Source Code for Module openid.dh

 1  from openid import cryptutil 
 2  from openid import oidutil 
 3   
4 -def strxor(x, y):
5 if len(x) != len(y): 6 raise ValueError('Inputs to strxor must have the same length') 7 8 xor = lambda (a, b): chr(ord(a) ^ ord(b)) 9 return "".join(map(xor, zip(x, y)))
10
11 -class DiffieHellman(object):
12 DEFAULT_MOD = 155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443L 13 14 DEFAULT_GEN = 2 15
16 - def fromDefaults(cls):
17 return cls(cls.DEFAULT_MOD, cls.DEFAULT_GEN)
18 19 fromDefaults = classmethod(fromDefaults) 20
21 - def __init__(self, modulus, generator):
22 self.modulus = long(modulus) 23 self.generator = long(generator) 24 25 self._setPrivate(cryptutil.randrange(1, modulus - 1))
26
27 - def _setPrivate(self, private):
28 """This is here to make testing easier""" 29 self.private = private 30 self.public = pow(self.generator, self.private, self.modulus)
31
32 - def usingDefaultValues(self):
33 return (self.modulus == self.DEFAULT_MOD and 34 self.generator == self.DEFAULT_GEN)
35
36 - def getSharedSecret(self, composite):
37 return pow(composite, self.private, self.modulus)
38
39 - def xorSecret(self, composite, secret, hash_func):
40 dh_shared = self.getSharedSecret(composite) 41 hashed_dh_shared = hash_func(cryptutil.longToBinary(dh_shared)) 42 return strxor(secret, hashed_dh_shared)
43