aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2018-04-05 21:28:02 -0700
committerAlexander Karatarakis <alkarata@microsoft.com>2018-04-06 17:26:58 -0700
commitca9503b9d7f9bca6e2f9881a7f4ec9ce9d600928 (patch)
treec64f9ae4dfc9dc1de5a518e7df8551b30c7e4b1e
parent3f6f9f3f4d48ef0115f3095f7d8d2c6d3b0c63e1 (diff)
downloadvcpkg-ca9503b9d7f9bca6e2f9881a7f4ec9ce9d600928.tar.gz
vcpkg-ca9503b9d7f9bca6e2f9881a7f4ec9ce9d600928.zip
[vcpkg hash] Refactor and tweaks in BCryptHasher.
-rw-r--r--toolsrc/src/vcpkg/commands.hash.cpp83
1 files changed, 42 insertions, 41 deletions
diff --git a/toolsrc/src/vcpkg/commands.hash.cpp b/toolsrc/src/vcpkg/commands.hash.cpp
index c0130f12f..19f73b463 100644
--- a/toolsrc/src/vcpkg/commands.hash.cpp
+++ b/toolsrc/src/vcpkg/commands.hash.cpp
@@ -39,28 +39,52 @@ namespace vcpkg::Commands::Hash
return output;
}
- struct BCryptAlgorithmHandle : Util::ResourceBase
+ class BCryptHasher
{
- BCRYPT_ALG_HANDLE handle = nullptr;
+ struct BCryptAlgorithmHandle : Util::ResourceBase
+ {
+ BCRYPT_ALG_HANDLE handle = nullptr;
+
+ ~BCryptAlgorithmHandle()
+ {
+ if (handle) BCryptCloseAlgorithmProvider(handle, 0);
+ }
+ };
+
+ struct BCryptHashHandle : Util::ResourceBase
+ {
+ BCRYPT_HASH_HANDLE handle = nullptr;
+
+ ~BCryptHashHandle()
+ {
+ if (handle) BCryptDestroyHash(handle);
+ }
+ };
- ~BCryptAlgorithmHandle()
+ static void initialize_hash_handle(BCryptHashHandle& hash_handle,
+ const BCryptAlgorithmHandle& algorithm_handle)
{
- if (handle) BCryptCloseAlgorithmProvider(handle, 0);
+ const NTSTATUS error_code =
+ BCryptCreateHash(algorithm_handle.handle, &hash_handle.handle, nullptr, 0, nullptr, 0, 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to initialize the hasher");
}
- };
- struct BCryptHashHandle : Util::ResourceBase
- {
- BCRYPT_HASH_HANDLE handle = nullptr;
+ static void hash_data(BCryptHashHandle& hash_handle, const unsigned char* buffer, const size_t& data_size)
+ {
+ const NTSTATUS error_code = BCryptHashData(
+ hash_handle.handle, const_cast<unsigned char*>(buffer), static_cast<ULONG>(data_size), 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to hash data");
+ }
- ~BCryptHashHandle()
+ static std::string finalize_hash_handle(const BCryptHashHandle& hash_handle, const ULONG length_in_bytes)
{
- if (handle) BCryptDestroyHash(handle);
+ std::unique_ptr<unsigned char[]> hash_buffer = std::make_unique<UCHAR[]>(length_in_bytes);
+ const NTSTATUS error_code = BCryptFinishHash(hash_handle.handle, hash_buffer.get(), length_in_bytes, 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to finalize the hash");
+ return to_hex(hash_buffer.get(), length_in_bytes);
}
- };
- struct BCryptHasher
- {
+ public:
explicit BCryptHasher(const std::string& hash_type)
{
NTSTATUS error_code =
@@ -85,7 +109,7 @@ namespace vcpkg::Commands::Hash
std::string hash_file(const fs::path& path) const
{
BCryptHashHandle hash_handle;
- this->initialize_hash_handle(hash_handle);
+ initialize_hash_handle(hash_handle, this->algorithm_handle);
FILE* file = nullptr;
const auto ec = _wfopen_s(&file, path.c_str(), L"rb");
@@ -97,41 +121,18 @@ namespace vcpkg::Commands::Hash
}
fclose(file);
- return this->finalize_hash_handle(hash_handle);
+ return finalize_hash_handle(hash_handle, length_in_bytes);
}
std::string hash_string(const std::string& s) const
{
BCryptHashHandle hash_handle;
- this->initialize_hash_handle(hash_handle);
- hash_data(hash_handle, reinterpret_cast<unsigned char*>(const_cast<char*>(s.c_str())), s.size());
- return this->finalize_hash_handle(hash_handle);
+ initialize_hash_handle(hash_handle, this->algorithm_handle);
+ hash_data(hash_handle, reinterpret_cast<const unsigned char*>(s.c_str()), s.size());
+ return finalize_hash_handle(hash_handle, length_in_bytes);
}
private:
- void initialize_hash_handle(BCryptHashHandle& hash_handle) const
- {
- const NTSTATUS error_code =
- BCryptCreateHash(this->algorithm_handle.handle, &hash_handle.handle, nullptr, 0, nullptr, 0, 0);
- Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to initialize the hasher");
- }
-
- void hash_data(BCryptHashHandle& hash_handle, unsigned char* buffer, const size_t& data_size) const
- {
- const NTSTATUS error_code =
- BCryptHashData(hash_handle.handle, buffer, static_cast<ULONG>(data_size), 0);
- Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to hash data");
- }
-
- std::string finalize_hash_handle(const BCryptHashHandle& hash_handle) const
- {
- std::unique_ptr<unsigned char[]> hash_buffer = std::make_unique<UCHAR[]>(this->length_in_bytes);
- const NTSTATUS error_code =
- BCryptFinishHash(hash_handle.handle, hash_buffer.get(), this->length_in_bytes, 0);
- Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to finalize the hash");
- return to_hex(hash_buffer.get(), this->length_in_bytes);
- }
-
BCryptAlgorithmHandle algorithm_handle;
ULONG length_in_bytes;
};