helper_ackPayload formating

This commit is contained in:
Mahendra 2018-04-06 16:21:29 +05:30
parent c5462a00cc
commit a70b063004
No known key found for this signature in database
GPG Key ID: A672D8FAAEE398B3

View File

@ -1,31 +1,32 @@
import hashlib
import highlevelcrypto import highlevelcrypto
import random
import helper_random import helper_random
from binascii import hexlify, unhexlify from binascii import hexlify
from struct import pack, unpack from struct import pack
from addresses import encodeVarint from addresses import encodeVarint
# This function generates payload objects for message acknowledgements # This function generates payload objects for message acknowledgements
# Several stealth levels are available depending on the privacy needs; # Several stealth levels are available depending on the privacy needs;
# a higher level means better stealth, but also higher cost (size+POW) # a higher level means better stealth, but also higher cost (size+POW)
# - level 0: a random 32-byte sequence with a message header appended # - level 0: a random 32-byte sequence with a message header appended
# - level 1: a getpubkey request for a (random) dummy key hash # - level 1: a getpubkey request for a (random) dummy key hash
# - level 2: a standard message, encrypted to a random pubkey # - level 2: a standard message, encrypted to a random pubkey
def genAckPayload(streamNumber=1, stealthLevel=0): def genAckPayload(streamNumber=1, stealthLevel=0):
if (stealthLevel==2): # Generate privacy-enhanced payload if (stealthLevel == 2): # Generate privacy-enhanced payload
# Generate a dummy privkey and derive the pubkey # Generate a dummy privkey and derive the pubkey
dummyPubKeyHex = highlevelcrypto.privToPub(hexlify(helper_random.randomBytes(32))) dummyPubKeyHex = highlevelcrypto.privToPub(
hexlify(helper_random.randomBytes(32)))
# Generate a dummy message of random length # Generate a dummy message of random length
# (the smallest possible standard-formatted message is 234 bytes) # (the smallest possible standard-formatted message is 234 bytes)
dummyMessage = helper_random.randomBytes(random.randint(234, 800)) dummyMessage = helper_random.randomBytes(
helper_random.randomrandrange(234, 801))
# Encrypt the message using standard BM encryption (ECIES) # Encrypt the message using standard BM encryption (ECIES)
ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex) ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex)
acktype = 2 # message acktype = 2 # message
version = 1 version = 1
elif (stealthLevel==1): # Basic privacy payload (random getpubkey) elif (stealthLevel == 1): # Basic privacy payload (random getpubkey)
ackdata = helper_random.randomBytes(32) ackdata = helper_random.randomBytes(32)
acktype = 0 # getpubkey acktype = 0 # getpubkey
version = 4 version = 4
@ -35,6 +36,7 @@ def genAckPayload(streamNumber=1, stealthLevel=0):
acktype = 2 # message acktype = 2 # message
version = 1 version = 1
ackobject = pack('>I', acktype) + encodeVarint(version) + encodeVarint(streamNumber) + ackdata ackobject = pack('>I', acktype) + encodeVarint(
version) + encodeVarint(streamNumber) + ackdata
return ackobject return ackobject