[vcpkg-hash] Use BCrypt on Windows
MODIFY
include/vcpkg/base/strings.h
include/vcpkg/base/strings.h
@@ -46,7 +46,9 @@ namespace vcpkg::Strings
bool case_insensitive_ascii_equals(const CStringView left, const CStringView right);
- std::string ascii_to_lowercase(std::string input);
+ std::string ascii_to_lowercase(std::string s);
+
+ std::string ascii_to_uppercase(std::string s);
bool case_insensitive_ascii_starts_with(const std::string& s, const std::string& pattern);

MODIFY
include/vcpkg/base/util.h
include/vcpkg/base/util.h
#include <utility>
#include <vector>
-#include <vcpkg/base/optional.h>
-
namespace vcpkg::Util
{
template<class Container>
@@ -158,6 +156,8 @@ namespace vcpkg::Util
MoveOnlyBase& operator=(const MoveOnlyBase&) = delete;
MoveOnlyBase& operator=(MoveOnlyBase&&) = default;
+
+ ~MoveOnlyBase() = default;
};
struct ResourceBase
@@ -168,6 +168,8 @@ namespace vcpkg::Util
ResourceBase& operator=(const ResourceBase&) = delete;
ResourceBase& operator=(ResourceBase&&) = delete;
+
+ ~ResourceBase() = default;
};
template<class T>
@@ -214,4 +216,10 @@ namespace vcpkg::Util
return e == E::YES;
}
}
+
+ template<class T>
+ void unused(T&& param)
+ {
+ (void)param;
+ }
}

MODIFY
src/vcpkg/base/strings.cpp
src/vcpkg/base/strings.cpp
namespace vcpkg::Strings::details
{
// To disambiguate between two overloads
- static const auto isspace = [](const char c) { return std::isspace(c); };
+ static bool IS_SPACE(const char c) { return std::isspace(c) != 0; };
// Avoids C4244 warnings because of char<->int conversion that occur when using std::tolower()
static char tolower_char(const char c) { return static_cast<char>(std::tolower(c)); }
+ static char toupper_char(const char c) { return static_cast<char>(std::toupper(c)); }
#if defined(_WIN32)
static _locale_t& c_locale()
@@ -114,6 +115,12 @@ namespace vcpkg::Strings
return s;
}
+ std::string ascii_to_uppercase(std::string s)
+ {
+ std::transform(s.begin(), s.end(), s.begin(), &details::toupper_char);
+ return s;
+ }
+
bool case_insensitive_ascii_starts_with(const std::string& s, const std::string& pattern)
{
#if defined(_WIN32)
@@ -136,8 +143,8 @@ namespace vcpkg::Strings
std::string trim(std::string&& s)
{
- s.erase(std::find_if_not(s.rbegin(), s.rend(), details::isspace).base(), s.end());
- s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), details::isspace));
+ s.erase(std::find_if_not(s.rbegin(), s.rend(), details::IS_SPACE).base(), s.end());
+ s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), details::IS_SPACE));
return std::move(s);
}

