Skip to content

Commit

Permalink
[vcpkg hash] Also try sha512sum
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaratarakis committed Oct 30, 2018
1 parent b084ab0 commit 7136958
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions toolsrc/src/vcpkg/base/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,25 @@ namespace vcpkg::Hash
return hash_type.substr(3, hash_type.length() - 3);
}

static std::string run_shasum_and_post_process(const std::string& cmd_line)
static Optional<std::string> run_shasum(const std::string& cmd_line)
{
const auto ec_data = System::cmd_execute_and_capture_output(cmd_line);
Checks::check_exit(VCPKG_LINE_INFO,
ec_data.exit_code == 0,
"Failed to run:\n"
" %s",
cmd_line);
if (ec_data.exit_code == 0)
{
return ec_data.output;
}

std::vector<std::string> split = Strings::split(ec_data.output, " ");
return nullopt;
}

static std::string parse_shasum_output(const std::string& shasum_output)
{
std::vector<std::string> split = Strings::split(shasum_output, " ");
Checks::check_exit(VCPKG_LINE_INFO,
split.size() == 3,
"Expected output of the form [hash filename\n] (3 tokens), but got\n"
"[%s] (%s tokens)",
ec_data.output,
shasum_output,
std::to_string(split.size()));

return split[0];
Expand All @@ -198,17 +202,46 @@ namespace vcpkg::Hash
{
const std::string digest_size = get_digest_size(hash_type);
Checks::check_exit(VCPKG_LINE_INFO, fs.exists(path), "File %s does not exist", path.u8string());

if (digest_size == "512")
{
const std::string cmd_line = Strings::format(R"(sha512sum "%s")", path.u8string());
const auto shasum_output = run_shasum(cmd_line);
if (auto s = shasum_output.get())
{
return parse_shasum_output(*s);
}
}

const std::string cmd_line = Strings::format(R"(shasum -a %s "%s")", digest_size, path.u8string());
return run_shasum_and_post_process(cmd_line);
const auto shasum_output = run_shasum(cmd_line);
if (auto s = shasum_output.get())
{
return parse_shasum_output(*s);
}
}

std::string get_string_hash(const std::string& s, const std::string& hash_type)
{
const std::string digest_size = get_digest_size(hash_type);
verify_has_only_allowed_chars(s);

if (digest_size == "512")
{
const std::string cmd_line = Strings::format(R"(echo -n "%s" | sha512sum)", s);
const auto shasum_output = run_shasum(cmd_line);
if (auto s = shasum_output.get())
{
return parse_shasum_output(*s);
}
}

const std::string cmd_line = Strings::format(R"(echo -n "%s" | shasum -a %s)", s, digest_size);
return run_shasum_and_post_process(cmd_line);
const auto shasum_output = run_shasum(cmd_line);
if (auto s = shasum_output.get())
{
return parse_shasum_output(*s);
}
}
#endif
}

0 comments on commit 7136958

Please sign in to comment.