• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    md5 checksum MD5
    Last Modify : 10 December, 2010(11:15)
    /**
     * @file CMD5Manager.h
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date October 7, 2009 17:49:3
     * @section LICENSE
     *
     * Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
     *
     * Permission is hereby granted, free of charge, to any person
     * obtaining a copy of this software and associated documentation
     * files (the "Software"), to deal in the Software without
     * restriction, including without limitation the rights to use,
     * copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following
     * conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     * OTHER DEALINGS IN THE SOFTWARE.
     * 
     * @section DESCRIPTION
     * <b>sample code</b>
     * @code
     *      CMD5 objMD5;
     *      if (objMD5.Check(szPath, szFileName) != KErrNone)
     *      {
     *			LOG("ERROR!!!\n");
     *      }
     *      objMD5.Sync();
     * @endcode
     * @n
     * @code
     *      if (CMD5Manager::getInstance()->Check2(szPath, szFileName, szMD5FileFullPath.c_str()))
     *		{
     *			LOG("ERROR!!!\n");
     *		}
     *      objMD5.Sync();
     * @endcode
     *
     * @section CHANGE_LOG
     * - ANDROID 를 위하여 try, catch 문 제거 June 7, 2010 15:6:35
     * 
     * - doxygen style 로 변경 2010-08-20 (11:29:50)
     * 
     * - 사용하지 않는 함수 제거 2010-08-20 (14:33:16)
     * 
     * - 사용하지 않는 interface 제거 2010-08-20 (14:33:39)
     * 
     * - refactoring 2010-08-20 (14:54:26)
     * 
     * - CheckWithKey, CheckFullPathWithKey 메소드 추가 2010-08-20 (15:38:29)
     *
     * - add method toMD5 2010-09-29 (16:56:46)
     */
    
    #ifndef __CD_CMD5MANAGER_H__
    #define __CD_CMD5MANAGER_H__
    
    
    #include <string>
    #include "CDSingleton.h"
    
    
    /**
     * @class CMD5
     * @brief md5sum 처리 클래스
     */
    class CMD5
    {
    public:
    
    	/*
    	 * Construction
    	 */
    	CMD5();
    
    	/*
    	 * Destructor
    	 */
    	virtual ~CMD5();
    
    public:
    
    	/*
    	 * Getter methods
    	 */
    	int Check(const char* path, const char* name);
    
    	int Check2(const char* path, const char* name, const char* md5key=NULL);
    
    	int CheckFullPath(const char* fullpath, const char* md5key);
    
    	int CheckWithKey(const char* path, const char* name, std::string md5keyValue);
    	
    	int CheckFullPathWithKey(const char* fullpath, std::string md5keyValue);
    
    	int Sync();
    
    	static std::string toMD5(const char* plainText, bool uppperCase=false);
    
    protected:
    
     	/*
    	 * Internal methods
    	 */
    	inline std::string getFullpath(const char* path, const char* name);
    
    protected:
    
    	/*
    	 * Attributes
    	 */
    };
    
    
    /**
     * @class CMD5Manager
     * @brief singleton 형태의 md5 manager
     */
    class CMD5Manager
    	: public CMD5
    	, public CDSingletonT<CMD5Manager>
    {
    
    };
    
    #endif
    
    /**
     * @file CMD5Manager.cpp
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date October 7, 2009 17:49:6
     * @section LICENSE
     *
     * Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
     *
     * Permission is hereby granted, free of charge, to any person
     * obtaining a copy of this software and associated documentation
     * files (the "Software"), to deal in the Software without
     * restriction, including without limitation the rights to use,
     * copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following
     * conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     * OTHER DEALINGS IN THE SOFTWARE.
     * 
     * @section DESCRIPTION
     *
     *
     * @section CHANGE_LOG
     *
     */
    
    
    #include "CMD5Manager.h"
    #include "RSLOGGER.H"
    #include "RSDEFINES.H"
    #include "CDFile.h"
    #include "openssl/md5.h"
    
    
    /*
    * Constructor CMD5Manager
    */
    CMD5::CMD5()
    {
    
    }
    
    
    /*
    * Destructor ~CMD5Manager
    */
    CMD5::~CMD5()
    {
    
    }
    
    
    /**
     * md5sum 을 비교 한다.
     *
     * @param path md5sum 대상 경로
     * @param path md5sum 대상 파일 명 
     * @return true or false
     */
    int CMD5::Check(const char* path, const char* name)
    {
    	LOG1("CMD5::Check\n");
    
    	/*
    	 * filename.jpg.md5 형태의 md5 기준 파일 
    	 */
    	std::string fullpath = getFullpath(path, name);
    	return CheckFullPath(fullpath.c_str(), (fullpath + ".md5").c_str());
    }
    
    
    /**
     * md5key file 경로를 지정해서 md5sum 을 하려고 할 때 
     * 즉 key 값이 target file 과 다른 경로에 존재할 경우
     *
     * @param szPath md5 대상 파일 경로
     * @param szName md5 대상 파일 이름
     * @param szMD5Path=NULL md5key 값
     * @return true or false
     */
    int CMD5::Check2(const char* path, const char* name, const char* md5key/*=NULL*/)
    {
    	LOG1("CMD5::Check\n");
    
    	std::string fullpath = getFullpath(path, name);
    	return CheckFullPath(fullpath.c_str(), md5key ? md5key : (fullpath + ".md5").c_str());
    }
    
    
    /**
     * full path name 으로 경로를 받았을 경우 md5sum 처리
     *
     * @param fullpath full path name
     * @param md5key md5key path
     * @return true or false
     */
    int CMD5::CheckFullPath(const char* fullpath, const char* md5key)
    {
    	LOG1("CMD5Manager::CheckFullPath\n");
    
    	std::string md5KeyValue;
    	CDFile md5KeyFile;
    	md5KeyFile.Open(md5key, "r");
    	md5KeyFile.ReadBuffer(md5KeyValue);
    	md5KeyFile.Close();
    
    	return CheckFullPathWithKey(fullpath, md5KeyValue);
    }
    
    
    //
    // Sync
    //
    
    int CMD5::Sync()
    {
    	LOG1("CMD5::Sync\n");
    
    #if !defined(_WIN32_WCE)
    	system("sync");
    #endif
    
    	return true;
    }
    
    
    /**
     * md5sum 비교를 한다.
     *
     * @param path 대상 경로
     * @param name 대상 파일 명
     * @param md5keyValue 기준 키 값
     * @return true or false
     */
    int CMD5::CheckWithKey(const char* path, const char* name, std::string md5keyValue)
    {
    	LOG1("CMD5Manager::CheckWithKey\n");
    
    	std::string fullpath = getFullpath(path, name);
    	return CheckFullPathWithKey(fullpath.c_str(), md5keyValue);
    }
    
    
    /**
     * md5sum 비교를 한다.
     *
     * @param fullpath 대상 전체 경로
     * @param md5keyValue 기준 키 값
     * @return true or false
     */
    int CMD5::CheckFullPathWithKey(const char* fullpath, std::string md5keyValue)
    {
    	LOG1("CMD5Manager::CheckFullPathWithKey\n");
    
    	/*
    	* target 이 되는 md5 file
    	*/	
    	CDFile md5Target;
    	if (md5Target.Open(fullpath, "rb") == false)
    	{
    		LOG3("ERROR::CMD5::Check <Do not found file : %s>\n", fullpath);
    
    		return KErrNotFound;
    	}
    
    	size_t len;
    	MD5_CTX lctx;
    	unsigned char buffer[512] = {0};
    	unsigned char digest[16]  = {0};
    
    	/*
    	* file을 512 사이즈 씩 읽어서 md5sum 을 생성한다.
    	*/
    	MD5_Init(&lctx);
    	do
    	{
    		len = md5Target.ReadBuffer(buffer, 512);
    		MD5_Update(&lctx, buffer, len);
    	}
    	while(len > 0);
    	MD5_Final(digest, &lctx);
    
    	md5Target.Close();
    
    	/*
    	* 완성된 md5sum binary data 를 hex string 으로 변경
    	*/
    	char toHex[40] = {0};
    	for (size_t i=0; i<16; ++i)
    	{
    		sprintf(toHex + (i * 2), "%02x", digest[i]);
    	}
    
    	/*
    	* key value 와 hex string 을 비교한다.
    	*/
    
    	LOG2("md5sum : %s\n", toHex);
    	LOG2("md5key : %s\n", md5keyValue.c_str());
    
    	if (md5keyValue == toHex)
    	{
    		return KErrNone;
    	}
    	else
    	{
    		/*
    		* md5 비교 대상 파일이 일치하지 않을 경우는 삭제한다.
    		*/
    		CDFile::RemoveFile(fullpath);
    
    		return KErrCorrupt;
    	}
    }
    
    
    /**
     * path / name 을 full path name 으로 만들어 준다.
     *
     * @param path path value
     * @param name name value
     * @return full path name
     */
    std::string CMD5::getFullpath(const char* path, const char* name)
    {
    	LOG1("CMD5Manager::getFullpath\n");
    
    	char lastChr;
    	std::string fullpath;
    
    	fullpath = path;
    	lastChr  = path[fullpath.size() - 1];
    
    	/*
    	 * path 에 마지막이나 name 처음에 '/' 나 '\' 가 없으면 붙여준다.
    	 */
    	if (!((lastChr == '\\' || lastChr == '/') || (name[0] == '/' || name[0] == '\\')))
    	{
    		fullpath += '/';
    	}
    
    	fullpath += name;
    
    	return fullpath;
    }
    
    
    /**
     * 
     *
     * @param plainText 
     * @return 
     */
    std::string CMD5::toMD5(const char* plainText, bool uppperCase/*=false*/)
    {
    	LOG1("CMD5Manager::toMD5\n");
    
    	MD5_CTX lctx;
    	unsigned char digest[16]  = {0};
    
    	MD5_Init(&lctx);
    	MD5_Update(&lctx, plainText, strlen(plainText));
    	MD5_Final(digest, &lctx);
    
    	char toHex[40] = {0};
    
    	if (uppperCase)
    	{
    		for (size_t i=0; i<16; ++i)
    		{
    			sprintf(toHex + (i * 2), "%02X", digest[i]);
    		}
    	}
    	else
    	{
    		for (size_t i=0; i<16; ++i)
    		{
    			sprintf(toHex + (i * 2), "%02x", digest[i]);
    		}
    	}
    
    	return toHex;
    }
    

    Comment


    입력하3 1502976667



    Locations of visitors to this page