MODIFY
src/vcpkg/commands.hash.cpp
src/vcpkg/commands.hash.cpp
#include "pch.h"
+#include <vcpkg/base/checks.h>
+#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
+#if defined(_WIN32)
+#include <bcrypt.h>
+
+#ifndef NT_SUCCESS
+#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
+#endif
+
+namespace vcpkg::Commands::Hash
+{
+ namespace
+ {
+ static std::string to_hex(const unsigned char* string, const size_t bytes)
+ {
+ static constexpr char HEX_MAP[] = "0123456789abcdef";
+
+ std::string output;
+ output.resize(2 * bytes);
+
+ size_t current_char = 0;
+ for (size_t i = 0; i < bytes; i++)
+ {
+ // high
+ output[current_char] = HEX_MAP[(string[i] & 0xF0) >> 4];
+ ++current_char;
+ // low
+ output[current_char] = HEX_MAP[(string[i] & 0x0F)];
+ ++current_char;
+ }
+
+ return output;
+ }
+
+ 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);
+ }
+ };
+ }
+
+ std::string get_file_hash(const VcpkgPaths&, const fs::path& path, const std::string& hash_type)
+ {
+ BCryptAlgorithmHandle algorithm_handle;
+
+ NTSTATUS error_code = BCryptOpenAlgorithmProvider(
+ &algorithm_handle.handle, Strings::to_utf16(Strings::ascii_to_uppercase(hash_type)).c_str(), nullptr, 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to open the algorithm provider");
+
+ DWORD hash_buffer_bytes;
+ DWORD cb_data;
+ error_code = BCryptGetProperty(algorithm_handle.handle,
+ BCRYPT_HASH_LENGTH,
+ reinterpret_cast<PUCHAR>(&hash_buffer_bytes),
+ sizeof(DWORD),
+ &cb_data,
+ 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to get hash length");
+ const ULONG length_in_bytes = hash_buffer_bytes;
+
+ BCryptHashHandle hash_handle;
+
+ 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");
+
+ FILE* file = nullptr;
+ const auto ec = _wfopen_s(&file, path.c_str(), L"rb");
+ Checks::check_exit(VCPKG_LINE_INFO, ec == 0, "Failed to open file: %s", path.u8string());
+ unsigned char buffer[4096];
+ while (const auto actual_size = fread(buffer, 1, sizeof(buffer), file))
+ {
+ error_code = BCryptHashData(hash_handle.handle, buffer, static_cast<ULONG>(actual_size), 0);
+ Checks::check_exit(VCPKG_LINE_INFO, NT_SUCCESS(error_code), "Failed to hash data");
+ }
+
+ fclose(file);
+
+ std::unique_ptr<unsigned char[]> hash_buffer = std::make_unique<UCHAR[]>(length_in_bytes);
+
+ 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);
+ }
+}
+
+#else
namespace vcpkg::Commands::Hash
{
- std::string get_file_hash(const VcpkgPaths& paths, fs::path const& path, std::string const& hash_type)
+ std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type)
{
const std::string cmd_line = Strings::format(
R"("%s" -E %ssum "%s")",
@@ -32,7 +133,11 @@ namespace vcpkg::Commands::Hash
Util::erase_remove_if(hash, isspace);
return hash;
}
+}
+#endif
+namespace vcpkg::Commands::Hash
+{
const CommandStructure COMMAND_STRUCTURE = {
Strings::format("The argument should be a file path\n%s",
Help::create_example_string("hash boost_1_62_0.tar.bz2")),
@@ -44,19 +149,12 @@ namespace vcpkg::Commands::Hash
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
- args.parse_arguments(COMMAND_STRUCTURE);
+ Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
- if (args.command_arguments.size() == 1)
- {
- auto hash = get_file_hash(paths, args.command_arguments[0], "SHA512");
- System::println(hash);
- }
- if (args.command_arguments.size() == 2)
- {
- auto hash = get_file_hash(paths, args.command_arguments[0], args.command_arguments[1]);
+ const fs::path file_to_hash = args.command_arguments[0];
+ const std::string algorithm = args.command_arguments.size() == 2 ? args.command_arguments[1] : "SHA512";
+ const std::string hash = get_file_hash(paths, file_to_hash, algorithm);
System::println(hash);
- }
-
Checks::exit_success(VCPKG_LINE_INFO);
}
}

MODIFY
vcpkg/vcpkg.vcxproj
vcpkg/vcpkg.vcxproj
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>

MODIFY
vcpkgmetricsuploader/vcpkgmetricsuploader.vcxproj
vcpkgmetricsuploader/vcpkgmetricsuploader.vcxproj
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>winhttp.lib;version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>

MODIFY
vcpkgtest/vcpkgtest.vcxproj
vcpkgtest/vcpkgtest.vcxproj
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;$(VsInstallRoot)\VC\Auxiliary\VS\UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+ <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;$(VsInstallRoot)\VC\Auxiliary\VS\UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+ <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;$(VsInstallRoot)\VC\Auxiliary\VS\UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+ <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;$(VsInstallRoot)\VC\Auxiliary\VS\UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+ <AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

[vcpkg-hash] Use BCrypt on Windows