리눅스용 소스
Linux 2.4.x 운영체제에서 openssl 에서 제공하는 MD5 함수를 이용해서 코딩되었습니다.
컴파일 방법 : $gcc -o getmd5 getmd5.c -lssl -lcrypto
#include <openssl/md5.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
MD5_CTX lctx;
unsigned char digest[16];
char md5msg[40];
int i;
int fd;
struct stat status;
char *file_name = argv[1];
char *data;
MD5_Init(&lctx);
fd = open(file_name, O_RDONLY);
if (fd < 0)
{
perror("error : ");
exit(0);
}
fstat(fd, &status);
data = (char *)malloc(status.st_size);
read (fd, data, status.st_size);
MD5_Update(&lctx, data, status.st_size);
MD5_Final(digest, &lctx);
for (i = 0; i < sizeof(digest); ++i)
{
sprintf(md5msg+(i*2), "%02x", digest[i]);
}
printf ("%s\n", md5msg);
free(data);
}
아래는 윈도용 소스 입니다.
사용법:
Cmd5Capi md5Capi;
CString passwdStr;
totalStr = md5Capi.Digest(passwdStr);
code:--------------------------------------------------------------------------------// md5Capi.h: interface for the Cmd5Capi class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E
7B6D1FD7E2__INCLUDED_)
#define AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E
2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <wincrypt.h> // Cryptographic API Prototypes and Definitions
class Cmd5Capi
{
public:
CString &Digest(CString & csBuffer);
CString &GetDigest(void);
Cmd5Capi(CString & csBuffer);
Cmd5Capi();
virtual ~Cmd5Capi();
CString csDigest;
};
#endif // !defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E
7B6D1FD7E2__INCLUDED_)
--------------------------------------------------------------------------------
code:--------------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////
// md5Capi.cpp: implementation of the Cmd5Capi class.
// Calcule MD5 Digest using the WIN Crypto API.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "md5Capi.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cmd5Capi::Cmd5Capi()
{
csDigest.Empty();
}
Cmd5Capi::Cmd5Capi(CString & csBuffer)
{
Digest(csBuffer);
}
Cmd5Capi::~Cmd5Capi()
{
}
CString &Cmd5Capi::GetDigest(void)
{
return csDigest;
}
CString &Cmd5Capi::Digest(CString & csBuffer)
{
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
BYTE bHash[0x7f];
DWORD dwHashLen= 16; // The MD5 algorithm always returns 16 bytes.
DWORD cbContent= csBuffer.GetLength();
BYTE* pbContent= (BYTE*)csBuffer.GetBuffer(cbContent);
if(CryptAcquireContext(&hCryptProv,
NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if(CryptCreateHash(hCryptProv,
CALG_MD5, // algorithm identifier definitions see: wincrypt.h
0, 0, &hHash))
{
if(CryptHashData(hHash, pbContent, cbContent, 0))
{
if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
{
// Make a string version of the numeric digest value
csDigest.Empty();
CString tmp;
for (int i = 0; i<16; i++)
{
tmp.Format("%02x", bHash[i]);
csDigest+=tmp;
}
}
else csDigest=_T("Error getting hash param");
}
else csDigest=_T("Error hashing data");
}
else csDigest=_T("Error creating hash");
}
else csDigest=_T("Error acquiring context");
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
csBuffer.ReleaseBuffer();
return csDigest;
}