python实现文件快照加密保护的方法
本文实例讲述了python实现文件快照加密保护的方法。分享给大家供大家参考。具体如下:
这段代码可以对指定的目录进行扫描,包含子目录,对指定扩展名的文件进行SHA-1加密后存储在cvs文件,以防止文件被篡改
调用方法:pythonsnapper.py>todayCheck.csv
#Hello,thisisascriptwritteninPython.Seehttp://www.pyhon.org # #Snapper1.2p # #Thisscriptwillwalkadirectory(anditssubdirectories)andcompute #SHA(SecureHashAlgorithm)forspecificfiles(accordingtotheir #extensions)andouputaCSVfile(suitedforloadingintoaspreadsheet #editor,adatabaseorsimplycomparingwithdifforExamDiff.). # #Youcanredirecttheoutputofthisscripttoafile. #eg.pythonsnapper.py>todayCheck.csv # #Thisscriptcanbeusefulltochecksystemfilestampering. # #Thisscriptispublicdomain.Feelfreetoreuseit. #Theauthoris: #SebastienSAUVAGE #<sebsauvageatsebsauvagedotnet> #http://sebsauvage.net # #Morequick&dirtyscriptsareavailableathttp://sebsauvage.net/python/ # #Directorytoscanandextensionsarehardcodedbelow: directoryStart=r'c:\windows' extensionList=['.exe','.dll','.ini','.ocx','.cpl','.vxd','.drv','.vbx','.com','.bat','.src', '.sys','.386','.acm','.ax','.bpl','.bin','.cab','.olb','.mpd','.pdr','.jar'] importos,string,sha,stat,sys defsnapper(directoryStart,extensionList): os.path.walk(directoryStart,snapper_callback,extensionList) defsnapper_callback(extensionList,directory,files): sys.stderr.write('Scanning'+directory+'\n') forfileNameinfiles: ifos.path.isfile(os.path.join(directory,fileName)): ifstring.lower(os.path.splitext(fileName)[1])inextensionList: filelist.append(fileSHA(os.path.join(directory,fileName))) deffileSHA(filepath): sys.stderr.write('Reading'+os.path.split(filepath)[1]+'\n') file=open(filepath,'rb') digest=sha.new() data=file.read(65536) whilelen(data)!=0: digest.update(data) data=file.read(65536) file.close() return'"'+filepath+'",'+str(os.stat(filepath)[6])+',"'+digest.hexdigest()+'"' sys.stderr.write('Snapper1.1p-http://sebsauvage.net/python/\n') filelist=[] snapper(directoryStart,extensionList) sys.stderr.write('Sorting...\n') filelist.sort() filelist.insert(0,'"Filepath","Filesize","SHA"') sys.stderr.write('Printing...\n') forlineinfilelist: printline sys.stderr.write('Alldone.\n')
希望本文所述对大家的Python程序设计有所帮助。