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


Posted in Python onJune 30, 2015

本文实例讲述了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 相关文章推荐
Python内置的字符串处理函数详细整理(覆盖日常所用)
Aug 19 Python
Python functools模块学习总结
May 09 Python
python通过apply使用元祖和列表调用函数实例
May 26 Python
Python中内建函数的简单用法说明
May 05 Python
Python采用Django制作简易的知乎日报API
Aug 03 Python
python匹配两个短语之间的字符实例
Dec 25 Python
Python实现的对本地host127.0.0.1主机进行扫描端口功能示例
Feb 15 Python
python实现七段数码管和倒计时效果
Nov 23 Python
15行Python代码实现免费发送手机短信推送消息功能
Feb 27 Python
使用OpenCV校准鱼眼镜头的方法
Nov 26 Python
利用Opencv实现图片的油画特效实例
Feb 28 Python
8g内存用python读取10文件_面试题-python 如何读取一个大于 10G 的txt文件?
May 28 Python
Python实现高效求解素数代码实例
Jun 30 #Python
python实现DES加密解密方法实例详解
Jun 30 #Python
python实现的系统实用log类实例
Jun 30 #Python
python实现在windows服务中新建进程的方法
Jun 30 #Python
python实现线程池的方法
Jun 30 #Python
python实现的简单FTP上传下载文件实例
Jun 30 #Python
编写Python CGI脚本的教程
Jun 29 #Python
You might like
动态新闻发布的实现及其技巧
2006/10/09 PHP
PHP中key和current,next的联合运用实例分析
2016/03/29 PHP
Yii2创建控制器(createController)方法详解
2016/07/23 PHP
PHP实现的mysql读写分离操作示例
2018/05/22 PHP
Thinkphp 框架扩展之行为扩展原理与实现方法分析
2020/04/23 PHP
javascript遍历控件实例详细解析
2014/01/10 Javascript
javascript中的正则表达式使用指南
2015/03/01 Javascript
JS运动基础框架实例分析
2015/03/03 Javascript
纯javascript制作日历控件
2015/07/17 Javascript
Javascript使用SWFUpload进行多文件上传
2016/11/16 Javascript
Bootstrap Table使用整理(四)之工具栏
2017/06/09 Javascript
bootstrap table表格客户端分页实例
2017/08/07 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
Angular2进阶之如何避免Dom误区
2018/04/02 Javascript
详解关于vue-area-linkage走过的坑
2018/06/27 Javascript
微信小程序自定义组件实现tabs选项卡功能
2018/07/14 Javascript
layer弹出层 iframe层去掉滚动条的实例代码
2018/08/17 Javascript
让webpack+vue-cil项目不再自动打开浏览器的方法
2018/09/27 Javascript
js中this的指向问题归纳总结
2018/11/28 Javascript
Vue实现push数组并删除的例子
2019/11/01 Javascript
使用React代码动态生成栅格布局的方法
2020/05/24 Javascript
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
2014/06/09 Python
利用Python破解斗地主残局详解
2017/06/30 Python
python对配置文件.ini进行增删改查操作的方法示例
2017/07/28 Python
如何在sae中设置django,让sae的工作环境跟本地python环境一致
2017/11/21 Python
pytorch-RNN进行回归曲线预测方式
2020/01/14 Python
python统计字符的个数代码实例
2020/02/07 Python
利用Python批量识别电子账单数据的方法
2021/02/08 Python
python利用proxybroker构建爬虫免费IP代理池的实现
2021/02/21 Python
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
学校岗位设置方案
2014/01/16 职场文书
技校学生个人职业生涯规划范文
2014/03/03 职场文书
2015年学生会工作总结范文
2015/03/31 职场文书
高二数学教学反思
2016/02/18 职场文书
你会写请假条吗?
2019/06/26 职场文书
送给教师们,到底该如何写好教学反思?
2019/07/02 职场文书