From a7ec4c0555523020e3ff4e142cb5eaddb2bf9556 Mon Sep 17 00:00:00 2001 From: mirrorwish Date: Wed, 23 Mar 2016 23:26:57 +0100 Subject: [PATCH] Some preparations for python 3 --- src/addresses.py | 13 ++-- src/api.py | 29 +++++---- src/class_addressGenerator.py | 7 +- src/class_objectProcessor.py | 29 +++++---- src/class_receiveDataThread.py | 7 +- src/class_singleWorker.py | 113 +++++++++++++++++---------------- src/helper_generic.py | 7 +- src/highlevelcrypto.py | 3 +- src/message_data_reader.py | 7 +- src/shared.py | 29 +++++---- 10 files changed, 127 insertions(+), 117 deletions(-) diff --git a/src/addresses.py b/src/addresses.py index 017eaa82..fa87677a 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -1,6 +1,7 @@ import hashlib from struct import * from pyelliptic import arithmetic +from binascii import hexlify, unhexlify @@ -10,9 +11,9 @@ def convertIntToString(n): if a[-1:] == 'L': a = a[:-1] if (len(a) % 2) == 0: - return a[2:].decode('hex') + return unhexlify(a[2:]) else: - return ('0'+a[2:]).decode('hex') + return unhexlify('0'+a[2:]) ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" @@ -142,7 +143,7 @@ def encodeAddress(version,stream,ripe): sha.update(currentHash) checksum = sha.digest()[0:4] - asInt = int(storedBinaryData.encode('hex') + checksum.encode('hex'),16) + asInt = int(hexlify(storedBinaryData) + hexlify(checksum),16) return 'BM-'+ encodeBase58(asInt) def decodeAddress(address): @@ -165,7 +166,7 @@ def decodeAddress(address): #print 'hexdata', hexdata - data = hexdata.decode('hex') + data = unhexlify(hexdata) checksum = data[-4:] sha = hashlib.new('sha512') @@ -268,7 +269,7 @@ if __name__ == "__main__": ripe.update(sha.digest()) addressVersionNumber = 2 streamNumber = 1 - print 'Ripe digest that we will encode in the address:', ripe.digest().encode('hex') + print 'Ripe digest that we will encode in the address:', hexlify(ripe.digest()) returnedAddress = encodeAddress(addressVersionNumber,streamNumber,ripe.digest()) print 'Encoded address:', returnedAddress status,addressVersionNumber,streamNumber,data = decodeAddress(returnedAddress) @@ -277,5 +278,5 @@ if __name__ == "__main__": print 'addressVersionNumber', addressVersionNumber print 'streamNumber', streamNumber print 'length of data(the ripe hash):', len(data) - print 'ripe data:', data.encode('hex') + print 'ripe data:', hexlify(data) diff --git a/src/api.py b/src/api.py index edae0dc5..37e80421 100644 --- a/src/api.py +++ b/src/api.py @@ -14,6 +14,7 @@ if __name__ == "__main__": from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer import json +from binascii import hexlify import shared import time @@ -477,7 +478,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): message = shared.fixPotentiallyInvalidUTF8Data(message) if len(data) > 25: data += ',' - data += json.dumps({'msgid': msgid.encode('hex'), 'toAddress': toAddress, 'fromAddress': fromAddress, 'subject': subject.encode( + data += json.dumps({'msgid': hexlify(msgid), 'toAddress': toAddress, 'fromAddress': fromAddress, 'subject': subject.encode( 'base64'), 'message': message.encode('base64'), 'encodingType': encodingtype, 'receivedTime': received, 'read': read}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -490,7 +491,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): msgid = row[0] if len(data) > 25: data += ',' - data += json.dumps({'msgid': msgid.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid': hexlify(msgid)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -515,7 +516,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row subject = shared.fixPotentiallyInvalidUTF8Data(subject) message = shared.fixPotentiallyInvalidUTF8Data(message) - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received, 'read': read}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received, 'read': read}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -528,7 +529,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): message = shared.fixPotentiallyInvalidUTF8Data(message) if len(data) > 25: data += ',' - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':hexlify(ackdata)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -539,7 +540,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): msgid = row[0] if len(data) > 25: data += ',' - data += json.dumps({'msgid':msgid.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -555,7 +556,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): message = shared.fixPotentiallyInvalidUTF8Data(message) if len(data) > 25: data += ',' - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -569,7 +570,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row subject = shared.fixPotentiallyInvalidUTF8Data(subject) message = shared.fixPotentiallyInvalidUTF8Data(message) - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':hexlify(ackdata)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -586,7 +587,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): message = shared.fixPotentiallyInvalidUTF8Data(message) if len(data) > 25: data += ',' - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':hexlify(ackdata)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -601,7 +602,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row subject = shared.fixPotentiallyInvalidUTF8Data(subject) message = shared.fixPotentiallyInvalidUTF8Data(message) - data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'msgid':hexlify(msgid), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':hexlify(ackdata)}, indent=4, separators=(',', ': ')) data += ']}' return data @@ -694,7 +695,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): shared.workerQueue.put(('sendmessage', toAddress)) - return ackdata.encode('hex') + return hexlify(ackdata) def HandleSendBroadcast(self, params): if len(params) == 0: @@ -751,7 +752,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): toAddress, toLabel, fromAddress, subject, message, ackdata))) shared.workerQueue.put(('sendbroadcast', '')) - return ackdata.encode('hex') + return hexlify(ackdata) def HandleGetStatus(self, params): if len(params) != 1: @@ -851,7 +852,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): shared.inventory[inventoryHash] = ( objectType, toStreamNumber, encryptedPayload, int(time.time()) + TTL,'') with shared.printLock: - print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', inventoryHash.encode('hex') + print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', hexlify(inventoryHash) shared.broadcastToSendDataQueues(( toStreamNumber, 'advertiseobject', inventoryHash)) @@ -898,7 +899,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): shared.inventory[inventoryHash] = ( objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL,'') with shared.printLock: - print 'broadcasting inv within API command disseminatePubkey with hash:', inventoryHash.encode('hex') + print 'broadcasting inv within API command disseminatePubkey with hash:', hexlify(inventoryHash) shared.broadcastToSendDataQueues(( streamNumber, 'advertiseobject', inventoryHash)) @@ -933,7 +934,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): payload, = row if len(data) > 25: data += ',' - data += json.dumps({'data':payload.encode('hex')}, indent=4, separators=(',', ': ')) + data += json.dumps({'data':hexlify(payload)}, indent=4, separators=(',', ': ')) data += ']}' return data diff --git a/src/class_addressGenerator.py b/src/class_addressGenerator.py index b84be8e3..28f329ea 100644 --- a/src/class_addressGenerator.py +++ b/src/class_addressGenerator.py @@ -11,6 +11,7 @@ from debug import logger from helper_threading import * from pyelliptic import arithmetic import tr +from binascii import hexlify class addressGenerator(threading.Thread, StoppableThread): @@ -104,7 +105,7 @@ class addressGenerator(threading.Thread, StoppableThread): ripe.update(sha.digest()) if ripe.digest()[:numberOfNullBytesDemandedOnFrontOfRipeHash] == '\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash: break - logger.info('Generated address with ripe digest: %s' % ripe.digest().encode('hex')) + logger.info('Generated address with ripe digest: %s' % hexlify(ripe.digest())) try: logger.info('Address generator calculated %s addresses at %s addresses per second before finding one with the correct ripe-prefix.' % (numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix / (time.time() - startTime))) except ZeroDivisionError: @@ -196,7 +197,7 @@ class addressGenerator(threading.Thread, StoppableThread): break - logger.info('Generated address with ripe digest: %s' % ripe.digest().encode('hex')) + logger.info('Generated address with ripe digest: %s' % hexlify(ripe.digest())) try: logger.info('Address generator calculated %s addresses at %s addresses per second before finding one with the correct ripe-prefix.' % (numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix / (time.time() - startTime))) except ZeroDivisionError: @@ -262,7 +263,7 @@ class addressGenerator(threading.Thread, StoppableThread): listOfNewAddressesToSendOutThroughTheAPI.append( address) shared.myECCryptorObjects[ripe.digest()] = highlevelcrypto.makeCryptor( - potentialPrivEncryptionKey.encode('hex')) + hexlify(potentialPrivEncryptionKey)) shared.myAddressesByHash[ripe.digest()] = address tag = hashlib.sha512(hashlib.sha512(encodeVarint( addressVersionNumber) + encodeVarint(streamNumber) + ripe.digest()).digest()).digest()[32:] diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index a2402643..6885db8c 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -8,6 +8,7 @@ import sys import string from subprocess import call # used when the API must execute an outside program import traceback +from binascii import hexlify from pyelliptic.openssl import OpenSSL import highlevelcrypto @@ -106,7 +107,7 @@ class objectProcessor(threading.Thread): if len(requestedHash) != 20: logger.debug('The length of the requested hash is not 20 bytes. Something is wrong. Ignoring.') return - logger.info('the hash requested in this getpubkey request is: %s' % requestedHash.encode('hex')) + logger.info('the hash requested in this getpubkey request is: %s' % hexlify(requestedHash)) if requestedHash in shared.myAddressesByHash: # if this address hash is one of mine myAddress = shared.myAddressesByHash[requestedHash] elif requestedAddressVersionNumber >= 4: @@ -114,7 +115,7 @@ class objectProcessor(threading.Thread): if len(requestedTag) != 32: logger.debug('The length of the requested tag is not 32 bytes. Something is wrong. Ignoring.') return - logger.debug('the tag requested in this getpubkey request is: %s' % requestedTag.encode('hex')) + logger.debug('the tag requested in this getpubkey request is: %s' % hexlify(requestedTag)) if requestedTag in shared.myAddressesByTag: myAddress = shared.myAddressesByTag[requestedTag] @@ -199,9 +200,9 @@ class objectProcessor(threading.Thread): publicSigningKey in hex: %s\n\ publicEncryptionKey in hex: %s' % (addressVersion, streamNumber, - ripe.encode('hex'), - publicSigningKey.encode('hex'), - publicEncryptionKey.encode('hex') + hexlify(ripe), + hexlify(publicSigningKey), + hexlify(publicEncryptionKey) ) ) @@ -240,7 +241,7 @@ class objectProcessor(threading.Thread): data[readPosition:readPosition + 10]) readPosition += signatureLengthLength signature = data[readPosition:readPosition + signatureLength] - if highlevelcrypto.verify(data[8:endOfSignedDataPosition], signature, publicSigningKey.encode('hex')): + if highlevelcrypto.verify(data[8:endOfSignedDataPosition], signature, hexlify(publicSigningKey)): logger.debug('ECDSA verify passed (within processpubkey)') else: logger.warning('ECDSA verify failed (within processpubkey)') @@ -258,9 +259,9 @@ class objectProcessor(threading.Thread): publicSigningKey in hex: %s\n\ publicEncryptionKey in hex: %s' % (addressVersion, streamNumber, - ripe.encode('hex'), - publicSigningKey.encode('hex'), - publicEncryptionKey.encode('hex') + hexlify(ripe), + hexlify(publicSigningKey), + hexlify(publicEncryptionKey) ) ) @@ -341,7 +342,7 @@ class objectProcessor(threading.Thread): decryptedData = cryptorObject.decrypt(data[readPosition:]) toRipe = key # This is the RIPE hash of my pubkeys. We need this below to compare to the destination_ripe included in the encrypted data. initialDecryptionSuccessful = True - logger.info('EC decryption successful using key associated with ripe hash: %s.' % key.encode('hex')) + logger.info('EC decryption successful using key associated with ripe hash: %s.' % hexlify(key)) except Exception as err: pass if not initialDecryptionSuccessful: @@ -393,7 +394,7 @@ class objectProcessor(threading.Thread): logger.info('The original sender of this message did not send it to you. Someone is attempting a Surreptitious Forwarding Attack.\n\ See: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html \n\ your toRipe: %s\n\ - embedded destination toRipe: %s' % (toRipe.encode('hex'), decryptedData[readPosition:readPosition + 20].encode('hex')) + embedded destination toRipe: %s' % (hexlify(toRipe), hexlify(decryptedData[readPosition:readPosition + 20])) ) return readPosition += 20 @@ -419,7 +420,7 @@ class objectProcessor(threading.Thread): readPosition:readPosition + signatureLength] signedData = data[8:20] + encodeVarint(1) + encodeVarint(streamNumberAsClaimedByMsg) + decryptedData[:positionOfBottomOfAckData] - if not highlevelcrypto.verify(signedData, signature, pubSigningKey.encode('hex')): + if not highlevelcrypto.verify(signedData, signature, hexlify(pubSigningKey)): logger.debug('ECDSA verify failed') return logger.debug('ECDSA verify passed') @@ -623,7 +624,7 @@ class objectProcessor(threading.Thread): decryptedData = cryptorObject.decrypt(data[readPosition:]) toRipe = key # This is the RIPE hash of the sender's pubkey. We need this below to compare to the RIPE hash of the sender's address to verify that it was encrypted by with their key rather than some other key. initialDecryptionSuccessful = True - logger.info('EC decryption successful using key associated with ripe hash: %s' % key.encode('hex')) + logger.info('EC decryption successful using key associated with ripe hash: %s' % hexlify(key)) except Exception as err: pass # print 'cryptorObject.decrypt Exception:', err @@ -718,7 +719,7 @@ class objectProcessor(threading.Thread): signature = decryptedData[ readPosition:readPosition + signatureLength] signedData += decryptedData[:readPositionAtBottomOfMessage] - if not highlevelcrypto.verify(signedData, signature, sendersPubSigningKey.encode('hex')): + if not highlevelcrypto.verify(signedData, signature, hexlify(sendersPubSigningKey)): logger.debug('ECDSA verify failed') return logger.debug('ECDSA verify passed') diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index d63b19eb..5883ab42 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -15,6 +15,7 @@ import ssl from struct import unpack, pack import sys import traceback +from binascii import hexlify #import string #from subprocess import call # used when the API must execute an outside program #from pyelliptic.openssl import OpenSSL @@ -374,7 +375,7 @@ class receiveDataThread(threading.Thread): fatalHumanFriendly = 'Fatal' message = '%s message received from %s: %s.' % (fatalHumanFriendly, self.peer, errorText) if inventoryVector: - message += " This concerns object %s" % inventoryVector.encode('hex') + message += " This concerns object %s" % hexlify(inventoryVector) if banTime > 0: message += " Remote node says that the ban time is %s" % banTime logger.error(message) @@ -447,7 +448,7 @@ class receiveDataThread(threading.Thread): # Send a getdata message to our peer to request the object with the given # hash def sendgetdata(self, hash): - logger.debug('sending getdata to retrieve object with hash: ' + hash.encode('hex')) + logger.debug('sending getdata to retrieve object with hash: ' + hexlify(hash)) payload = '\x01' + hash self.sendDataThreadQueue.put((0, 'sendRawData', shared.CreatePacket('getdata', payload))) @@ -463,7 +464,7 @@ class receiveDataThread(threading.Thread): for i in xrange(numberOfRequestedInventoryItems): hash = data[lengthOfVarint + ( i * 32):32 + lengthOfVarint + (i * 32)] - logger.debug('received getdata request for item:' + hash.encode('hex')) + logger.debug('received getdata request for item:' + hexlify(hash)) shared.numberOfInventoryLookupsPerformed += 1 shared.inventoryLock.acquire() diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 73127adf..169b40c0 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -18,6 +18,7 @@ from helper_generic import addDataPadding from helper_threading import * import l10n from protocol import * +from binascii import hexlify, unhexlify # This thread, of which there is only one, does the heavy lifting: # calculating POWs. @@ -58,14 +59,14 @@ class singleWorker(threading.Thread, StoppableThread): toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() privEncryptionKey = doubleHashOfAddressData[:32] # Note that this is the first half of the sha512 hash. tag = doubleHashOfAddressData[32:] - shared.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex'))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. + shared.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. # Initialize the shared.ackdataForWhichImWatching data structure queryreturn = sqlQuery( '''SELECT ackdata FROM sent where (status='msgsent' OR status='doingmsgpow')''') for row in queryreturn: ackdata, = row - logger.info('Watching for ackdata ' + ackdata.encode('hex')) + logger.info('Watching for ackdata ' + hexlify(ackdata)) shared.ackdataForWhichImWatching[ackdata] = 0 self.stop.wait( @@ -134,14 +135,14 @@ class singleWorker(threading.Thread, StoppableThread): logger.error('Error within doPOWForMyV2Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) return - privSigningKeyHex = shared.decodeWalletImportFormat( - privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') - pubSigningKey = highlevelcrypto.privToPub( - privSigningKeyHex).decode('hex') - pubEncryptionKey = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex') + privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( + privSigningKeyBase58)) + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) + pubSigningKey = unhexlify(highlevelcrypto.privToPub( + privSigningKeyHex)) + pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex)) payload += pubSigningKey[1:] payload += pubEncryptionKey[1:] @@ -159,7 +160,7 @@ class singleWorker(threading.Thread, StoppableThread): shared.inventory[inventoryHash] = ( objectType, streamNumber, payload, embeddedTime,'') - logger.info('broadcasting inv with hash: ' + inventoryHash.encode('hex')) + logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) shared.broadcastToSendDataQueues(( streamNumber, 'advertiseobject', inventoryHash)) @@ -215,14 +216,14 @@ class singleWorker(threading.Thread, StoppableThread): return - privSigningKeyHex = shared.decodeWalletImportFormat( - privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') - pubSigningKey = highlevelcrypto.privToPub( - privSigningKeyHex).decode('hex') - pubEncryptionKey = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex') + privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( + privSigningKeyBase58)) + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) + pubSigningKey = unhexlify(highlevelcrypto.privToPub( + privSigningKeyHex)) + pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex)) payload += pubSigningKey[1:] payload += pubEncryptionKey[1:] @@ -249,7 +250,7 @@ class singleWorker(threading.Thread, StoppableThread): shared.inventory[inventoryHash] = ( objectType, streamNumber, payload, embeddedTime,'') - logger.info('broadcasting inv with hash: ' + inventoryHash.encode('hex')) + logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) shared.broadcastToSendDataQueues(( streamNumber, 'advertiseobject', inventoryHash)) @@ -292,14 +293,14 @@ class singleWorker(threading.Thread, StoppableThread): logger.error('Error within sendOutOrStoreMyV4Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) return - privSigningKeyHex = shared.decodeWalletImportFormat( - privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') - pubSigningKey = highlevelcrypto.privToPub( - privSigningKeyHex).decode('hex') - pubEncryptionKey = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex') + privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( + privSigningKeyBase58)) + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) + pubSigningKey = unhexlify(highlevelcrypto.privToPub( + privSigningKeyHex)) + pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex)) dataToEncrypt += pubSigningKey[1:] dataToEncrypt += pubEncryptionKey[1:] @@ -324,7 +325,7 @@ class singleWorker(threading.Thread, StoppableThread): privEncryptionKey = doubleHashOfAddressData[:32] pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey) payload += highlevelcrypto.encrypt( - dataToEncrypt, pubEncryptionKey.encode('hex')) + dataToEncrypt, hexlify(pubEncryptionKey)) # Do the POW for this pubkey message target = 2 ** 64 / (shared.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + shared.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+shared.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) @@ -339,7 +340,7 @@ class singleWorker(threading.Thread, StoppableThread): shared.inventory[inventoryHash] = ( objectType, streamNumber, payload, embeddedTime, doubleHashOfAddressData[32:]) - logger.info('broadcasting inv with hash: ' + inventoryHash.encode('hex')) + logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) shared.broadcastToSendDataQueues(( streamNumber, 'advertiseobject', inventoryHash)) @@ -373,15 +374,15 @@ class singleWorker(threading.Thread, StoppableThread): ackdata, tr.translateText("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file.")))) continue - privSigningKeyHex = shared.decodeWalletImportFormat( - privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( + privSigningKeyBase58)) + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode( 'hex') # At this time these pubkeys are 65 bytes long because they include the encoding byte which we won't be sending in the broadcast message. - pubEncryptionKey = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex') + pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex)) if TTL > 28 * 24 * 60 * 60: TTL = 28 * 24 * 60 * 60 @@ -437,7 +438,7 @@ class singleWorker(threading.Thread, StoppableThread): pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey) payload += highlevelcrypto.encrypt( - dataToEncrypt, pubEncryptionKey.encode('hex')) + dataToEncrypt, hexlify(pubEncryptionKey)) target = 2 ** 64 / (shared.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + shared.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+shared.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) logger.info('(For broadcast message) Doing proof of work...') @@ -460,7 +461,7 @@ class singleWorker(threading.Thread, StoppableThread): objectType = 3 shared.inventory[inventoryHash] = ( objectType, streamNumber, payload, embeddedTime, tag) - logger.info('sending inv (within sendBroadcast function) for object: ' + inventoryHash.encode('hex')) + logger.info('sending inv (within sendBroadcast function) for object: ' + hexlify(inventoryHash)) shared.broadcastToSendDataQueues(( streamNumber, 'advertiseobject', inventoryHash)) @@ -553,7 +554,7 @@ class singleWorker(threading.Thread, StoppableThread): toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() privEncryptionKey = doubleHashOfToAddressData[:32] # The first half of the sha512 hash. tag = doubleHashOfToAddressData[32:] # The second half of the sha512 hash. - shared.neededPubkeys[tag] = (toaddress, highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex'))) + shared.neededPubkeys[tag] = (toaddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) for value in shared.inventory.by_type_and_tag(1, toTag): if shared.decryptAndCheckPubkeyPayload(value.payload, toaddress) == 'successful': #if valid, this function also puts it in the pubkeys table. @@ -675,10 +676,10 @@ class singleWorker(threading.Thread, StoppableThread): shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,tr.translateText("MainWindow",'Problem: You are trying to send a message to yourself or a chan but your encryption key could not be found in the keys.dat file. Could not encrypt message. %1').arg(l10n.formatTimestamp())))) logger.error('Error within sendMsg. Could not read the keys from the keys.dat file for our own address. %s\n' % err) continue - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') - pubEncryptionKeyBase256 = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex')[1:] + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) + pubEncryptionKeyBase256 = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex))[1:] requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes shared.UISignalQueue.put(('updateSentItemStatusByAckdata', ( @@ -701,15 +702,15 @@ class singleWorker(threading.Thread, StoppableThread): ackdata, tr.translateText("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file.")))) continue - privSigningKeyHex = shared.decodeWalletImportFormat( - privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = shared.decodeWalletImportFormat( - privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( + privSigningKeyBase58)) + privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( + privEncryptionKeyBase58)) - pubSigningKey = highlevelcrypto.privToPub( - privSigningKeyHex).decode('hex') - pubEncryptionKey = highlevelcrypto.privToPub( - privEncryptionKeyHex).decode('hex') + pubSigningKey = unhexlify(highlevelcrypto.privToPub( + privSigningKeyHex)) + pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( + privEncryptionKeyHex)) payload += pubSigningKey[ 1:] # The \x04 on the beginning of the public keys are not sent. This way there is only one acceptable way to encode and send a public key. @@ -755,7 +756,7 @@ class singleWorker(threading.Thread, StoppableThread): # We have assembled the data that will be encrypted. try: - encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) + encrypted = highlevelcrypto.encrypt(payload,"04"+hexlify(pubEncryptionKeyBase256)) except: sqlExecute('''UPDATE sent SET status='badkey' WHERE ackdata=?''', ackdata) shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,tr.translateText("MainWindow",'Problem: The recipient\'s encryption key is no good. Could not encrypt message. %1').arg(l10n.formatTimestamp())))) @@ -795,7 +796,7 @@ class singleWorker(threading.Thread, StoppableThread): else: # not sending to a chan or one of my addresses shared.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr.translateText("MainWindow", "Message sent. Waiting for acknowledgement. Sent on %1").arg(l10n.formatTimestamp())))) - logger.info('Broadcasting inv for my msg(within sendmsg function):' + inventoryHash.encode('hex')) + logger.info('Broadcasting inv for my msg(within sendmsg function):' + hexlify(inventoryHash)) shared.broadcastToSendDataQueues(( toStreamNumber, 'advertiseobject', inventoryHash)) @@ -864,7 +865,7 @@ class singleWorker(threading.Thread, StoppableThread): privEncryptionKey = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[:32] # Note that this is the first half of the sha512 hash. tag = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[32:] # Note that this is the second half of the sha512 hash. if tag not in shared.neededPubkeys: - shared.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex'))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. + shared.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. if retryNumber == 0: TTL = 2.5*24*60*60 # 2.5 days. This was chosen fairly arbitrarily. @@ -878,10 +879,10 @@ class singleWorker(threading.Thread, StoppableThread): payload += encodeVarint(streamNumber) if addressVersionNumber <= 3: payload += ripe - logger.info('making request for pubkey with ripe: %s', ripe.encode('hex')) + logger.info('making request for pubkey with ripe: %s', hexlify(ripe)) else: payload += tag - logger.info('making request for v4 pubkey with tag: %s', tag.encode('hex')) + logger.info('making request for v4 pubkey with tag: %s', hexlify(tag)) # print 'trial value', trialValue statusbar = 'Doing the computations necessary to request the recipient\'s public key.' diff --git a/src/helper_generic.py b/src/helper_generic.py index a9de6e05..b8a38863 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -1,5 +1,6 @@ import socket import sys +from binascii import hexlify, unhexlify import shared @@ -8,13 +9,13 @@ def convertIntToString(n): if a[-1:] == 'L': a = a[:-1] if (len(a) % 2) == 0: - return a[2:].decode('hex') + return unhexlify(a[2:]) else: - return ('0' + a[2:]).decode('hex') + return unhexlify('0' + a[2:]) def convertStringToInt(s): - return int(s.encode('hex'), 16) + return int(hexlify(s), 16) def signal_handler(signal, frame): diff --git a/src/highlevelcrypto.py b/src/highlevelcrypto.py index 5a2ae920..c8b67e77 100644 --- a/src/highlevelcrypto.py +++ b/src/highlevelcrypto.py @@ -1,3 +1,4 @@ +from binascii import hexlify import pyelliptic from pyelliptic import arithmetic as a, OpenSSL def makeCryptor(privkey): @@ -18,7 +19,7 @@ def makePubCryptor(pubkey): def privToPub(privkey): private_key = a.changebase(privkey, 16, 256, minlen=32) public_key = pointMult(private_key) - return public_key.encode('hex') + return hexlify(public_key) # Encrypts message with hex public key def encrypt(msg,hexPubkey): return pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey)) diff --git a/src/message_data_reader.py b/src/message_data_reader.py index 250e06a9..a99be336 100644 --- a/src/message_data_reader.py +++ b/src/message_data_reader.py @@ -7,6 +7,7 @@ from time import strftime, localtime import sys import shared import string +from binascii import hexlify appdata = shared.lookupAppdataFolder() @@ -31,7 +32,7 @@ def readSent(): output = cur.fetchall() for row in output: msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, sleeptill, status, retrynumber, folder, encodingtype, ttl = row - print msgid.encode('hex'), toaddress, 'toripe:', toripe.encode('hex'), 'fromaddress:', fromaddress, 'ENCODING TYPE:', encodingtype, 'SUBJECT:', repr(subject), 'MESSAGE:', repr(message), 'ACKDATA:', ackdata.encode('hex'), lastactiontime, status, retrynumber, folder + print hexlify(msgid), toaddress, 'toripe:', hexlify(toripe), 'fromaddress:', fromaddress, 'ENCODING TYPE:', encodingtype, 'SUBJECT:', repr(subject), 'MESSAGE:', repr(message), 'ACKDATA:', hexlify(ackdata), lastactiontime, status, retrynumber, folder def readSubscriptions(): print 'Printing everything in subscriptions table:' @@ -50,7 +51,7 @@ def readPubkeys(): output = cur.fetchall() for row in output: address, transmitdata, time, usedpersonally = row - print 'Address:', address, '\tTime first broadcast:', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(time)),'utf-8'), '\tUsed by me personally:', usedpersonally, '\tFull pubkey message:', transmitdata.encode('hex') + print 'Address:', address, '\tTime first broadcast:', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(time)),'utf-8'), '\tUsed by me personally:', usedpersonally, '\tFull pubkey message:', hexlify(transmitdata) def readInventory(): print 'Printing everything in inventory table:' @@ -60,7 +61,7 @@ def readInventory(): output = cur.fetchall() for row in output: hash, objecttype, streamnumber, payload, expirestime = row - print 'Hash:', hash.encode('hex'), objecttype, streamnumber, '\t', payload.encode('hex'), '\t', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(expirestime)),'utf-8') + print 'Hash:', hexlify(hash), objecttype, streamnumber, '\t', hexlify(payload), '\t', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(expirestime)),'utf-8') def takeInboxMessagesOutOfTrash(): diff --git a/src/shared.py b/src/shared.py index 9c4c90b7..315e5671 100644 --- a/src/shared.py +++ b/src/shared.py @@ -26,6 +26,7 @@ import datetime from os import path, environ from struct import Struct import traceback +from binascii import hexlify # Project imports. from addresses import * @@ -417,8 +418,8 @@ def reloadMyAddressHashes(): if addressVersionNumber == 2 or addressVersionNumber == 3 or addressVersionNumber == 4: # Returns a simple 32 bytes of information encoded in 64 Hex characters, # or null if there was an error. - privEncryptionKey = decodeWalletImportFormat( - config.get(addressInKeysFile, 'privencryptionkey')).encode('hex') + privEncryptionKey = hexlify(decodeWalletImportFormat( + config.get(addressInKeysFile, 'privencryptionkey'))) if len(privEncryptionKey) == 64:#It is 32 bytes encoded as 64 hex characters myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) @@ -447,13 +448,13 @@ def reloadBroadcastSendersForWhichImWatching(): if addressVersionNumber <= 3: privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] - MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) + MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(hexlify(privEncryptionKey)) else: doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( addressVersionNumber) + encodeVarint(streamNumber) + hash).digest()).digest() tag = doubleHashOfAddressData[32:] privEncryptionKey = doubleHashOfAddressData[:32] - MyECSubscriptionCryptorObjects[tag] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) + MyECSubscriptionCryptorObjects[tag] = highlevelcrypto.makeCryptor(hexlify(privEncryptionKey)) def isProofOfWorkSufficient(data, nonceTrialsPerByte=0, @@ -662,7 +663,7 @@ def decryptAndCheckPubkeyPayload(data, address): readPosition += signatureLengthLength signature = decryptedData[readPosition:readPosition + signatureLength] - if highlevelcrypto.verify(signedData, signature, publicSigningKey.encode('hex')): + if highlevelcrypto.verify(signedData, signature, hexlify(publicSigningKey)): logger.info('ECDSA verify passed (within decryptAndCheckPubkeyPayload)') else: logger.info('ECDSA verify failed (within decryptAndCheckPubkeyPayload)') @@ -688,9 +689,9 @@ def decryptAndCheckPubkeyPayload(data, address): publicSigningKey in hex: %s\n\ publicEncryptionKey in hex: %s' % (addressVersion, streamNumber, - ripe.encode('hex'), - publicSigningKey.encode('hex'), - publicEncryptionKey.encode('hex') + hexlify(ripe), + hexlify(publicSigningKey), + hexlify(publicEncryptionKey) ) ) @@ -775,7 +776,7 @@ def _checkAndShareUndefinedObjectWithPeers(data): inventory[inventoryHash] = ( objectType, streamNumber, data, embeddedTime,'') inventoryLock.release() - logger.debug('advertising inv with hash: %s' % inventoryHash.encode('hex')) + logger.debug('advertising inv with hash: %s' % hexlify(inventoryHash)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash)) @@ -803,7 +804,7 @@ def _checkAndShareMsgWithPeers(data): inventory[inventoryHash] = ( objectType, streamNumber, data, embeddedTime,'') inventoryLock.release() - logger.debug('advertising inv with hash: %s' % inventoryHash.encode('hex')) + logger.debug('advertising inv with hash: %s' % hexlify(inventoryHash)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash)) # Now let's enqueue it to be processed ourselves. @@ -840,7 +841,7 @@ def _checkAndShareGetpubkeyWithPeers(data): objectType, streamNumber, data, embeddedTime,'') inventoryLock.release() # This getpubkey request is valid. Forward to peers. - logger.debug('advertising inv with hash: %s' % inventoryHash.encode('hex')) + logger.debug('advertising inv with hash: %s' % hexlify(inventoryHash)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash)) # Now let's queue it to be processed ourselves. @@ -862,7 +863,7 @@ def _checkAndSharePubkeyWithPeers(data): return if addressVersion >= 4: tag = data[readPosition:readPosition + 32] - logger.debug('tag in received pubkey is: %s' % tag.encode('hex')) + logger.debug('tag in received pubkey is: %s' % hexlify(tag)) else: tag = '' @@ -878,7 +879,7 @@ def _checkAndSharePubkeyWithPeers(data): objectType, streamNumber, data, embeddedTime, tag) inventoryLock.release() # This object is valid. Forward it to peers. - logger.debug('advertising inv with hash: %s' % inventoryHash.encode('hex')) + logger.debug('advertising inv with hash: %s' % hexlify(inventoryHash)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash)) @@ -918,7 +919,7 @@ def _checkAndShareBroadcastWithPeers(data): objectType, streamNumber, data, embeddedTime, tag) inventoryLock.release() # This object is valid. Forward it to peers. - logger.debug('advertising inv with hash: %s' % inventoryHash.encode('hex')) + logger.debug('advertising inv with hash: %s' % hexlify(inventoryHash)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash)) # Now let's queue it to be processed ourselves.