Source code of Windows XP (NT5)
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.

168 lines
2.8 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. lResult = MD5((UCHAR*)(CHAR*)bstrSource,
  60. ::SysStringByteLen(bstrSource),
  61. achDigest);
  62. if(lResult != ERROR_SUCCESS)
  63. {
  64. hr = PP_E_MD5_HASH_FAILED;
  65. goto Cleanup;
  66. }
  67. ToHex(achDigest, 16, achDigestStr);
  68. *pbstrDigest = ::SysAllocStringByteLen(achDigestStr, ::strlen(achDigestStr));
  69. if(*pbstrDigest == NULL)
  70. {
  71. hr = E_OUTOFMEMORY;
  72. goto Cleanup;
  73. }
  74. hr = S_OK;
  75. Cleanup:
  76. return hr;
  77. }
  78. STDMETHODIMP
  79. CCoMD5::MD5Hash(
  80. BSTR bstrSource,
  81. BSTR* pbstrDigest
  82. )
  83. {
  84. HRESULT hr;
  85. ATL::CComBSTR asciiDigest;
  86. hr = MD5HashASCII(bstrSource, &asciiDigest);
  87. if (S_OK != hr)
  88. goto Cleanup;
  89. *pbstrDigest = ::SysAllocString((WCHAR*)_bstr_t((CHAR*)(BSTR)asciiDigest));
  90. if(*pbstrDigest == NULL)
  91. {
  92. hr = E_OUTOFMEMORY;
  93. goto Cleanup;
  94. }
  95. hr = S_OK;
  96. Cleanup:
  97. return hr;
  98. }
  99. /////////////////////////////////////////////////////////////////////////////
  100. // IPassportService implementation
  101. STDMETHODIMP CCoMD5::Initialize(BSTR configfile, IServiceProvider* p)
  102. {
  103. return S_OK;
  104. }
  105. STDMETHODIMP CCoMD5::Shutdown()
  106. {
  107. return S_OK;
  108. }
  109. STDMETHODIMP CCoMD5::ReloadState(IServiceProvider*)
  110. {
  111. return S_OK;
  112. }
  113. STDMETHODIMP CCoMD5::CommitState(IServiceProvider*)
  114. {
  115. return S_OK;
  116. }
  117. STDMETHODIMP CCoMD5::DumpState(BSTR* pbstrState)
  118. {
  119. ATLASSERT( *pbstrState != NULL &&
  120. "CCoMD5:DumpState - "
  121. "Are you sure you want to hand me a non-null BSTR?" );
  122. HRESULT hr = S_OK;
  123. return hr;
  124. }