批量获取文件的摘要(md5、sha1、sha256)

批量获取文件的摘要(md5、sha1、sha256)

0x01 功能

刚刚写了一个小工具:用于批量获取文件的摘要:md5、sha1、sha256

具体用法:
>>>> python3 getFileFeatures.py  [参数:当前目录下的文件夹或者文件]
批量计算,只需要把所有文件放入当前目录下的文件夹中,参数为文件夹名
计算后的结果:在当前目录下生成“filefeatures.txt”文件,存在该文件中

0x02源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/12/25/025 22:53
# @Author  : H
# @File    : getFileFeatures.py

"""批量获取文件夹中各个文件的MD5、SHA1、SHA256"""

import hashlib
import os
import sys


def getFileFeatures(dirpath):
   path = os.getcwd() + "\\" + dirpath

   subpaths = []
   datas = {} # 结果存为json,以便后需
   if os.path.exists(path):
       # 路径存在
       if os.path.isdir(path):
           # 获得的路径为文件夹
           for root, dirs, files in os.walk(path):
               for file in files:
                   subpaths.append([file, os.path.join(root, file)])
       else:
           # 获得的路径为文件
           file = path.split("\\")[-1]
           subpaths.append([file, path])

       for i in subpaths:
           f = open(i[1], "rb")
           rb = f.read()
           data = {'MD5': hashlib.md5(rb).hexdigest(),
                   'SHA1': hashlib.sha1(rb).hexdigest(),
                   'SHA256': hashlib.sha256(rb).hexdigest()}
           datas[i[0]] = data
           f.close()
       with open("filefeatures.txt", "a")as f:

           for k, v in datas.items():
               f.writelines("filename:\t" + k + "\n")
               print("[+]succeed get the teatures of " + k)
               for key, value in v.items():
                   f.writelines(key + ":\t" + value + "\n")
               f.writelines("\n")

   else:
       print("Folder does not exist")


if sys.argv:
   print("""
  _     _      _     _      _     _      _     _      _     _      _     _   
 (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)  
  / ._. \      / ._. \      / ._. \      / ._. \      / ._. \      / ._. \   
__\( Y )/__  __\( Y )/__  __\( Y )/__  __\( Y )/__  __\( Y )/__  __\( Y )/__ 
(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)
  || K ||      || I ||      || L ||      || L ||      || E ||      || R ||   
_.' `-' '._  _.' `-' '._  _.' `-' '._  _.' `-' '._  _.' `-' '._  _.' `-' '._ 
(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-'\.-.)(.-./`-`\.-.)
`-'     `-'  `-'     `-'  `-'     `-'  `-'     `-'  `-'     `-'  `-'     `-' 
   
======  function:  Get the characteristics of file or files in folder ========

=>=>=>=>=>usage :  python getFileFeatures.py file/dir     <=<=<=<=<=<=<=<=<=<=
   """)
   if len(sys.argv) == 2:
       getFileFeatures(sys.argv[1])
   else:
       print("[-]Usage errors")

0x03运行

1、使用
在这里插入图片描述
2、结果
在这里插入图片描述
!!!成长的道路没有一帆风顺!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值