instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Core / ProxyService / ProxyServiceUtil.py
1 # Written George Milescu
2 # see LICENSE.txt for license information
3
4 # This class contains all util methods related to the ProxyService
5
6 import string
7 import random
8
9 def generate_proxy_challenge():
10     """ Generates a challenge (8 byte long random number) that a doe sends to a proxy during the handshake
11     
12     @return: an 8 byte log string
13     """
14     # Generate a random challenge - random number on 8 bytes (62**8 possible combinations)
15     chars = string.letters + string.digits #len(chars)=62
16     challenge = ''
17     for i in range(8):
18         challenge = challenge + random.choice(chars)
19     
20     return challenge
21
22
23 def decode_challenge_from_peerid(peerid):
24     """ Method used to retrieve (decode) a challenge from a peerid
25     
26     @param peerid: the peerid of the peer whose challenge is to be retrieved
27     
28     @return: a number, the challenge previously send to that peer, and encoded by the peer in its peerid
29     """
30
31     return peerid[12:20]
32
33
34 def encode_challenge_in_peerid(peerid, challenge):
35     """ Method used to insert (encode) a challenge into a peerid
36     
37     @param peerid: the regular peerid, into which the challenge will be encoded
38     @param challenge: an 8 byte long number, to be encoded in the peerid
39     
40     @return: a new peerid, with the challenge encoded in it
41     """
42
43     # proxy_peer_id = | regular_peer_id[1:12] | challenge[1:8] |
44     proxy_peer_id = peerid[:12] + challenge # len(self.challenge) = 8
45     
46     return proxy_peer_id