Some preparations for python 3

This commit is contained in:
mirrorwish 2016-03-23 23:26:57 +01:00 committed by Peter Surda
parent be16be2bb4
commit a7ec4c0555
10 changed files with 127 additions and 117 deletions

View File

@ -1,6 +1,7 @@
import hashlib import hashlib
from struct import * from struct import *
from pyelliptic import arithmetic from pyelliptic import arithmetic
from binascii import hexlify, unhexlify
@ -10,9 +11,9 @@ def convertIntToString(n):
if a[-1:] == 'L': if a[-1:] == 'L':
a = a[:-1] a = a[:-1]
if (len(a) % 2) == 0: if (len(a) % 2) == 0:
return a[2:].decode('hex') return unhexlify(a[2:])
else: else:
return ('0'+a[2:]).decode('hex') return unhexlify('0'+a[2:])
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
@ -142,7 +143,7 @@ def encodeAddress(version,stream,ripe):
sha.update(currentHash) sha.update(currentHash)
checksum = sha.digest()[0:4] 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) return 'BM-'+ encodeBase58(asInt)
def decodeAddress(address): def decodeAddress(address):
@ -165,7 +166,7 @@ def decodeAddress(address):
#print 'hexdata', hexdata #print 'hexdata', hexdata
data = hexdata.decode('hex') data = unhexlify(hexdata)
checksum = data[-4:] checksum = data[-4:]
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
@ -268,7 +269,7 @@ if __name__ == "__main__":
ripe.update(sha.digest()) ripe.update(sha.digest())
addressVersionNumber = 2 addressVersionNumber = 2
streamNumber = 1 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()) returnedAddress = encodeAddress(addressVersionNumber,streamNumber,ripe.digest())
print 'Encoded address:', returnedAddress print 'Encoded address:', returnedAddress
status,addressVersionNumber,streamNumber,data = decodeAddress(returnedAddress) status,addressVersionNumber,streamNumber,data = decodeAddress(returnedAddress)
@ -277,5 +278,5 @@ if __name__ == "__main__":
print 'addressVersionNumber', addressVersionNumber print 'addressVersionNumber', addressVersionNumber
print 'streamNumber', streamNumber print 'streamNumber', streamNumber
print 'length of data(the ripe hash):', len(data) print 'length of data(the ripe hash):', len(data)
print 'ripe data:', data.encode('hex') print 'ripe data:', hexlify(data)

View File

