def writefile(self,filename,sha1v): file = self.getfile(filename) path = self.dir+filename if os.path.isfile(path): input = FileUtil.open(path,"r") oldfile = input.read() input.close() else: oldfile = None output = FileUtil.open(path,"wb") output.write(file) print 'Update '+filename+' OK!' output.close() input = FileUtil.open(path,"rb") sha1vv = FileUtil.get_file_sha1(input) #print sha1v.strip() #print sha1vv.strip() input.close() if sha1v.strip()==sha1vv.strip() : print 'Verify '+filename+' OK!' else: print 'Verify '+filename+' Fail!' if oldfile: output = FileUtil.open(path,"wb") output.write(oldfile) output.close() print 'Recover '+filename+' OK!' if filename.strip() == '/autoupdate.ini'.strip(): newconfig = Config(__config__) newconfig.writeconfig('autoupdate', 'server',common.AUTOUPDATE_SERVER_STR) print 'ReWrite /autoupdate.ini OK!' common.reloadini() print 'ReLoad /autoupdate.ini OK!'
def writefile(self, filename, sha1v): file = self.getfile(filename) path = self.dir + filename if os.path.isfile(path): input = FileUtil.open(path, "r") oldfile = input.read() input.close() else: oldfile = None output = FileUtil.open(path, "wb") output.write(file) print "Update " + filename + " OK!" output.close() input = FileUtil.open(path, "rb") sha1vv = FileUtil.get_file_sha1(input) # print sha1v.strip() # print sha1vv.strip() input.close() if sha1v.strip() == sha1vv.strip(): print "Verify " + filename + " OK!" else: print "Verify " + filename + " Fail!" if oldfile: output = FileUtil.open(path, "wb") output.write(oldfile) output.close() print "Recover " + filename + " OK!" if filename.strip() == "/autoupdate.ini".strip(): newconfig = Config(__config__) newconfig.writeconfig("autoupdate", "server", common.AUTOUPDATE_SERVER_STR) print "ReWrite /autoupdate.ini OK!" common.reloadini() print "ReLoad /autoupdate.ini OK!"
def writefile(self, filename, sha1v): file = self.getfile(filename) path = self.dir + filename if os.path.isfile(path): input = FileUtil.open(path, "r") oldfile = input.read() input.close() else: oldfile = None output = FileUtil.open(path, "wb") output.write(file) print 'Update ' + filename + ' OK!' output.close() input = FileUtil.open(path, "rb") sha1vv = FileUtil.get_file_sha1(input) #print sha1v.strip() #print sha1vv.strip() input.close() if sha1v.strip() == sha1vv.strip(): print 'Verify ' + filename + ' OK!' else: print 'Verify ' + filename + ' Fail!' if oldfile: output = FileUtil.open(path, "wb") output.write(oldfile) output.close() print 'Recover ' + filename + ' OK!' if filename.strip() == '/autoupdate.ini'.strip(): newconfig = Config(__config__) newconfig.writeconfig('autoupdate', 'server', common.AUTOUPDATE_SERVER_STR) print 'ReWrite /autoupdate.ini OK!' common.reloadini() print 'ReLoad /autoupdate.ini OK!'
def version(): input = FileUtil.open(common.CONFIG_GIT,"r") gits = input.read().replace('\r\n','\n').split('\n') input.close() message = "==============================" message += "\r\n" message += "Name:"+common.CONFIG_NAMES message += "\r\n" message += "Author:"+common.CONFIG_AUTHOR message += "\r\n" message += "Version:"+common.CONFIG_VERSION message += "\r\n" message += "Now Git Version:" message += str(len(gits)) message += "\r\n" message += "Last Git Commit:" message += gits[len(gits)-2] message += "\r\n" message += "Update Time:" message += time.strftime("%Y-%m-%d %X",time.localtime()) message += "\r\n" message += "==============================" message += "\r\n" out = FileUtil.open(common.CONFIG_VERSIONFILE,"w") out.write(message) out.close return message
def do(message,filename): FileUtil.if_has_file_remove(filename) output = FileUtil.open(filename,"w") output.write(sign(message)) output.close() input = FileUtil.open(filename,"r") ok = verify(message,input.read()) input.close() return ok
def getnewsha1(self,path,oldsha1): output = FileUtil.open(path,"wb") output.write(self.netopen('/'+common.CONFIG_SHA1)) output.close() input = FileUtil.open(path,"r") tmp2 = input.read() input.close() hash = self.netopen('/'+common.CONFIG_SIGN) print 'Verifing Hash Table.....' ok = verify(tmp2,hash) if not ok: print 'Verify Failed!' sys.exit() print 'Verify Successful1!'
def getnewsha1(self, path, oldsha1): output = FileUtil.open(path, "wb") output.write(self.getfile('/' + common.CONFIG_SHA1)) output.close() input = FileUtil.open(path, "r") tmp2 = input.read() input.close() hash = self.getfile('/' + common.CONFIG_SIGN) print 'Verifing Hash Table.....' ok = verify(tmp2, hash) if not ok: print 'Verify Failed!' raise urllib2.HTTPError(filename, '500', 'Verify Failed!', '', None) print 'Verify Successful1!'
def sign(message): #message = base92.encode(message) privatefile = FileUtil.open(common.CONFIG_PRIKEY,'r') keydata = privatefile.read() prikey = rsa.PrivateKey.load_pkcs1(keydata) signature = rsa.sign(message, prikey, 'SHA-1') return base92.encode(signature)
def verify(message,signature): #message = base92.encode(message) signature = base92.decode(signature) publicfile = FileUtil.open(common.CONFIG_PUBKEY,'r') keydata = publicfile.read() pubkey = rsa.PublicKey.load_pkcs1(keydata) try: rsa.verify(message,signature, pubkey) return True except rsa.pkcs1.VerificationError: return False