def _generateRandomID( self, c1 = constants.CRYPTO_CHALLENGE_C1, c2 = constants.CRYPTO_CHALLENGE_C2): """Generates the NodeID by solving two cryptographic puzzles.""" # Solve the static cryptographic puzzle. rsaKey = None p = 0x1 # non-zero value pub = None randomStream = Crypto.Random.new().read while not util.hasNZeroBitPrefix(p, c1): rsaKey = Crypto.PublicKey.RSA.generate(constants.RSA_BITS, randomStream) pub = str(rsaKey.n) + str(rsaKey.e) p = util.hsh2int(Crypto.Hash.SHA.new(Crypto.Hash.SHA.new(pub).digest())) # created correct NodeID self.rsaKey = rsaKey nodeID = Crypto.Hash.SHA.new(pub) # Solve the dynamic cryptographic puzzle. p, x = 0x1, None while not util.hasNZeroBitPrefix(p, c2): x = util.bin2int(util.generateRandomString(constants.ID_LENGTH)) # This is madness! p = util.hsh2int( Crypto.Hash.SHA.new( util.int2bin( (util.hsh2int(nodeID) ^ x)))) # Found a correct value of X and nodeID self.x = x return nodeID.digest()
def _verifyID(self, nodeID, x): '''Verifies if a user's ID has been generated using the ''' p1 = util.hsh2int(Crypto.Hash.SHA.new(nodeID)) p2 = util.hsh2int(Crypto.Hash.SHA.new( util.int2bin((util.bin2int(nodeID) ^ x)))) # check preceeding c_i bits in P1 and P2 using sharesXPrefices. return ( util.hasNZeroBitPrefix(p1, constants.CRYPTO_CHALLENGE_C1) and util.hasNZeroBitPrefix(p2, constants.CRYPTO_CHALLENGE_C2))