ファイルパスがあります。どうすればMD5ハッシュを取得できますか?
12 に答える
md5sum
コマンドラインで指定されたファイルの MD5 を計算して表示するコマンドの簡単な実装を次に示します。動作させるには、OpenSSL ライブラリ ( ) にリンクする必要がありますgcc md5.c -o md5 -lssl
。これは純粋な C ですが、C++ アプリケーションに簡単に適応させることができるはずです。
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
unsigned char result[MD5_DIGEST_LENGTH];
// Print the MD5 sum as hex-digits.
void print_md5_sum(unsigned char* md) {
int i;
for(i=0; i <MD5_DIGEST_LENGTH; i++) {
printf("%02x",md[i]);
}
}
// Get the size of the file by its file descriptor
unsigned long get_size_by_fd(int fd) {
struct stat statbuf;
if(fstat(fd, &statbuf) < 0) exit(-1);
return statbuf.st_size;
}
int main(int argc, char *argv[]) {
int file_descript;
unsigned long file_size;
char* file_buffer;
if(argc != 2) {
printf("Must specify the file\n");
exit(-1);
}
printf("using file:\t%s\n", argv[1]);
file_descript = open(argv[1], O_RDONLY);
if(file_descript < 0) exit(-1);
file_size = get_size_by_fd(file_descript);
printf("file size:\t%lu\n", file_size);
file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
MD5((unsigned char*) file_buffer, file_size, result);
munmap(file_buffer, file_size);
print_md5_sum(result);
printf(" %s\n", argv[1]);
return 0;
}
MD5 アルゴリズムを自分で実装することも (例は Web 全体にあります)、OpenSSL ライブラリにリンクして OpenSSL のダイジェスト関数を使用することもできます。バイト配列の MD5 を取得する例を次に示します。
#include <openssl/md5.h>
QByteArray AESWrapper::md5 ( const QByteArray& data) {
unsigned char * tmp_hash;
tmp_hash = MD5((const unsigned char*)data.constData(), data.length(), NULL);
return QByteArray((const char*)tmp_hash, MD5_DIGEST_LENGTH);
}
「 https://stackoverflow.com/questions/4393017/md5-implementation-in-c 」からリダイレクトされた人は、重複と誤ってラベル付けされているためです。
ここにある例は機能します:
http://www.zedwood.com/article/cpp-md5-function
VC++2010 でコンパイルしている場合は、main.cpp を次のように変更する必要があります。
#include <iostream> //for std::cout
#include <string.h> //for std::string
#include "MD5.h"
using std::cout; using std::endl;
int main(int argc, char *argv[])
{
std::string Temp = md5("The quick brown fox jumps over the lazy dog");
cout << Temp.c_str() << endl;
return 0;
}
このページの質問に答えるために、文字列ではなく char * 配列を読み込む場合は、MD5 クラスを少し変更する必要があります。
編集:
どうやら MD5 ライブラリの変更は明確ではありません。完全な VC++2010 ソリューションは、便利なように char * を含めるためにここにあります。
https://github.com/alm4096/MD5-Hash-Example-VS
ここに少し説明があります:
#include <iostream> //for std::cout
#include <string.h> //for std::string
#include <fstream>
#include "MD5.h"
using std::cout; using std::endl;
int main(int argc, char *argv[])
{
//Start opening your file
ifstream inBigArrayfile;
inBigArrayfile.open ("Data.dat", std::ios::binary | std::ios::in);
//Find length of file
inBigArrayfile.seekg (0, std::ios::end);
long Length = inBigArrayfile.tellg();
inBigArrayfile.seekg (0, std::ios::beg);
//read in the data from your file
char * InFileData = new char[Length];
inBigArrayfile.read(InFileData,Length);
//Calculate MD5 hash
std::string Temp = md5(InFileData,Length);
cout << Temp.c_str() << endl;
//Clean up
delete [] InFileData;
return 0;
}
MD5 ライブラリに以下を追加しただけです。
MD5.cpp:
MD5::MD5(char * Input, long length)
{
init();
update(Input, length);
finalize();
}
MD5.h:
std::string md5(char * Input, long length);
QFile file("bigimage.jpg");
if (file.open(QIODevice::ReadOnly))
{
QByteArray fileData = file.readAll();
QByteArray hashData = QCryptographicHash::hash(fileData,QCryptographicHash::Md5); // or QCryptographicHash::Sha1
qDebug() << hashData.toHex(); // 0e0c2180dfd784dd84423b00af86e2fc
}
私は今これを行う必要があり、c++ 11、ブースト、openssl に適したクロスプラットフォーム ソリューションが必要でした。D'Nabreのソリューションを出発点として、次のように煮詰めました。
#include <openssl/md5.h>
#include <iomanip>
#include <sstream>
#include <boost/iostreams/device/mapped_file.hpp>
const std::string md5_from_file(const std::string& path)
{
unsigned char result[MD5_DIGEST_LENGTH];
boost::iostreams::mapped_file_source src(path);
MD5((unsigned char*)src.data(), src.size(), result);
std::ostringstream sout;
sout<<std::hex<<std::setfill('0');
for(auto c: result) sout<<std::setw(2)<<(int)c;
return sout.str();
}
簡単なテスト実行可能ファイルは、次のことを示しています。
#include <iostream>
int main(int argc, char *argv[]) {
if(argc != 2) {
std::cerr<<"Must specify the file\n";
exit(-1);
}
std::cout<<md5_from_file(argv[1])<<" "<<argv[1]<<std::endl;
return 0;
}
いくつかのリンク ノート: Linux: -lcrypto -lboost_iostreams
Windows:-DBOOST_ALL_DYN_LINK libeay32.lib ssleay32.lib
私はこのファイルを使用しますhttp://people.csail.mit.edu/rivest/Md5.c
ぼたんを使ってこの操作などを行ったことがあります。AraK は Crypto++ を指摘しました。どちらのライブラリも完全に有効だと思います。今はあなた次第です:-)。
Crypto++ を使用すると、次のことができます。
#include <sha.h>
#include <iostream>
SHA256 sha;
while ( !f.eof() ) {
char buff[4096];
int numchars = f.read(...);
sha.Update(buff, numchars);
}
char hash[size];
sha.Final(hash);
cout << hash <<endl;
ハッシュを計算するためだけに数ギガバイトのファイルを読み取ることができないため、非常によく似たものが必要です。理論的には、それらをメモリ マップできますが、32 ビット プラットフォームをサポートする必要があります。これは、大きなファイルの場合は依然として問題です。
http://256stuff.com/sources/md5/にはかなりのライブラリがあり、使用例があります。これは MD5 用の最も単純なライブラリです。
@D'Nabre による C++ 用の実装のリワーク。最後に -lcrypto を付けてコンパイルすることを忘れないでください: gcc md5.c -o md5 -lcrypto
.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <openssl/md5.h>
using namespace std;
unsigned char result[MD5_DIGEST_LENGTH];
// function to print MD5 correctly
void printMD5(unsigned char* md, long size = MD5_DIGEST_LENGTH) {
for (int i=0; i<size; i++) {
cout<< hex << setw(2) << setfill('0') << (int) md[i];
}
}
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Specify the file..." << endl;
return 0;
}
ifstream::pos_type fileSize;
char * memBlock;
ifstream file (argv[1], ios::ate);
//check if opened
if (file.is_open() ) { cout<< "Using file\t"<< argv[1]<<endl; }
else {
cout<< "Unnable to open\t"<< argv[1]<<endl;
return 0;
}
//get file size & copy file to memory
//~ file.seekg(-1,ios::end); // exludes EOF
fileSize = file.tellg();
cout << "File size \t"<< fileSize << endl;
memBlock = new char[fileSize];
file.seekg(0,ios::beg);
file.read(memBlock, fileSize);
file.close();
//get md5 sum
MD5((unsigned char*) memBlock, fileSize, result);
//~ cout << "MD5_DIGEST_LENGTH = "<< MD5_DIGEST_LENGTH << endl;
printMD5(result);
cout<<endl;
return 0;
}
John Walker の実装にはソースが付属しています。