diff options
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r-- | src/libutil/hash.cc | 20 |
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 { |