This commit is contained in:
yumoqing 2019-08-12 11:55:18 +08:00
parent 60476e2c1f
commit 87ad573a80
2 changed files with 16 additions and 22 deletions

View File

@ -24,7 +24,6 @@ class MyTemplateEngine:
'type':type, 'type':type,
'isNone':isNone, 'isNone':isNone,
'len':len, 'len':len,
'recordFind':recordFind,
'render':self.render, 'render':self.render,
'renders':self.renders, 'renders':self.renders,
'ArgsConvert':ArgsConvert, 'ArgsConvert':ArgsConvert,
@ -36,13 +35,12 @@ class MyTemplateEngine:
} }
self.env.globals.update(denv) self.env.globals.update(denv)
def setGlobal(self,dic): def set(self,k,v):
self.env.globals.update(dic) self.env.globals.update({k:v})
def _render(self,template,data): def _render(self,template,data):
self._setEnv()
uRet = template.render(**data) uRet = template.render(**data)
return uRet.encode(self.out_coding) return uRet
def renders(self,tmplstring,data): def renders(self,tmplstring,data):
def getGlobal(): def getGlobal():
@ -59,7 +57,6 @@ class MyTemplateEngine:
return self._render(template,data) return self._render(template,data)
def renderJsonFile(self,tmplfile,jsonfile): def renderJsonFile(self,tmplfile,jsonfile):
f = codecs.open(jsonfile,"r",self.file_coding) with codecs.open(jsonfile,"r",self.file_coding) as f:
data = json.load(f) data = json.load(f)
f.close()
return self.render(tmplfile,data) return self.render(tmplfile,data)

View File

@ -3,10 +3,9 @@ import random, base64
from hashlib import sha1 from hashlib import sha1
class RC4: class RC4:
def __init__(self,key='1234567890',data_coding='utf8'): def __init__(self,data_coding='utf8'):
self.bcoding = 'iso-8859-1' self.bcoding = 'iso-8859-1'
self.dcoding = data_coding self.dcoding = data_coding
self.key = key.encode(self.bcoding)
self.salt = b'AFUqx9WZuI32lnHk' self.salt = b'AFUqx9WZuI32lnHk'
def _crypt(self,data,key): def _crypt(self,data,key):
@ -28,13 +27,10 @@ class RC4:
return ''.join(out).encode(self.bcoding) return ''.join(out).encode(self.bcoding)
def encode(self,data, encode=base64.b64encode, salt_length=16): def encode(self,data, key,encode=base64.b64encode, salt_length=16):
"""RC4 encryption with random salt and final encoding""" """RC4 encryption with random salt and final encoding"""
#salt = '' akey = key.encode(self.bcoding)
#for n in range(salt_length): a = sha1(akey + self.salt)
# salt += chr(random.randrange(256))
#salt = salt.encode(self.bcoding)
a = sha1(self.key + self.salt)
k = a.digest() k = a.digest()
data = self.salt + self._crypt(data, k) data = self.salt + self._crypt(data, k)
@ -42,12 +38,13 @@ class RC4:
data = encode(data) data = encode(data)
return data.decode(self.dcoding) return data.decode(self.dcoding)
def decode(self,data, decode=base64.b64decode, salt_length=16): def decode(self,data, key,decode=base64.b64decode, salt_length=16):
"""RC4 decryption of encoded data""" """RC4 decryption of encoded data"""
akey = key.encode(self.bcoding)
if decode: if decode:
data = decode(data) data = decode(data)
salt = data[:salt_length] salt = data[:salt_length]
a = sha1(self.key + self.salt) a = sha1(akey + self.salt)
k = a.digest() #.decode('iso-8859-1') k = a.digest() #.decode('iso-8859-1')
r = self._crypt(data[salt_length:], k) r = self._crypt(data[salt_length:], k)
return r.decode(self.dcoding) return r.decode(self.dcoding)
@ -58,11 +55,11 @@ if __name__=='__main__':
# 密钥 # 密钥
data="231r3 feregrenerjk gkht324g8924gnfw k;ejkvwkjerv" data="231r3 feregrenerjk gkht324g8924gnfw k;ejkvwkjerv"
key = '123456' key = '123456'
rc4 = RC4(key) rc4 = RC4()
print(data) print(data)
# 加码 # 加码
encoded_data = rc4.encode(data) encoded_data = rc4.encode(data,key)
print(encoded_data,len(encoded_data) ) print(encoded_data,len(encoded_data) )
# 解码 # 解码
decoded_data = rc4.decode(encoded_data) decoded_data = rc4.decode(encoded_data,key)
print(decoded_data) print(decoded_data)