logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

MD5 파일 변조 검사 관련 소스 (리눅스/Windows)

by digipine posted Oct 29, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

 

리눅스용 소스
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;


}

TAG •

List of Articles
No. Subject Author Date Views
23 소켓 통신을 이용한 HTTP 서버 개발 강의록 file digipine 2020.08.01 1482
22 [shared lib] so 동적 라이브러리 만들기와 사용법 - 리눅스 digipine 2017.11.01 6434
21 [linux] zlib build 방법 digipine 2017.11.01 1483
20 [Linux] Pthread 사용법, Thread 동기화 총정리 digipine 2017.11.01 294048
19 [C/C++] 현재시간 구하기 digipine 2017.10.28 2212
18 [C/C++] Random UUID String 생성 코드 digipine 2021.10.21 1302
17 wchar_t에 대하여 digipine 2017.11.01 7343
16 Unix C/C++ Input and Output Function Reference digipine 2017.11.01 88072
15 STL MAP 예제로 공부하기 digipine 2017.10.29 5204
14 Solaris에서 pmap을 이용하여 백그라운드 프로세스 메모리 크기 구하기 digipine 2017.10.29 28598
13 Solaris 10에 개발 Tool (gcc,vim,gdb) 설치 digipine 2017.10.29 1257
» MD5 파일 변조 검사 관련 소스 (리눅스/Windows) digipine 2017.10.29 2613
11 make -j 옵션으로 컴파일 속도 최적화 하기 digipine 2017.11.01 2759
10 Linux C 언어로 Shell 명령어 실행하기 digipine 2017.11.01 22587
9 Introduce to Singly-linked List file digipine 2017.11.01 1288
8 fopen 파일 열기 모드 옵션 정리 digipine 2017.11.02 3894
7 C를 이용한 객체지향 프로그래밍 digipine 2017.11.01 568
6 Callback in C++ 와 Delegate 차이점 digipine 2017.11.01 2525
5 C++에서 extern의 역할, 기능 digipine 2017.10.29 2656
4 C++ 컴파일 오류(error): variable 'std::istringstream sstream' has initializer but incomplete type digipine 2017.11.02 21077
Board Pagination Prev 1 2 Next
/ 2