c++代码获取一个字符串的md5值并输出
#include "md5.h"
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
extern "C"
{
void getStrMd5(char* filePath)
{
MD5 md5(filePath);
std::string str = md5.md5();
printf("%s", str.c_str());
}
}
生成动态库,供python调用
g++ md5.cpp getStringMd5.cpp -fPIC -shared -o getHash.so
获取字符串的md5值:getHashValue.py
#!/usr/bin/python
from ctypes import *
import os
import sys
target = (sys.argv)[1]
libtest = cdll.LoadLibrary(os.getcwd() + '/getHash.so')
print libtest.getStrMd5(target)
处理得到哈希目录脚本:process.py
32位md5值,前4位用作目录名, 后28位用作文件名
#!/usr/bin/python
import sys
import os
import os.path
import shutil
import commands
srcPath = os.path.join((sys.path)[0], "res")
desPath = os.path.join((sys.path)[0], "HashRes")
if os.path.exists(desPath):
shutil.rmtree(desPath)
os.mkdir(desPath)
def process(path):
for parent, dirnames, filenames in os.walk(path):
for file in filenames :
if file != ".DS_Store" :
fullPath = parent + "/" + file
splitPath = fullPath[len(path)+1:]
command = "python getHashValue.py " + splitPath
f = os.popen(command)
data = f.readlines()[0][0:31]
fileNewPath = desPath + "/" + data[0:4]
if not os.path.exists(fileNewPath):
os.mkdir(fileNewPath)
shutil.copyfile(parent+"/"+file, fileNewPath+"/"+data[4:32])
process(os.path.join((sys.path)[0], "res"))