How to Implement file snapshot encryption and protection in python

Source: Internet
Author: User
Tags dot net

How to Implement file snapshot encryption and protection in python

This example describes how to implement file snapshot encryption protection in python. Share it with you for your reference. The details are as follows:

This code can scan a specified directory, including sub-directories. SHA-1 is encrypted for files with the specified extension and stored in the cvs file to prevent file tampering.

Call method: python snapper. py> todayCheck.csv

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

# Hello, this is a script written in Python. See http://www.pyhon.org

#

# Snapper 1.2 p

#

# This script will walk a directory (and its subdirectories) and compute

# SHA (Secure Hash Algorithm) for specific files (according to their

# Extensions) and ouput a CSV file (suited for loading into a spreadsheet

# Editor, a database or simply comparing with diff or ExamDiff .).

#

# You can redirect the output of this script to a file.

# Eg. python snapper. py> todayCheck.csv

#

# This script can be usefull to check system files tampering.

#

# This script is public domain. Feel free to reuse it.

# The author is:

# Sebastien SAUVAGE

# <Sebsauvage at sebsauvage dot net>

# Http://sebsauvage.net

#

# More quick & dirty scripts are available at http://sebsauvage.net/python/

#

# Directory to scan and extensions are hardcoded below:

DirectoryStart = r'c: \ Windows'

Extensionlistparts using '.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, sys

Def snapper (directoryStart, extensionList ):

OS. path. walk (directoryStart, snapper_callback, extensionList)

Def snapper_callback (extensionList, directory, files ):

Sys. stderr. write ('scanning' + directory + '\ n ')

For 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 (1, 65536)

While len (data )! = 0:

Digest. update (data)

Data = file. read (1, 65536)

File. close ()

Return '"' + filepath + '",' + str (OS. stat (filepath) [6]) + ', "' + digest. hexdigest () + '"'

Sys. stderr. write ('snapper 1.1 p-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 line

Sys. stderr. write ('all done. \ n ')

I hope this article will help you with Python programming.

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.