Python method for file snapshot encryption protection

Source: Internet
Author: User
Tags dot net
The example of this paper describes how Python implements file-snapshot encryption protection. Share to everyone for your reference. Specific as follows:

This code can scan the specified directory, contain subdirectories, and store the files of the specified extension in a CVS file after SHA-1 encryption to prevent the file from being tampered with.

Calling method: Python snapper.py > Todaycheck.csv

# Hello, this was a script written in Python. See http://www.pyhon.org## Snapper 1.2p## This script would walk a directory (and its subdirectories) and compute# SHA (Sec Ure Hash algorithm) for specific files (according to their# extensions) and ouput a CSV file (suited for loading into a SP readsheet# Editor,a database or simply comparing with diff or examdiff.). # # can redirect the output of this script to a file.# eg. Python snapper.py > todaycheck.csv## This script can are usefull to check the system files tampering.## this script is Publi C domain. Feel free to reuse it.# the author is:# Sebastien sauvage#
 
  
# http://sebsauvage.net## More Quick & dirty scripts is available at http://sebsauvage.net/python/## Directory to Scan and extensions are hardcoded Below: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 ']import os,string,sha,stat,sysdef snapper (Directorystart, extensionlist): Os.path.walk (Directorystart, Snapper_callback, Extensionlist) def snapper_callback (extensionlist, directory, files): Sys.stderr.write (' scanning ' +directory+ ' \ n ') for the fileName in Files:if os.path.isfile (Os.path.join (directory,filename)): If String.Lower ( Os.path.splitext (FileName) [1]) in ExtensionList:filelist.append (Filesha (Os.path.join (Directory,filename))) def Filesha (filepath): Sys.stderr.write (' Reading ' +os.path.split (filepath) [1]+ ' \ n ') file = open (filepath, ' RB ') digest = Sha.new () data = File.read (65536) while Len (data) = 0:digest.update (data) data = File.read (65536) file.close () return ' "' +filepath+ '", ' +str (OS . stat (filepath) [6]) + ', ' ' +digest.hexdigest () + ' "' Sys.stderr.write (' Snapper 1.1p-http://sebsauvage.net/python/\n ') FileList = []snapper (Directorystart, extensionlist) sys.stderr.write (' sorting...\n ') filelist.sort () Filelist.insert (0, ' File path ', "File Size", "SHA" ') sys.stderr.write (' printing...\n ') for line in Filelist:print Linesys.stderr.write ( ' All done.\n ')
 

Hopefully this article will help you with Python programming.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.