@ -14,6 +14,7 @@ if __name__ == "__main__":
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
import json import json
from binascii import hexlify
import shared import shared
import time import time
@ -477,7 +478,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
message = shared.fixPotentiallyInvalidUTF8Data(message) message = shared.fixPotentiallyInvalidUTF8Data(message)
if len(data) > 25: if len(data) > 25:
data += ',' 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=(',', ': ')) 'base64'), 'message': message.encode('base64'), 'encodingType': encodingtype, 'receivedTime': received, 'read': read}, indent=4, separators=(',', ': '))
data += ']}' data += ']}'
return data return data
@ -490,7 +491,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
msgid = row[0] msgid = row[0]
if len(data) > 25: if len(data) > 25:
data += ',' data += ','
data += json.dumps({'msgid': msgid.encode('hex')}, indent=4, separators=(',', ': ')) data += json.dumps({'msgid': hexlify(msgid)}, indent=4, separators=(',', ': '))
data += ']}' data += ']}'
return data return data
@ -515,7 +516,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
subject = shared.fixPotentiallyInvalidUTF8Data(subject) subject = shared.fixPotentiallyInvalidUTF8Data(subject)
message = shared.fixPotentiallyInvalidUTF8Data(message) 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 += ']}' data += ']}'
return data return data
@ -528,7 +529,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
message = shared.fixPotentiallyInvalidUTF8Data(message) message = shared.fixPotentiallyInvalidUTF8Data(message)
if len(data) > 25: if len(data) > 25:
data += ',' 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 += ']}' data += ']}'
return data return data
@ -539,7 +540,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
msgid = row[0] msgid = row[0]
if len(data) > 25: if len(data) > 25:
data += ',' data += ','
data += json.dumps({'msgid':msgid.encode('hex')}, indent=4, separators=(',', ': ')) data += json.dumps({'msgid':hexlify(msgid)}, indent=4, separators=(',', ': '))
data += ']}' data += ']}'
return data return data
@ -555,7 +556,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
message = shared.fixPotentiallyInvalidUTF8Data(message) message = shared.fixPotentiallyInvalidUTF8Data(message)
if len(data) > 25: if len(data) > 25:
data += ',' 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 += ']}' data += ']}'
return data return data
@ -569,7 +570,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
subject = shared.fixPotentiallyInvalidUTF8Data(subject) subject = shared.fixPotentiallyInvalidUTF8Data(subject)
message = shared.fixPotentiallyInvalidUTF8Data(message) 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 += ']}' data += ']}'
return data return data
@ -586,7 +587,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
message = shared.fixPotentiallyInvalidUTF8Data(message) message = shared.fixPotentiallyInvalidUTF8Data(message)
if len(data) > 25: if len(data) > 25:
data += ',' 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 += ']}' data += ']}'
return data return data
@ -601,7 +602,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
subject = shared.fixPotentiallyInvalidUTF8Data(subject) subject = shared.fixPotentiallyInvalidUTF8Data(subject)
message = shared.fixPotentiallyInvalidUTF8Data(message) 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 += ']}' data += ']}'
return data return data
@ -694,7 +695,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
shared.workerQueue.put(('sendmessage', toAddress)) shared.workerQueue.put(('sendmessage', toAddress))
return ackdata.encode('hex') return hexlify(ackdata)
def HandleSendBroadcast(self, params): def HandleSendBroadcast(self, params):
if len(params) == 0: if len(params) == 0:
@ -751,7 +752,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
toAddress, toLabel, fromAddress, subject, message, ackdata))) toAddress, toLabel, fromAddress, subject, message, ackdata)))
shared.workerQueue.put(('sendbroadcast', '')) shared.workerQueue.put(('sendbroadcast', ''))
return ackdata.encode('hex') return hexlify(ackdata)
def HandleGetStatus(self, params): def HandleGetStatus(self, params):
if len(params) != 1: if len(params) != 1:
@ -851,7 +852,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
shared.inventory[inventoryHash] = ( shared.inventory[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, int(time.time()) + TTL,'') objectType, toStreamNumber, encryptedPayload, int(time.time()) + TTL,'')
with shared.printLock: 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(( shared.broadcastToSendDataQueues((
toStreamNumber, 'advertiseobject', inventoryHash)) toStreamNumber, 'advertiseobject', inventoryHash))
@ -898,7 +899,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
shared.inventory[inventoryHash] = ( shared.inventory[inventoryHash] = (
objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL,'') objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL,'')
with shared.printLock: 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(( shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash)) streamNumber, 'advertiseobject', inventoryHash))
@ -933,7 +934,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
payload, = row payload, = row
if len(data) > 25: if len(data) > 25:
data += ',' data += ','
data += json.dumps({'data':payload.encode('hex')}, indent=4, separators=(',', ': ')) data += json.dumps({'data':hexlify(payload)}, indent=4, separators=(',', ': '))
data += ']}' data += ']}'
return data return data

View File

