aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2018-03-16 16:53:48 -0700
committerRobert Schumacher <roschuma@microsoft.com>2018-03-20 19:12:35 -0700
commitab7985a34b8a4de59dc9a6e6c4c40fbb564797b8 (patch)
tree700acb5e350b0b7fa14e1ebbce6b73f8ae31c24a /toolsrc/src
parenteab1d5c531695c2a644276832578e5550dd9abf6 (diff)
downloadvcpkg-ab7985a34b8a4de59dc9a6e6c4c40fbb564797b8.tar.gz
vcpkg-ab7985a34b8a4de59dc9a6e6c4c40fbb564797b8.zip
[vcpkg-hash] Use BCrypt on Windows
Diffstat (limited to 'toolsrc/src')
-rw-r--r--toolsrc/src/vcpkg/base/strings.cpp13
-rw-r--r--toolsrc/src/vcpkg/commands.hash.cpp124
2 files changed, 121 insertions, 16 deletions
diff --git a/toolsrc/src/vcpkg/base/strings.cpp b/toolsrc/src/vcpkg/base/strings.cpp
index a08512c9f..5fedf3e1f 100644
--- a/toolsrc/src/vcpkg/base/strings.cpp
+++ b/toolsrc/src/vcpkg/base/strings.cpp
@@ -7,10 +7,11 @@
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);
}
diff --git a/toolsrc/src/vcpkg/commands.hash.cpp b/toolsrc/src/vcpkg/commands.hash.cpp
index cb0a8cc51..9e1b54390 100644
--- a/toolsrc/src/vcpkg/commands.hash.cpp
+++ b/toolsrc/src/vcpkg/commands.hash.cpp
@@ -1,13 +1,114 @@
#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
{
- std::string get_file_hash(const VcpkgPaths& paths, fs::path const& path, std::string const& hash_type)
+ 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, 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);
-
- 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]);
- System::println(hash);
- }
+ Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
+ 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);
}
}