-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash_value.py
74 lines (65 loc) · 2.45 KB
/
hash_value.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 输入文件路径返回如下信息:
# File path:
# Size:
# Date modified:
# MD5:
# SHA1:
# CRC32:
# ----------------------------------------------------------
# Author: mozillazg
# Blog: http://mzgblog.appspot.com/
# Version: 2011/5/20
import hashlib
import zlib
import os
from time import localtime, strftime
import re
# hash 计算 MD5 、SHA1
def hash_value(filename, filesize, maxsize, xhash):
"""提供要计算 hash 值的文件路径、文件大小、每次读取的文件块大小及
算法的摘要对象
返回字符串类型的 hash 值
"""
with open(filename, 'rb') as openfile: # 打开文件,一定要是以二进制打开
while True:
data = openfile.read(maxsize) # 读取文件块
if not data: # 直到读完文件
break
xhash.update(data)
return xhash.hexdigest()
# CRC32 计算 CRC32
def crc32_value(filename, filesize, maxsize):
"""提供要计算 CRC32 值的文件路径、文件大小及每次读取的文件块大小
返回整型类型的 CRC32 值
"""
crc = 0
with open(filename, 'rb') as openfile:
while True:
data = openfile.read(maxsize)
if not data:
break
crc = zlib.crc32(data, crc)
return crc
if __name__ == '__main__':
text = raw_input('请输入文件路径:').strip().decode('utf-8')
# 去除 filepath 首尾的单引号及双引号
filepath = re.sub('^\"|^\'|\"$|\'$', '', text).strip()
blocksize = 1024 * 1024 # 每次读取的文件块的大小(bytes)
size = os.path.getsize(filepath) # 文件大小
date = strftime('%Y/%m/%d %H:%M:%S', # 文件最后修改时间
localtime(os.path.getmtime(filepath)))
md5 = hash_value(filepath, size, blocksize, hashlib.md5()) # 计算 MD5 值
sha1 = hash_value(filepath, size, blocksize, hashlib.sha1()) # 计算 SHA1 值
crc32 = crc32_value(filepath, size, blocksize) # CRC32
print 'File path: %s' % filepath
print 'Size: %s bytes' % size
print 'Date modified: %s' % date
#print 'MD5: %s' % md5
#print 'SHA1: %s' % sha1
#print 'CRC32: %x' % (crc32 & 0xffffffff)
# 处理结果使其中的字母大写
print 'MD5: %s' % md5.upper()
print 'SHA1: %s' % sha1.upper()
print 'CRC32: %X' % (crc32 & 0xffffffff)