aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.hh
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-02 15:52:13 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-02 15:52:13 +0000
commit450dcf2c1b60a36f5ffeab2411805287d122bcdd (patch)
treed1fe49020e97198147483f42454da46f36e42094 /src/libutil/hash.hh
parent6dd471ebf6b9a4996405398093ccb371b8abdf2f (diff)
Remove `HashType::Unknown`
Instead, `Hash` uses `std::optional<HashType>`. In the future, we may also make `Hash` itself require a known hash type, encoraging people to use `std::optional<Hash>` instead.
Diffstat (limited to 'src/libutil/hash.hh')
-rw-r--r--src/libutil/hash.hh15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 0fe6e7677..41322be67 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -11,7 +11,6 @@ MakeError(BadHash, Error);
enum struct HashType : char {
- Unknown,
MD5,
SHA1,
SHA256,
@@ -40,7 +39,7 @@ struct Hash
unsigned int hashSize = 0;
unsigned char hash[maxHashSize] = {};
- HashType type = HashType::Unknown;
+ std::optional<HashType> type = {};
/* Create an unset hash object. */
Hash() { };
@@ -51,14 +50,18 @@ struct Hash
/* Initialize the hash from a string representation, in the format
"[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a
Subresource Integrity hash expression). If the 'type' argument
- is HashType::Unknown, then the hash type must be specified in the
+ is not present, then the hash type must be specified in the
string. */
- Hash(const std::string & s, HashType type = HashType::Unknown);
+ Hash(const std::string & s, std::optional<HashType> type);
+ // type must be provided
+ Hash(const std::string & s, HashType type);
+ // hash type must be part of string
+ Hash(const std::string & s);
void init();
/* Check whether a hash is set. */
- operator bool () const { return type != HashType::Unknown; }
+ operator bool () const { return (bool) type; }
/* Check whether two hash are equal. */
bool operator == (const Hash & h2) const;
@@ -127,6 +130,8 @@ Hash compressHash(const Hash & hash, unsigned int newSize);
/* Parse a string representing a hash type. */
HashType parseHashType(const string & s);
+/* Will return nothing on parse error */
+std::optional<HashType> parseHashTypeOpt(const string & s);
/* And the reverse. */
string printHashType(HashType ht);