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 相关文章推荐
使用cx_freeze把python打包exe示例
Jan 24 Python
python统计文本字符串里单词出现频率的方法
May 26 Python
python模块简介之有序字典(OrderedDict)
Dec 01 Python
Python编程实现二叉树及七种遍历方法详解
Jun 02 Python
win8下python3.4安装和环境配置图文教程
Jul 31 Python
pygame游戏之旅 游戏中添加显示文字
Nov 20 Python
python虚拟环境迁移方法
Jan 03 Python
Python实现的列表排序、反转操作示例
Mar 13 Python
Pycharm创建项目时如何自动添加头部信息
Nov 14 Python
PyTorch 普通卷积和空洞卷积实例
Jan 07 Python
Python 程序员必须掌握的日志记录
Aug 17 Python
Python可视化神器pyecharts之绘制箱形图
Jul 07 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
无线电的诞生过程
2021/03/01 无线电
PHP 翻页 实例代码
2009/08/07 PHP
php 无限级分类 获取顶级分类ID
2016/03/13 PHP
[原创]PHP正则删除html代码中a标签并保留标签内容的方法
2017/05/23 PHP
javascript操作cookie_获取与修改代码
2009/05/21 Javascript
javascript 精粹笔记
2010/05/09 Javascript
方便实用的jQuery checkbox复选框全选功能简单实例
2013/10/09 Javascript
深入理解jQuery中live与bind方法的区别
2013/12/18 Javascript
JS实现超精简响应鼠标显示二级菜单代码
2015/09/12 Javascript
jQuery ajax时间差导致的变量赋值问题分析
2016/01/22 Javascript
巧用数组制作图片切换js代码
2016/11/29 Javascript
深入理解JS继承和原型链的问题
2016/12/17 Javascript
AngularJS入门教程之Helloworld示例
2016/12/25 Javascript
jQuery动态生成不规则表格(前后端)
2017/02/21 Javascript
Angular.js自动化测试之protractor详解
2017/07/07 Javascript
使用JavaScript实现链表的数据结构的代码
2017/08/02 Javascript
在 Angular中 使用 Lodash 的方法
2018/02/11 Javascript
vue中render函数的使用详解
2018/10/12 Javascript
小程序卡片切换效果组件wxCardSwiper的实现
2020/02/13 Javascript
go和python变量赋值遇到的一个问题
2017/08/31 Python
Python程序员面试题 你必须提前准备!
2018/01/16 Python
深入浅析Python传值与传址
2018/07/10 Python
在python中实现将一张图片剪切成四份的方法
2018/12/05 Python
Django接收post前端返回的json格式数据代码实现
2019/07/31 Python
利用python实现汉字转拼音的2种方法
2019/08/12 Python
修改Pandas的行或列的名字(重命名)
2019/12/18 Python
详解Css3新特性应用之过渡与动画
2017/01/10 HTML / CSS
在数据文件自动增长时,自动增长是否会阻塞对文件的更新
2014/05/01 面试题
销售行业个人求职自荐信
2013/09/25 职场文书
护理专业本科生自荐信
2013/10/01 职场文书
夜大毕业生自我评价分享
2013/11/10 职场文书
高一英语教学反思
2014/01/22 职场文书
学生会干部自荐信
2014/02/04 职场文书
《水乡歌》教学反思
2014/04/24 职场文书
优秀党员先进事迹材料
2014/12/18 职场文书
高温慰问简报
2015/07/21 职场文书