|
MD5ハッシュを生成する |
| ダウンロード : MD5ハッシュ生成プログラムサンプル(2008/04/05) |
|
#include "wincrypt.h" |
|
BYTE pbyHash[0x7f]; HCRYPTPROV hCryptProv = NULL; HCRYPTHASH hHash = NULL; DWORD dwHashLen = 16; // キーコンテナを準備する if (::CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET) ) { // MD5 ハッシュ計算のインスタンスを生成する if (::CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) { // ハッシュ値を計算する if (::CryptHashData(hHash, (BYTE*)c_pbySource, dwLen, 0)) { // 計算されたハッシュ値を取り出す if (::CryptGetHashParam(hHash, HP_HASHVAL, pbyHash, &dwHashLen, 0)) { // 取り出しに成功したのでその文字列を指定の配列にコピーする ::memcpy(pbyMD5Hash, pbyHash, dwHashLen); } } } } // 各種リソースを開放する if (hHash) ::CryptDestroyHash(hHash); if (hCryptProv) ::CryptReleaseContext(hCryptProv, 0); |
|
// --------------------------------------------------------------------- // Shift-JIS を UTF-8 に変換する // 参考/http://www.atmark.gr.jp/~s2000/r/rtl/encode.html // --------------------------------------------------------------------- BOOL SJIStoUTF8(CString& r_strText) { if (r_strText.IsEmpty()) return FALSE; USES_CONVERSION; // ShiftJIS 文字列を Unicode に変換する LPWSTR lpw = T2W(static_cast<LPCTSTR>(r_strText)); // Unicode 文字列を UTF-8 に変換する const int c_nMultiByte = (lstrlenW(lpw) + 1) * 3; LPSTR lpa = reinterpret_cast<LPSTR>(_alloca(c_nMultiByte)); *lpa = '\0'; const int nBytes = ::WideCharToMultiByte( CP_UTF8, 0, lpw, -1, lpa, c_nMultiByte, NULL, NULL ); ATLASSERT(nBytes > 0); if (nBytes == 0) return FALSE; // 変換結果を反映する r_strText = lpa; return TRUE; } |