From 45f36035924b8a129a5ddcc1cbefd8da2610263a Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 18 Mar 2022 12:45:57 +0800 Subject: [PATCH] bugfix --- appPublic/across_nat.py | 2 +- appPublic/rc4.py | 13 +++++++++---- appPublic/rsa.py | 32 ++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/appPublic/across_nat.py b/appPublic/across_nat.py index 2de6cbf..8632274 100644 --- a/appPublic/across_nat.py +++ b/appPublic/across_nat.py @@ -81,7 +81,7 @@ class AcrossNat(object): def is_port_mapped(self, external_port, protocol='TCP'): protocol = protocol.upper() if self.upnp_supported: - return self.upnp_map_port_check(external_port, + return self.upnp_check_external_port(external_port, protocol=protocol) raise Exception('not implemented') diff --git a/appPublic/rc4.py b/appPublic/rc4.py index 41c1df7..6412e5e 100644 --- a/appPublic/rc4.py +++ b/appPublic/rc4.py @@ -62,11 +62,12 @@ class RC4: return r.decode(self.dcoding) class KeyChain(object): - def __init__(self, seed_str, crypter=None, keylen=23, period=600, threshold=60): + def __init__(self, seed_str, crypter=None, keylen=23, period=600, threshold=60, time_delta=0): self.seed_str = seed_str self.period = int(period) self.threshold = int(threshold) self.crypter = crypter + self.time_delta = time_delta if crypter is None: self.crypter = RC4() self.keylen = keylen @@ -75,8 +76,12 @@ class KeyChain(object): delta = datetime.timedelta(0) self.timezone = datetime.timezone(delta, name='gmt') + def get_timestamp(self): + ts = int(time.time()) - self.time_delta + return ts + def is_near_bottom(self, indicator=None): - ts = time.time() + ts = self.get_timestamp() i = indicator if i is None: i = self.get_indicator(ts) @@ -85,7 +90,7 @@ class KeyChain(object): return FalseTrue def is_near_top(self, indicator=None): - ts = time.time() + ts = self.get_timestamp() i = indicator if i is None: i = self.get_indicator(ts) @@ -95,7 +100,7 @@ class KeyChain(object): def get_indicator(self, ts=None): if ts is None: - ts = time.time() + ts = self.get_timestamp() return int(ts / self.period) * self.period def genKey(self, indicator): diff --git a/appPublic/rsa.py b/appPublic/rsa.py index 21c75da..fb5aa38 100644 --- a/appPublic/rsa.py +++ b/appPublic/rsa.py @@ -66,10 +66,10 @@ class RSA: text = f.read() return self.publickeyFromText(text) - def create_privatekey(self): + def create_privatekey(self, keylength=2048): return rsa.generate_private_key( public_exponent=65537, - key_size=2048, + key_size=keylength, backend=default_backend() ) @@ -154,19 +154,23 @@ if __name__ == '__main__': import os prikey1_file = os.path.join(os.path.dirname(__file__),'..','test', 'prikey1.rsa') r = RSA() - mpri = r.create_privatekey() + mpri = r.create_privatekey(4096) mpub = r.create_publickey(mpri) - zpri = r.create_privatekey() + zpri = r.create_privatekey(4096) zpub = r.create_publickey(zpri) - text = 'this is a test data, aaa' - cipher = r.encode(mpub,text) - ntext = r.decode(mpri,cipher) - print('encode text=', text, \ - 'decode result=', ntext, - 'cyber size=', len(cipher), - 'check if equal=', text==ntext) - signature = r.sign(zpri,text) - check = r.check_sign(zpub,text,signature) - print('sign and verify=',len(signature),check) + l = 100 + while True: + text = 'h' * l + cipher = r.encode(mpub,text) + ntext = r.decode(mpri,cipher) + print('textlen=', l, 'encode text=', text, \ + 'decode result=', ntext, + 'cyber size=', len(cipher), + 'check if equal=', text==ntext) + signature = r.sign(zpri,text) + check = r.check_sign(zpub,text,signature) + print('sign and verify=',len(signature),check) + l += 1 +