@ -11,6 +11,7 @@ from debug import logger
from helper_threading import * from helper_threading import *
from pyelliptic import arithmetic from pyelliptic import arithmetic
import tr import tr
from binascii import hexlify
class addressGenerator(threading.Thread, StoppableThread): class addressGenerator(threading.Thread, StoppableThread):
@ -104,7 +105,7 @@ class addressGenerator(threading.Thread, StoppableThread):
ripe.update(sha.digest()) ripe.update(sha.digest())
if ripe.digest()[:numberOfNullBytesDemandedOnFrontOfRipeHash] == '\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash: if ripe.digest()[:numberOfNullBytesDemandedOnFrontOfRipeHash] == '\x00' * numberOfNullBytesDemandedOnFrontOfRipeHash:
break 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: 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))) 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: except ZeroDivisionError:
@ -196,7 +197,7 @@ class addressGenerator(threading.Thread, StoppableThread):
break 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: 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))) 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: except ZeroDivisionError:
@ -262,7 +263,7 @@ class addressGenerator(threading.Thread, StoppableThread):
listOfNewAddressesToSendOutThroughTheAPI.append( listOfNewAddressesToSendOutThroughTheAPI.append(
address) address)
shared.myECCryptorObjects[ripe.digest()] = highlevelcrypto.makeCryptor( shared.myECCryptorObjects[ripe.digest()] = highlevelcrypto.makeCryptor(
potentialPrivEncryptionKey.encode('hex')) hexlify(potentialPrivEncryptionKey))
shared.myAddressesByHash[ripe.digest()] = address shared.myAddressesByHash[ripe.digest()] = address
tag = hashlib.sha512(hashlib.sha512(encodeVarint( tag = hashlib.sha512(hashlib.sha512(encodeVarint(
addressVersionNumber) + encodeVarint(streamNumber) + ripe.digest()).digest()).digest()[32:] addressVersionNumber) + encodeVarint(streamNumber) + ripe.digest()).digest()).digest()[32:]

View File

@ -8,6 +8,7 @@ import sys
import string import string
from subprocess import call # used when the API must execute an outside program from subprocess import call # used when the API must execute an outside program
import traceback import traceback
from binascii import hexlify
from pyelliptic.openssl import OpenSSL from pyelliptic.openssl import OpenSSL
import highlevelcrypto import highlevelcrypto
@ -106,7 +107,7 @@ class objectProcessor(threading.Thread):
if len(requestedHash) != 20: if len(requestedHash) != 20:
logger.debug('The length of the requested hash is not 20 bytes. Something is wrong. Ignoring.') logger.debug('The length of the requested hash is not 20 bytes. Something is wrong. Ignoring.')
return 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 if requestedHash in shared.myAddressesByHash: # if this address hash is one of mine
myAddress = shared.myAddressesByHash[requestedHash] myAddress = shared.myAddressesByHash[requestedHash]
elif requestedAddressVersionNumber >= 4: elif requestedAddressVersionNumber >= 4:
@ -114,7 +115,7 @@ class objectProcessor(threading.Thread):
if len(requestedTag) != 32: if len(requestedTag) != 32:
logger.debug('The length of the requested tag is not 32 bytes. Something is wrong. Ignoring.') logger.debug('The length of the requested tag is not 32 bytes. Something is wrong. Ignoring.')
return 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: if requestedTag in shared.myAddressesByTag:
myAddress = shared.myAddressesByTag[requestedTag] myAddress = shared.myAddressesByTag[requestedTag]
@ -199,9 +200,9 @@ class objectProcessor(threading.Thread):
publicSigningKey in hex: %s\n\ publicSigningKey in hex: %s\n\
publicEncryptionKey in hex: %s' % (addressVersion, publicEncryptionKey in hex: %s' % (addressVersion,
streamNumber, streamNumber,
ripe.encode('hex'), hexlify(ripe),
publicSigningKey.encode('hex'), hexlify(publicSigningKey),
publicEncryptionKey.encode('hex') hexlify(publicEncryptionKey)
) )
) )
@ -240,7 +241,7 @@ class objectProcessor(threading.Thread):
data[readPosition:readPosition + 10]) data[readPosition:readPosition + 10])
readPosition += signatureLengthLength readPosition += signatureLengthLength
signature = data[readPosition:readPosition + signatureLength] 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)') logger.debug('ECDSA verify passed (within processpubkey)')
else: else:
logger.warning('ECDSA verify failed (within processpubkey)') logger.warning('ECDSA verify failed (within processpubkey)')
@ -258,9 +259,9 @@ class objectProcessor(threading.Thread):
publicSigningKey in hex: %s\n\ publicSigningKey in hex: %s\n\
publicEncryptionKey in hex: %s' % (addressVersion, publicEncryptionKey in hex: %s' % (addressVersion,
streamNumber, streamNumber,
ripe.encode('hex'), hexlify(ripe),
publicSigningKey.encode('hex'), hexlify(publicSigningKey),
publicEncryptionKey.encode('hex') hexlify(publicEncryptionKey)
) )
) )
@ -341,7 +342,7 @@ class objectProcessor(threading.Thread):
decryptedData = cryptorObject.decrypt(data[readPosition:]) 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. 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 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: except Exception as err:
pass pass
if not initialDecryptionSuccessful: 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\ 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\ See: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html \n\
your toRipe: %s\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 return
readPosition += 20 readPosition += 20
@ -419,7 +420,7 @@ class objectProcessor(threading.Thread):
readPosition:readPosition + signatureLength] readPosition:readPosition + signatureLength]
signedData = data[8:20] + encodeVarint(1) + encodeVarint(streamNumberAsClaimedByMsg) + decryptedData[:positionOfBottomOfAckData] 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') logger.debug('ECDSA verify failed')
return return
logger.debug('ECDSA verify passed') logger.debug('ECDSA verify passed')
@ -623,7 +624,7 @@ class objectProcessor(threading.Thread):
decryptedData = cryptorObject.decrypt(data[readPosition:]) 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. 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 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: except Exception as err:
pass pass
# print 'cryptorObject.decrypt Exception:', err # print 'cryptorObject.decrypt Exception:', err
@ -718,7 +719,7 @@ class objectProcessor(threading.Thread):
signature = decryptedData[ signature = decryptedData[
readPosition:readPosition + signatureLength] readPosition:readPosition + signatureLength]
signedData += decryptedData[:readPositionAtBottomOfMessage] 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') logger.debug('ECDSA verify failed')
return return
logger.debug('ECDSA verify passed') logger.debug('ECDSA verify passed')

