This commit is contained in:
yumoqing 2021-07-14 12:33:53 +08:00
parent df645fbc86
commit 2315f8706a
3 changed files with 19 additions and 19 deletions

View File

@ -6,7 +6,7 @@ from appPublic.rsa import RSA
from appPublic.rc4 import RC4
from appPublic.uniqueID import getID
import brotli
# import brotli
import zlib
import struct
@ -90,6 +90,7 @@ class DataEncoder:
def pack(self, peer_id, data):
pk = self.get_peer_pubkey(peer_id)
print(peer_id, 'pk=', pk)
t, d = self.identify_datatype(data)
d, k = self.encode_data(pk, d)
f = 'b%05ds%03ds' % (len(d), len(k))
@ -101,15 +102,11 @@ class DataEncoder:
pd += s
self.pack_d = [t,d,k,s]
origin_len = len(pd)
pd = brotli.compress(pd)
pdz = zlib.compress(pd)
print('brotli lenght=%d, zlib length=%d, origin length=%d' % \
(len(pd), len(pdz), origin_len)
)
pd = zlib.compress(pd)
return pd
def unpack(self, peer_id, data):
data = brotli.decompress(data)
data = zlib.decompress(data)
org_data = data
pk = self.get_peer_pubkey(peer_id)
f = data[:18]

View File

@ -1,13 +1,14 @@
import sys
from brotli import compress, decompress
import zlib
def do(fn):
with open(fn, 'rb') as f:
b = f.read()
c = compress(b)
d = decompress(c)
zc = zlib.compress(b)
print(fn,'\t',
float(len(c))/float(len(b)))
float(len(c))/float(len(b)), float(len(zc))/float(len(b)))
fns = sys.argv[1:]
for fn in fns:
do(fn)

View File

@ -2,18 +2,20 @@
from appPublic.rsa import RSA
from appPublic.dataencoder import DataEncoder
def get_prikey(id):
prifile='prikey2.rsa'
if id=='node1':
prifile = 'prikey1.rsa'
rsa = RSA()
prikey = rsa.read_privatekey(prifile)
pubkey = rsa.create_publickey(prikey)
return pubkey
Nodes = {
}
def get_pubkey(id):
node = Nodes.get(id)
return node.public_key
node1 = DataEncoder('node1', get_prikey, 'prikey1.rsa')
node2 = DataEncoder('node2', get_prikey, 'prikey2.rsa')
Nodes['node1'] = node1 = DataEncoder('node1', get_pubkey, 'prikey1.rsa')
Nodes['node2'] = node2 = DataEncoder('node2', get_pubkey, 'prikey2.rsa')
node1.set_peer_pubkey('node2', get_pubkey('node2'))
node2.set_peer_pubkey('node1', get_pubkey('node1'))
data1 = {
'a':'iy34ti3y42ti23t425g4',
'b':100,