fix(3p/nix/hash): provide a Status-returning constructor

Additionally, add IsValidBase16() to restore the behavior of rejecting invalid base16, which absl's HexStringToBytes does not do.

Change-Id: I777a36f5dc787aa54a2aa316d6728f68da129768
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1484
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Kane York 2020-07-27 16:50:56 -07:00 committed by kanepyork
parent 976a36c2e4
commit 31f9ee58d0
5 changed files with 123 additions and 52 deletions

View file

@ -1,5 +1,7 @@
#pragma once
#include <absl/status/statusor.h>
#include "libutil/serialise.hh"
#include "libutil/types.hh"
@ -36,6 +38,10 @@ struct Hash {
string. */
Hash(const std::string& s, HashType type = htUnknown);
/* Status-returning version of above constructor */
static absl::StatusOr<Hash> deserialize(const std::string& s,
HashType type = htUnknown);
void init();
/* Check whether a hash is set. */
@ -64,6 +70,10 @@ struct Hash {
(e.g. "sha256:"). */
std::string to_string(Base base = Base32, bool includeType = true) const;
/* Returns whether the passed string contains entirely valid base16
characters. */
static bool IsValidBase16(absl::string_view s);
/* Returns whether the passed string contains entirely valid base32
characters. */
static bool IsValidBase32(absl::string_view s);