This commit is contained in:
yumoqing 2022-03-18 12:45:57 +08:00
parent a451a976e1
commit 45f3603592
3 changed files with 28 additions and 19 deletions

View File

@ -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')

View File

@ -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):

View File

@ -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