#keywords C++,cryptohash.h,Usage,SHA-1,MD5 [[TableOfContents]] == cryptohash == Microsoft Cryptographic Provider를 쉽게 사용할 수 있는 헬퍼 템플릿 클래스. attachment:cryptohash.h {{{#!gcode /* * Authod: Marius Bancila (http://mariusbancila.ro) * License: Code Project Open License * Disclaimer: The software is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. * Published: 19.09.2012 */ #pragma once #include #include #pragma comment(lib, "Advapi32.lib") ... }}} == Usage == === SHA-1 === {{{#!gcode CString strMsg("ABCDE"); CString strHash; CT2CA pszConvertedAnsiString(strMsg); string msg(pszConvertedAnsiString); crypto::sha1_helper_t hash_helper; string hash = hash_helper.hexdigesttext(msg); strHash.Format(L"Result= %S", hash.c_str()); OutputDebugString(strHash); }}} ---- {{{ Result= 7be07aaf460d593a323d0db33da05b64bfdcb3a5 }}} === hexdigestfile === {{{#!gcode CString strFilename("C:\\test_file.exe"); CString strHash; CT2CA pszConvertedAnsiString(strFilename); string target(pszConvertedAnsiString); crypto::sha1_helper_t hash_helper; string hash = hash_helper.hexdigestfile(target.c_str()); if (hash.empty()) { CString str; str.Format(L"SHA1 HASH Error code: %d; Error message: %S", hash_helper.lasterror().errorCode, hash_helper.lasterror().errorMessage.c_str()); OutputDebugString(str); return FALSE; } strHash.Format(_T("Result= %S"), hash.c_str()); OutputDebugString(strHash); }}} ---- {{{ Result= a735aff97994ad5ec4db1762697cc7faeff3a2f0 }}} === SHA-256 === 알고리즘 추가 하려면 다음을 참조하여 패치한다. {{{#!gcode cryptohash.h *************** *** 91,98 **** if(!::CryptAcquireContext( &m_hCryptProv, NULL, ! NULL, ! PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { m_lasterror = errorinfo_t(GetLastError(), "Failed to acquire cryptographic context."); --- 91,98 ---- if(!::CryptAcquireContext( &m_hCryptProv, NULL, ! MS_ENH_RSA_AES_PROV, ! PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { m_lasterror = errorinfo_t(GetLastError(), "Failed to acquire cryptographic context."); *************** *** 301,309 **** --- 301,311 ---- typedef cryptohash_t md4_t; typedef cryptohash_t md5_t; typedef cryptohash_t sha1_t; + typedef cryptohash_t sha256_t; typedef cryptohash_helper_t md2_helper_t; typedef cryptohash_helper_t md4_helper_t; typedef cryptohash_helper_t md5_helper_t; typedef cryptohash_helper_t sha1_helper_t; + typedef cryptohash_helper_t sha256_helper_t; } }}} {{{#!gcode string msg("..."); crypto::sha256_helper_t hash_helper; string hash = hash_helper.hexdigesttext(msg, TRUE); }}}