View File

@ -15,6 +15,7 @@ import ssl
from struct import unpack, pack from struct import unpack, pack
import sys import sys
import traceback import traceback
from binascii import hexlify
#import string #import string
#from subprocess import call # used when the API must execute an outside program #from subprocess import call # used when the API must execute an outside program
#from pyelliptic.openssl import OpenSSL #from pyelliptic.openssl import OpenSSL
@ -374,7 +375,7 @@ class receiveDataThread(threading.Thread):
fatalHumanFriendly = 'Fatal' fatalHumanFriendly = 'Fatal'
message = '%s message received from %s: %s.' % (fatalHumanFriendly, self.peer, errorText) message = '%s message received from %s: %s.' % (fatalHumanFriendly, self.peer, errorText)
if inventoryVector: if inventoryVector:
message += " This concerns object %s" % inventoryVector.encode('hex') message += " This concerns object %s" % hexlify(inventoryVector)
if banTime > 0: if banTime > 0:
message += " Remote node says that the ban time is %s" % banTime message += " Remote node says that the ban time is %s" % banTime
logger.error(message) 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 # Send a getdata message to our peer to request the object with the given
# hash # hash
def sendgetdata(self, 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 payload = '\x01' + hash
self.sendDataThreadQueue.put((0, 'sendRawData', shared.CreatePacket('getdata', payload))) self.sendDataThreadQueue.put((0, 'sendRawData', shared.CreatePacket('getdata', payload)))
@ -463,7 +464,7 @@ class receiveDataThread(threading.Thread):
for i in xrange(numberOfRequestedInventoryItems): for i in xrange(numberOfRequestedInventoryItems):
hash = data[lengthOfVarint + ( hash = data[lengthOfVarint + (
i * 32):32 + lengthOfVarint + (i * 32)] 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.numberOfInventoryLookupsPerformed += 1
shared.inventoryLock.acquire() shared.inventoryLock.acquire()

View File

@ -18,6 +18,7 @@ from helper_generic import addDataPadding
from helper_threading import * from helper_threading import *
import l10n import l10n
from protocol import * from protocol import *
from binascii import hexlify, unhexlify
# This thread, of which there is only one, does the heavy lifting: # This thread, of which there is only one, does the heavy lifting:
# calculating POWs. # calculating POWs.
@ -58,14 +59,14 @@ class singleWorker(threading.Thread, StoppableThread):
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest()
privEncryptionKey = doubleHashOfAddressData[:32] # Note that this is the first half of the sha512 hash. privEncryptionKey = doubleHashOfAddressData[:32] # Note that this is the first half of the sha512 hash.
tag = doubleHashOfAddressData[32:] 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 # Initialize the shared.ackdataForWhichImWatching data structure
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''SELECT ackdata FROM sent where (status='msgsent' OR status='doingmsgpow')''') '''SELECT ackdata FROM sent where (status='msgsent' OR status='doingmsgpow')''')
for row in queryreturn: for row in queryreturn:
ackdata, = row ackdata, = row
logger.info('Watching for ackdata ' + ackdata.encode('hex')) logger.info('Watching for ackdata ' + hexlify(ackdata))
shared.ackdataForWhichImWatching[ackdata] = 0 shared.ackdataForWhichImWatching[ackdata] = 0
self.stop.wait( 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) logger.error('Error within doPOWForMyV2Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err)
return return
privSigningKeyHex = shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
privSigningKeyBase58).encode('hex') privSigningKeyBase58))
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub( pubSigningKey = unhexlify(highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex') privSigningKeyHex))
pubEncryptionKey = highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex') privEncryptionKeyHex))
payload += pubSigningKey[1:] payload += pubSigningKey[1:]
payload += pubEncryptionKey[1:] payload += pubEncryptionKey[1:]
@ -159,7 +160,7 @@ class singleWorker(threading.Thread, StoppableThread):
shared.inventory[inventoryHash] = ( shared.inventory[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'') objectType, streamNumber, payload, embeddedTime,'')
logger.info('broadcasting inv with hash: ' + inventoryHash.encode('hex')) logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
shared.broadcastToSendDataQueues(( shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash)) streamNumber, 'advertiseobject', inventoryHash))
@ -215,14 +216,14 @@ class singleWorker(threading.Thread, StoppableThread):
return return
privSigningKeyHex = shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
privSigningKeyBase58).encode('hex') privSigningKeyBase58))
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub( pubSigningKey = unhexlify(highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex') privSigningKeyHex))
pubEncryptionKey = highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex') privEncryptionKeyHex))
payload += pubSigningKey[1:] payload += pubSigningKey[1:]
payload += pubEncryptionKey[1:] payload += pubEncryptionKey[1:]
@ -249,7 +250,7 @@ class singleWorker(threading.Thread, StoppableThread):
shared.inventory[inventoryHash] = ( shared.inventory[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'') objectType, streamNumber, payload, embeddedTime,'')
logger.info('broadcasting inv with hash: ' + inventoryHash.encode('hex')) logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
shared.broadcastToSendDataQueues(( shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash)) 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) logger.error('Error within sendOutOrStoreMyV4Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err)
return return
privSigningKeyHex = shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
privSigningKeyBase58).encode('hex') privSigningKeyBase58))
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub( pubSigningKey = unhexlify(highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex') privSigningKeyHex))
pubEncryptionKey = highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex') privEncryptionKeyHex))
dataToEncrypt += pubSigningKey[1:] dataToEncrypt += pubSigningKey[1:]
dataToEncrypt += pubEncryptionKey[1:] dataToEncrypt += pubEncryptionKey[1:]
@ -324,7 +325,7 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKey = doubleHashOfAddressData[:32] privEncryptionKey = doubleHashOfAddressData[:32]
pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey) pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey)
payload += highlevelcrypto.encrypt( payload += highlevelcrypto.encrypt(
dataToEncrypt, pubEncryptionKey.encode('hex')) dataToEncrypt, hexlify(pubEncryptionKey))
# Do the POW for this pubkey message # 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)))) 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] = ( shared.inventory[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, doubleHashOfAddressData[32:]) 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(( shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash)) 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.")))) ackdata, tr.translateText("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file."))))
continue continue
privSigningKeyHex = shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
privSigningKeyBase58).encode('hex') privSigningKeyBase58))
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode( 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. '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( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex') privEncryptionKeyHex))
if TTL > 28 * 24 * 60 * 60: if TTL > 28 * 24 * 60 * 60:
TTL = 28 * 24 * 60 * 60 TTL = 28 * 24 * 60 * 60
@ -437,7 +438,7 @@ class singleWorker(threading.Thread, StoppableThread):
pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey) pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey)
payload += highlevelcrypto.encrypt( 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)))) 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...') logger.info('(For broadcast message) Doing proof of work...')
@ -460,7 +461,7 @@ class singleWorker(threading.Thread, StoppableThread):
objectType = 3 objectType = 3
shared.inventory[inventoryHash] = ( shared.inventory[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, tag) 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(( shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash)) streamNumber, 'advertiseobject', inventoryHash))
@ -553,7 +554,7 @@ class singleWorker(threading.Thread, StoppableThread):
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest()
privEncryptionKey = doubleHashOfToAddressData[:32] # The first half of the sha512 hash. privEncryptionKey = doubleHashOfToAddressData[:32] # The first half of the sha512 hash.
tag = doubleHashOfToAddressData[32:] # The second 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): 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. 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())))) 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) logger.error('Error within sendMsg. Could not read the keys from the keys.dat file for our own address. %s\n' % err)
continue continue
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubEncryptionKeyBase256 = highlevelcrypto.privToPub( pubEncryptionKeyBase256 = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex')[1:] privEncryptionKeyHex))[1:]
requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte
requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes
shared.UISignalQueue.put(('updateSentItemStatusByAckdata', ( 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.")))) ackdata, tr.translateText("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file."))))
continue continue
privSigningKeyHex = shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
privSigningKeyBase58).encode('hex') privSigningKeyBase58))
privEncryptionKeyHex = shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58).encode('hex') privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub( pubSigningKey = unhexlify(highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex') privSigningKeyHex))
pubEncryptionKey = highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex).decode('hex') privEncryptionKeyHex))
payload += pubSigningKey[ 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. 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. # We have assembled the data that will be encrypted.
try: try:
encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) encrypted = highlevelcrypto.encrypt(payload,"04"+hexlify(pubEncryptionKeyBase256))
except: except:
sqlExecute('''UPDATE sent SET status='badkey' WHERE ackdata=?''', ackdata) 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())))) 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: else:
# not sending to a chan or one of my addresses # 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())))) 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(( shared.broadcastToSendDataQueues((
toStreamNumber, 'advertiseobject', inventoryHash)) 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. 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. 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: 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: if retryNumber == 0:
TTL = 2.5*24*60*60 # 2.5 days. This was chosen fairly arbitrarily. 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) payload += encodeVarint(streamNumber)
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
payload += ripe 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: else:
payload += tag 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 # print 'trial value', trialValue
statusbar = 'Doing the computations necessary to request the recipient\'s public key.' statusbar = 'Doing the computations necessary to request the recipient\'s public key.'

View File

@ -1,5 +1,6 @@
import socket import socket
import sys import sys
from binascii import hexlify, unhexlify
import shared import shared
@ -8,13 +9,13 @@ def convertIntToString(n):
if a[-1:] == 'L': if a[-1:] == 'L':
a = a[:-1] a = a[:-1]
if (len(a) % 2) == 0: if (len(a) % 2) == 0:
return a[2:].decode('hex') return unhexlify(a[2:])
else: else:
return ('0' + a[2:]).decode('hex') return unhexlify('0' + a[2:])
def convertStringToInt(s): def convertStringToInt(s):
return int(s.encode('hex'), 16) return int(hexlify(s), 16)
def signal_handler(signal, frame): def signal_handler(signal, frame):

View File

@ -1,3 +1,4 @@
from binascii import hexlify
import pyelliptic import pyelliptic
from pyelliptic import arithmetic as a, OpenSSL from pyelliptic import arithmetic as a, OpenSSL
def makeCryptor(privkey): def makeCryptor(privkey):
@ -18,7 +19,7 @@ def makePubCryptor(pubkey):
def privToPub(privkey): def privToPub(privkey):
private_key = a.changebase(privkey, 16, 256, minlen=32) private_key = a.changebase(privkey, 16, 256, minlen=32)
public_key = pointMult(private_key) public_key = pointMult(private_key)
return public_key.encode('hex') return hexlify(public_key)
# Encrypts message with hex public key # Encrypts message with hex public key
def encrypt(msg,hexPubkey): def encrypt(msg,hexPubkey):
return pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey)) return pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey))

View File

@ -7,6 +7,7 @@ from time import strftime, localtime
import sys import sys
import shared import shared
import string import string
from binascii import hexlify
appdata = shared.lookupAppdataFolder() appdata = shared.lookupAppdataFolder()
@ -31,7 +32,7 @@ def readSent():
output = cur.fetchall() output = cur.fetchall()
for row in output: for row in output:
msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, sleeptill, status, retrynumber, folder, encodingtype, ttl = row 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(): def readSubscriptions():
print 'Printing everything in subscriptions table:' print 'Printing everything in subscriptions table:'
@ -50,7 +51,7 @@ def readPubkeys():
output = cur.fetchall() output = cur.fetchall()
for row in output: for row in output:
address, transmitdata, time, usedpersonally = row 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(): def readInventory():
print 'Printing everything in inventory table:' print 'Printing everything in inventory table:'
@ -60,7 +61,7 @@ def readInventory():
output = cur.fetchall() output = cur.fetchall()
for row in output: for row in output:
hash, objecttype, streamnumber, payload, expirestime = row 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(): def takeInboxMessagesOutOfTrash():

View File

@ -26,6 +26,7 @@ import datetime
from os import path, environ from os import path, environ
from struct import Struct from struct import Struct
import traceback import traceback
from binascii import hexlify
# Project imports. # Project imports.
from addresses import * from addresses import *
@ -417,8 +418,8 @@ def reloadMyAddressHashes():
if addressVersionNumber == 2 or addressVersionNumber == 3 or addressVersionNumber == 4: if addressVersionNumber == 2 or addressVersionNumber == 3 or addressVersionNumber == 4:
# Returns a simple 32 bytes of information encoded in 64 Hex characters, # Returns a simple 32 bytes of information encoded in 64 Hex characters,
# or null if there was an error. # or null if there was an error.
privEncryptionKey = decodeWalletImportFormat( privEncryptionKey = hexlify(decodeWalletImportFormat(
config.get(addressInKeysFile, 'privencryptionkey')).encode('hex') config.get(addressInKeysFile, 'privencryptionkey')))
if len(privEncryptionKey) == 64:#It is 32 bytes encoded as 64 hex characters if len(privEncryptionKey) == 64:#It is 32 bytes encoded as 64 hex characters
myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey)
@ -447,13 +448,13 @@ def reloadBroadcastSendersForWhichImWatching():
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32]
MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))
else: else:
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint(
addressVersionNumber) + encodeVarint(streamNumber) + hash).digest()).digest() addressVersionNumber) + encodeVarint(streamNumber) + hash).digest()).digest()
tag = doubleHashOfAddressData[32:] tag = doubleHashOfAddressData[32:]
privEncryptionKey = doubleHashOfAddressData[:32] privEncryptionKey = doubleHashOfAddressData[:32]
MyECSubscriptionCryptorObjects[tag] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) MyECSubscriptionCryptorObjects[tag] = highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))
def isProofOfWorkSufficient(data, def isProofOfWorkSufficient(data,
nonceTrialsPerByte=0, nonceTrialsPerByte=0,
@ -662,7 +663,7 @@ def decryptAndCheckPubkeyPayload(data, address):
readPosition += signatureLengthLength readPosition += signatureLengthLength
signature = decryptedData[readPosition:readPosition + signatureLength] 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)') logger.info('ECDSA verify passed (within decryptAndCheckPubkeyPayload)')
else: else:
logger.info('ECDSA verify failed (within decryptAndCheckPubkeyPayload)') logger.info('ECDSA verify failed (within decryptAndCheckPubkeyPayload)')
@ -688,9 +689,9 @@ def decryptAndCheckPubkeyPayload(data, address):
publicSigningKey in hex: %s\n\ publicSigningKey in hex: %s\n\
publicEncryptionKey in hex: %s' % (addressVersion, publicEncryptionKey in hex: %s' % (addressVersion,
streamNumber, streamNumber,
ripe.encode('hex'), hexlify(ripe),
publicSigningKey.encode('hex'), hexlify(publicSigningKey),
publicEncryptionKey.encode('hex') hexlify(publicEncryptionKey)
) )
) )
@ -775,7 +776,7 @@ def _checkAndShareUndefinedObjectWithPeers(data):
inventory[inventoryHash] = ( inventory[inventoryHash] = (
objectType, streamNumber, data, embeddedTime,'') objectType, streamNumber, data, embeddedTime,'')
inventoryLock.release() 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)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash))
@ -803,7 +804,7 @@ def _checkAndShareMsgWithPeers(data):
inventory[inventoryHash] = ( inventory[inventoryHash] = (
objectType, streamNumber, data, embeddedTime,'') objectType, streamNumber, data, embeddedTime,'')
inventoryLock.release() 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)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash))
# Now let's enqueue it to be processed ourselves. # Now let's enqueue it to be processed ourselves.
@ -840,7 +841,7 @@ def _checkAndShareGetpubkeyWithPeers(data):
objectType, streamNumber, data, embeddedTime,'') objectType, streamNumber, data, embeddedTime,'')
inventoryLock.release() inventoryLock.release()
# This getpubkey request is valid. Forward to peers. # 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)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash))
# Now let's queue it to be processed ourselves. # Now let's queue it to be processed ourselves.
@ -862,7 +863,7 @@ def _checkAndSharePubkeyWithPeers(data):
return return
if addressVersion >= 4: if addressVersion >= 4:
tag = data[readPosition:readPosition + 32] 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: else:
tag = '' tag = ''
@ -878,7 +879,7 @@ def _checkAndSharePubkeyWithPeers(data):
objectType, streamNumber, data, embeddedTime, tag) objectType, streamNumber, data, embeddedTime, tag)
inventoryLock.release() inventoryLock.release()
# This object is valid. Forward it to peers. # 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)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash))
@ -918,7 +919,7 @@ def _checkAndShareBroadcastWithPeers(data):
objectType, streamNumber, data, embeddedTime, tag) objectType, streamNumber, data, embeddedTime, tag)
inventoryLock.release() inventoryLock.release()
# This object is valid. Forward it to peers. # 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)) broadcastToSendDataQueues((streamNumber, 'advertiseobject', inventoryHash))
# Now let's queue it to be processed ourselves. # Now let's queue it to be processed ourselves.