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 __init__(self, nodeID, method, methodArgs, rsaKey, cryptoChallengeX, rpcID=None, signedValue = None): if rpcID == None: rpcID = util.generateRandomString(160) Message.__init__(self, rpcID, nodeID, rsaKey, cryptoChallengeX, signedValue) self.request = method self.args = methodArgs
def post(self, content): """ Post some resource to the network. Ask the network to store some (encrypted) resource. Note: This should be encrypted with a symmetric key which will be private until shared through the above share() method. """ newSequenceNumber = self._getSequenceNumber() postKey = util.generateRandomString(constants.SYMMETRIC_KEY_LENGTH) #nonce = util.generateRandomString(constants.NONCE_LENGTH) # Use sequence number as nonce - that way we dont need to include it nonce = util.int2bin(newSequenceNumber, nbytes = constants.NONCE_LENGTH) encryptedContent = self._encryptPost(postKey, nonce, content) postName = ('%s:post:%s' % (self.node.id, newSequenceNumber)) postID = self.node.getNameID(postName) # We need to remember post keys used so we can issue sharing keys later self.sharingKeys[postID] = postKey postDefer = self.node.publishData(postName, encryptedContent) # update our latest sequence number latestName = ('%s:latest' % (self.node.id)) latestDefer = self.node.publishData(latestName, newSequenceNumber) # store post key by sharing the post with ourself postDefer.addCallback(lambda result: self.share(postID, self.node.id))
def __init__(self, propensitySize): self.userId = util.generateRandomString() self.propensitySize = propensitySize self.propensityList = [] self.generatePropensityRandomly()