aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-18 21:58:27 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-18 22:11:19 +0000
commitbbbf3602a323538b8da38f1a2c7ce136a20f74c6 (patch)
treee783d838d8a75d854e0b72eea03784c5909040bc /src/libutil/hash.cc
parent406dbb7fce32f7d80b02f560d91c956698b58d6e (diff)
parent40526fbea56a8006eb7f1758d461a5acbe9a1694 (diff)
Merge branch 'enum-class' into no-hash-type-unknown
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 6b9effdd2..106b47ae3 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -134,10 +134,10 @@ std::string Hash::to_string(Base base, bool includeType) const
return s;
}
-Hash::Hash(const std::string & s, HashType type) : Hash(s, std::optional { type }) { }
-Hash::Hash(const std::string & s) : Hash(s, std::optional<HashType>{}) { }
+Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
+Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
-Hash::Hash(const std::string & s, std::optional<HashType> type)
+Hash::Hash(std::string_view s, std::optional<HashType> type)
: type(type)
{
size_t pos = 0;
@@ -206,7 +206,7 @@ Hash::Hash(const std::string & s, std::optional<HashType> type)
}
else if (isSRI || size == base64Len()) {
- auto d = base64Decode(std::string(s, pos));
+ auto d = base64Decode(s.substr(pos));
if (d.size() != hashSize)
throw BadHash("invalid %s hash '%s'", isSRI ? "SRI" : "base-64", s);
assert(hashSize);
@@ -217,6 +217,18 @@ Hash::Hash(const std::string & s, std::optional<HashType> type)
throw BadHash("hash '%s' has wrong length for hash type '%s'", s, printHashType(*type));
}
+Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht)
+{
+ if (hashStr.empty()) {
+ if (!ht)
+ throw BadHash("empty hash requires explicit hash type");
+ Hash h(*ht);
+ warn("found empty hash, assuming '%s'", h.to_string(Base::SRI, true));
+ return h;
+ } else
+ return Hash(hashStr, ht);
+}
+
union Ctx
{