Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
4.1 KiB

  1. // CoMD5.cpp : Implementation of CCoMD5
  2. #include "stdafx.h"
  3. #include "ComMD5.h"
  4. #include "CoMD5.h"
  5. #include "pperr.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CCoMD5
  8. VOID
  9. ToHex(
  10. LPBYTE pSrc,
  11. UINT cSrc,
  12. LPSTR pDst
  13. )
  14. /*++
  15. Routine Description:
  16. Convert binary data to ASCII hex representation
  17. Arguments:
  18. pSrc - binary data to convert
  19. cSrc - length of binary data
  20. pDst - buffer receiving ASCII representation of pSrc
  21. Return Value:
  22. Nothing
  23. --*/
  24. {
  25. #define TOHEX(a) ((a)>=10 ? 'a'+(a)-10 : '0'+(a))
  26. for ( UINT x = 0, y = 0 ; x < cSrc ; ++x )
  27. {
  28. UINT v;
  29. v = pSrc[x]>>4;
  30. pDst[y++] = TOHEX( v );
  31. v = pSrc[x]&0x0f;
  32. pDst[y++] = TOHEX( v );
  33. }
  34. pDst[y] = '\0';
  35. }
  36. LONG MD5(UCHAR* pBuf, UINT nBuf, UCHAR* digest)
  37. {
  38. MD5_CTX context;
  39. if(pBuf==NULL || IsBadReadPtr((CONST VOID*)pBuf, (UINT)nBuf))
  40. {
  41. return ERROR_INVALID_PARAMETER;
  42. }
  43. MD5Init (&context);
  44. MD5Update (&context, pBuf, nBuf);
  45. MD5Final (&context);
  46. memcpy(digest, context.digest, 16);
  47. return ERROR_SUCCESS;
  48. }
  49. STDMETHODIMP
  50. CCoMD5::MD5HashASCII(
  51. BSTR bstrSource,
  52. BSTR* pbstrDigest
  53. )
  54. {
  55. HRESULT hr;
  56. LONG lResult;
  57. UCHAR achDigest[20];
  58. CHAR achDigestStr[36];
  59. if (NULL == pbstrDigest)
  60. {
  61. return E_POINTER;
  62. }
  63. lResult = MD5((UCHAR*)(CHAR*)bstrSource,
  64. ::SysStringByteLen(bstrSource),
  65. achDigest);
  66. if(lResult != ERROR_SUCCESS)
  67. {
  68. hr = PP_E_MD5_HASH_FAILED;
  69. goto Cleanup;
  70. }
  71. ToHex(achDigest, 16, achDigestStr);
  72. *pbstrDigest = ::SysAllocStringByteLen(achDigestStr, ::strlen(achDigestStr));
  73. if(*pbstrDigest == NULL)
  74. {
  75. hr = E_OUTOFMEMORY;
  76. goto Cleanup;
  77. }
  78. hr = S_OK;
  79. Cleanup:
  80. return hr;
  81. }
  82. STDMETHODIMP
  83. CCoMD5::MD5Hash(
  84. BSTR bstrSource,
  85. BSTR* pbstrDigest
  86. )
  87. {
  88. HRESULT hr;
  89. BSTR asciiDigest = NULL;
  90. if (NULL == pbstrDigest)
  91. {
  92. return E_POINTER;
  93. }
  94. hr = MD5HashASCII(bstrSource, &asciiDigest);
  95. if (S_OK != hr)
  96. goto Cleanup;
  97. *pbstrDigest = ::SysAllocString((WCHAR*)_bstr_t((CHAR*)asciiDigest));
  98. if(*pbstrDigest == NULL)
  99. {
  100. hr = E_OUTOFMEMORY;
  101. goto Cleanup;
  102. }
  103. hr = S_OK;
  104. Cleanup:
  105. if (asciiDigest)
  106. {
  107. SysFreeString(asciiDigest);
  108. }
  109. return hr;
  110. }
  111. // this hashes only the half of the bstrSource. It is provided for backword compatility only
  112. STDMETHODIMP CCoMD5::MD5HashAsp(
  113. BSTR bstrSource,
  114. BSTR* pbstrDigest
  115. )
  116. {
  117. HRESULT hr;
  118. LONG lResult;
  119. UCHAR achDigest[20];
  120. CHAR achDigestStr[36];
  121. if (NULL == pbstrDigest)
  122. {
  123. return E_POINTER;
  124. }
  125. lResult = MD5((UCHAR*)(
  126. CHAR*)_bstr_t(bstrSource),
  127. ::SysStringLen(bstrSource),
  128. achDigest);
  129. if(lResult != ERROR_SUCCESS)
  130. {
  131. hr = E_FAIL;
  132. goto Cleanup;
  133. }
  134. ToHex(achDigest, 16, achDigestStr);
  135. *pbstrDigest = ::SysAllocString((WCHAR*)_bstr_t((CHAR*)achDigestStr));
  136. if(*pbstrDigest == NULL)
  137. {
  138. hr = E_OUTOFMEMORY;
  139. goto Cleanup;
  140. }
  141. hr = S_OK;
  142. Cleanup:
  143. return hr;
  144. }
  145. /////////////////////////////////////////////////////////////////////////////
  146. // IPassportService implementation
  147. STDMETHODIMP CCoMD5::Initialize(BSTR configfile, IServiceProvider* p)
  148. {
  149. return S_OK;
  150. }
  151. STDMETHODIMP CCoMD5::Shutdown()
  152. {
  153. return S_OK;
  154. }
  155. STDMETHODIMP CCoMD5::ReloadState(IServiceProvider*)
  156. {
  157. return S_OK;
  158. }
  159. STDMETHODIMP CCoMD5::CommitState(IServiceProvider*)
  160. {
  161. return S_OK;
  162. }
  163. STDMETHODIMP CCoMD5::DumpState(BSTR* pbstrState)
  164. {
  165. ATLASSERT( *pbstrState != NULL &&
  166. "CCoMD5:DumpState - "
  167. "Are you sure you want to hand me a non-null BSTR?" );
  168. HRESULT hr = S_OK;
  169. return hr;
  170. }