【SHA1の場合】
openssl.exe sha1 [ファイル名]
Visual Studioを使ったC++(コンソールアプリ)のサンプルは下記のような感じになります。
サンプルではWindows8 RP版のisoイメージのファイルハッシュを計算してテストしてみました。
- #include "stdafx.h"
- #include "Hash.h" // OpenSSLのinclude
- #define TARGET_FILE "E:\\Windows8-ReleasePreview-32bit-Japanese.iso"
- #define BUFSIZE (1024 * 16) // OpenSSLの定義と同じ
- int _tmain(int argc, _TCHAR* argv[])
- {
- unsigned char sha1hash[SHA_DIGEST_LENGTH] = { 0 };
- unsigned char buf[BUFSIZE];
- DWORD dwStart = 0;
- DWORD dwEnd = 0;
- // SHA1 using OpneSSL lib
- printf("Calculate file hash for sha1\n");
- dwStart = ::GetTickCount();
- SHA_CTX ctx;
- SHA1_Init(&ctx);
- CFile file(TARGET_FILE, CFile::modeRead);
- UINT nRead = 0;
- do{
- nRead = file.Read(buf, BUFSIZE);
- if(nRead > 0){
- SHA1_Update(&ctx, buf, nRead);
- }
- }while(nRead == BUFSIZE);
- SHA1_Final(sha1hash, &ctx);
- dwEnd = ::GetTickCount() - dwStart;
- printf("SHA1(%s)= ", TARGET_FILE);
- for(int i = 0; i < SHA_DIGEST_LENGTH; i++){
- printf("%.2x",sha1hash[i]);
- }
- printf("\n");
- printf("Elapsed Time(ms) = %d\n", dwEnd);
- return 0;
- }
- // OpenSSL lib
- #pragma comment(lib, "openssl/lib/libeay32.lib")
- #pragma comment(lib, "openssl/lib/ssleay32.lib")
【MD5の場合】
openssl.exe md5 [ファイル名]
ソースの方はMD5_~に変わるだけで、あとは全く同じなので省略します。
0 件のコメント:
コメントを投稿