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.

137 lines
3.9 KiB

  1. // AuxHash.cpp -- Auxillary Hash class implementation
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "NoWarning.h"
  8. #include "ForceLib.h"
  9. #include <malloc.h> // for _alloca
  10. #include <windows.h>
  11. #include <wincrypt.h>
  12. #include <scuOsExc.h>
  13. #include "AuxHash.h"
  14. /////////////////////////// HELPER /////////////////////////////////
  15. /////////////////////////// LOCAL VAR /////////////////////////////////
  16. /////////////////////////// PUBLIC /////////////////////////////////
  17. // Types
  18. // C'tors/D'tors
  19. AuxHash::AuxHash(AuxContext &rauxcontext,
  20. ALG_ID ai,
  21. HCRYPTKEY hKey)
  22. : m_hHash(0)
  23. {
  24. if (!CryptCreateHash(rauxcontext(), ai, hKey, 0, &m_hHash))
  25. throw scu::OsException(GetLastError());
  26. }
  27. AuxHash::~AuxHash()
  28. {
  29. if (m_hHash)
  30. CryptDestroyHash(m_hHash);
  31. }
  32. // Operators
  33. // Operations
  34. // Import a hash value into the hash object.
  35. void
  36. AuxHash::Import(Blob const &rblbHashValue)
  37. {
  38. if (!CryptSetHashParam(m_hHash, HP_HASHVAL,
  39. const_cast<BYTE *>(rblbHashValue.data()), 0))
  40. throw scu::OsException(GetLastError());
  41. }
  42. // Update the hash value with the data blob.
  43. void
  44. AuxHash::Update(Blob const &rblob)
  45. {
  46. if (!CryptHashData(m_hHash, rblob.data(), rblob.length(), 0))
  47. throw scu::OsException(GetLastError());
  48. }
  49. // Access
  50. ALG_ID
  51. AuxHash::AlgId() const
  52. {
  53. ALG_ID ai;
  54. DWORD c = sizeof ai;
  55. if (!CryptGetHashParam(m_hHash, HP_ALGID,
  56. reinterpret_cast<BYTE *>(&ai), &c, 0))
  57. throw scu::OsException(GetLastError());
  58. return ai;
  59. }
  60. DWORD
  61. AuxHash::Size() const
  62. {
  63. DWORD dwSize;
  64. DWORD cSize = sizeof dwSize;
  65. if (!CryptGetHashParam(m_hHash, HP_ALGID,
  66. reinterpret_cast<BYTE *>(&dwSize), &cSize, 0))
  67. throw scu::OsException(GetLastError());
  68. return dwSize;
  69. }
  70. // Completes the hash returning the hash value. No more hashing can
  71. // occur with this object.
  72. Blob
  73. AuxHash::Value() const
  74. {
  75. DWORD dwSize = Size();
  76. BYTE *bp = reinterpret_cast<BYTE *>(_alloca(dwSize));
  77. if (!CryptGetHashParam(m_hHash, HP_HASHVAL, bp, &dwSize, 0))
  78. throw scu::OsException(GetLastError());
  79. return Blob(bp, dwSize);
  80. }
  81. // Returns the hash of the data blob passed. No more hashing can
  82. // occur with this object.
  83. Blob
  84. AuxHash::Value(Blob const &rblob)
  85. {
  86. Update(rblob);
  87. return Value();
  88. }
  89. // Predicates
  90. // Static Variables
  91. /////////////////////////// PROTECTED /////////////////////////////////
  92. // C'tors/D'tors
  93. // Operators
  94. // Operations
  95. // Access
  96. // Predicates
  97. // Static Variables
  98. /////////////////////////// PRIVATE /////////////////////////////////
  99. // C'tors/D'tors
  100. // Operators
  101. // Operations
  102. // Access
  103. // Predicates
  104. // Static Variables