python实现文件快照加密保护的方法

作者:秋风秋雨 时间:2022-08-21 20:01:29 

本文实例讲述了python实现文件快照加密保护的方法。分享给大家供大家参考。具体如下:

这段代码可以对指定的目录进行扫描,包含子目录,对指定扩展名的文件进行SHA-1加密后存储在cvs文件,以防止文件被篡改

调用方法:python snapper.py > todayCheck.csv


# Hello, this is a script written in Python. See http://www.pyhon.org
#
# Snapper 1.2p
#
# 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'
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,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(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 line
sys.stderr.write('All done.\n')

希望本文所述对大家的Python程序设计有所帮助。

标签:python,文件,加密
0
投稿

猜你喜欢

  • python+jinja2实现接口数据批量生成工具

    2022-04-30 14:00:20
  • 理解python中装饰器的作用

    2022-01-29 07:58:01
  • Python元组的定义及使用

    2021-06-30 01:24:40
  • python pygame实现五子棋双人联机

    2022-04-12 22:41:04
  • 简化ADO数据库操作的控件(带分页功能)

    2008-05-20 13:15:00
  • Windows 2003服务器上传文件受限制的解决方法

    2011-02-14 11:29:00
  • Python如何利用%操作符格式化字符串详解

    2022-07-17 14:08:39
  • Python如何利用opencv实现手势识别

    2023-09-02 21:55:13
  • MySQL数据库中CHAR与VARCHAR之争

    2011-05-05 16:33:00
  • django实现web接口 python3模拟Post请求方式

    2023-07-28 15:18:14
  • uni-app网络请求、数据缓存实例详解

    2023-08-09 03:49:12
  • Python入门学习指南分享

    2023-09-02 15:02:21
  • Python+OpenCV进行人脸面部表情识别

    2021-07-18 22:47:35
  • Python爬虫之urllib库详解

    2022-01-09 23:03:27
  • asp中access升级到sql server后要做的工作

    2007-08-11 13:35:00
  • 精简版的MySQL制作步骤

    2011-03-08 09:52:00
  • python中requests库session对象的妙用详解

    2021-10-30 14:42:58
  • python+opencv像素的加减和加权操作的实现

    2021-11-29 03:39:14
  • Python多线程正确用法实例解析

    2022-03-22 14:31:58
  • Python答题卡识别并给出分数的实现代码

    2022-04-10 03:52:46
  • asp之家 网络编程 m.aspxhome.com