00001 00032 #include "stdafx.h" 00033 #include "CPipeSHA1.h" 00034 00035 namespace AxPipe { 00036 namespace Stock { 00039 CPipeSHA1::CPipeSHA1() { 00040 if (!CryptAcquireContext(&m_hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) { 00041 SetError(AxPipe::ERROR_CODE_STOCK, _T("CPipeSHA1::CPipeSHA1() [CryptAcquireContext() failed]")); 00042 } 00043 m_hHash = NULL; 00044 } 00045 00048 CPipeSHA1::~CPipeSHA1() { 00049 if (m_hHash) CryptDestroyHash(m_hHash); 00050 if (m_hCryptProv) CryptReleaseContext(m_hCryptProv, 0); 00051 } 00052 00056 void 00057 CPipeSHA1::Out(AxPipe::CSeg *pSeg) { 00058 if (!CryptHashData(m_hHash, pSeg->PtrRd(), (DWORD)pSeg->Len(), 0)) { 00059 SetError(AxPipe::ERROR_CODE_STOCK, _T("CPipeSHA1::CPipeSHA1() [CryptHashData() failed]")); 00060 } 00061 m_cb += pSeg->Len(); 00062 Pump(pSeg); 00063 } 00064 00067 unsigned __int64 00068 CPipeSHA1::CountBytes() { 00069 return m_cb; 00070 } 00071 00074 unsigned char * 00075 CPipeSHA1::GetHash() { 00076 return (unsigned char *)m_Hash.GetLeft(160); 00077 } 00078 00081 bool 00082 CPipeSHA1::OutOpen() { 00083 bool fReturn = CPipe::OutOpen(); 00084 00085 if (!CryptCreateHash(m_hCryptProv, CALG_SHA1, 0, 0, &m_hHash)) { 00086 SetError(AxPipe::ERROR_CODE_STOCK, _T("CPipeSHA1::CPipeSHA1() [CryptCreateHash() failed]")); 00087 } 00088 m_cb = 0; 00089 return fReturn; 00090 } 00091 00094 bool 00095 CPipeSHA1::OutClose() { 00096 DWORD dwHashLen = sizeof m_Hash; 00097 if (!CryptGetHashParam(m_hHash, HP_HASHVAL, (unsigned char *)&m_Hash, &dwHashLen, 0)) { 00098 SetError(AxPipe::ERROR_CODE_STOCK, _T("CPipeSHA1::OutClose() [CryptGetHashParam() failed]")); 00099 } 00100 ASSAPI(CryptDestroyHash(m_hHash) == TRUE); 00101 m_hHash = NULL; 00102 return CPipe::OutClose(); 00103 } 00104 } 00105 }