def sendDataTest(): pub, priv = generateRSAKeyPair() temperature = readSensorTemperature() t = ((time.time() * 1000) * 1000) timeStr = "{:.0f}".format(t) data = timeStr + temperature signedData = criptoFunctions.signInfo(priv, data) ver = criptoFunctions.signVerify(data, signedData, pub) print("done: " + str(ver))
def addTransaction(self, devPublicKey, encryptedObj): global gwPvt global gwPub t1 = time.time() blk = chainFunctions.findBlock(devPublicKey) if (blk != False and blk.index > 0): devAESKey = findAESKey(devPublicKey) if (devAESKey != False): # plainObject contains [Signature + Time + Data] plainObject = criptoFunctions.decryptAES( encryptedObj, devAESKey) signature = plainObject[:-20] # remove the last 20 chars devTime = plainObject[-20: -4] # remove the 16 char of timestamp deviceData = plainObject[ -4:] # retrieve the las 4 chars which are the data d = devTime + deviceData isSigned = criptoFunctions.signVerify(d, signature, devPublicKey) if isSigned: deviceInfo = DeviceInfo.DeviceInfo(signature, devTime, deviceData) nextInt = blk.transactions[len(blk.transactions) - 1].index + 1 signData = criptoFunctions.signInfo(gwPvt, str(deviceInfo)) gwTime = "{:.0f}".format(((time.time() * 1000) * 1000)) # code responsible to create the hash between Info nodes. prevInfoHash = criptoFunctions.calculateTransactionHash( chainFunctions.getLatestBlockTransaction(blk)) transaction = Transaction.Transaction( nextInt, prevInfoHash, gwTime, deviceInfo, signData) # send to consensus #if not consensus(newBlockLedger, gwPub, devPublicKey): # return "Not Approved" chainFunctions.addBlockTransaction(blk, transaction) logger.debug( "block added locally... now sending to peers..") t2 = time.time() logger.debug( "=====2=====>time to add transaction in a block: " + '{0:.12f}'.format((t2 - t1) * 1000)) sendTransactionToPeers( devPublicKey, transaction ) # --->> this function should be run in a different thread. #print("all done") return "ok!" else: return "Invalid Signature" return "key not found"
def sendDataTest(): """ Send fake data to test the system """ pub, priv = generateRSAKeyPair() temperature = readSensorTemperature() t = ((time.time() * 1000) * 1000) timeStr = "{:.0f}".format(t) data = timeStr + temperature signedData = criptoFunctions.signInfo(priv, data) ver = criptoFunctions.signVerify(data, signedData, pub) logger.debug("Sending data teste: " + str(ver)) print("done: " + str(ver))
def isValidBlock(self, data, gatewayPublicKey, devicePublicKey, peer): newBlock = pickle.loads(data) blockIoT = chainFunctions.findBlock(devicePublicKey) consensus = True if blockIoT == False: print("Block not found in IoT ledger") consensus = False lastBlock = blockIoT.blockLedger[len(blockIoT.blockLedger) - 1] if newBlock.index != lastBlock.index + 1: print("New blovk Index not valid") consensus = False if lastBlock.calculateHashForBlockLedger( lastBlock) != newBlock.previousHash: print("New block previous hash not valid") consensus = False now = "{:.0f}".format(((time.time() * 1000) * 1000)) # check time if not (newBlock.timestamp > newBlock.signature.timestamp and newBlock.timestamp < now): print("New block time not valid") consensus = False # check device time if not (newBlock.signature.timestamp > lastBlock.signature.timestamp and newBlock.signature.timestamp < now): print("New block device time not valid") consensus = False # check device signature with device public key if not (criptoFunctions.signVerify(newBlock.signature.data, newBlock.signature.deviceSignature, gatewayPublicKey)): print("New block device signature not valid") consensus = False peer = getPeer(peer) obj = peer.object obj.receiveBlockConsensus(data, gatewayPublicKey, devicePublicKey, consensus)
def isTransactionValid(transaction, pubKey): data = str(transaction.data)[-22:-2] signature = str(transaction.data)[:-22] res = criptoFunctions.signVerify(data, signature, pubKey) return res