aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.hh
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-21 16:43:17 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-21 16:43:17 +0000
commitfdeabf71601e4ec9ff797e0283d06f9b5b9d8aa5 (patch)
tree51cfb1a7b05b0641fc7c3bb755210f6cd053c395 /src/libutil/hash.hh
parent02928f76fdf8ab991da404d4216e97d54af19976 (diff)
parent984e521392b3f41f7cdab203e5c00f3e00e27a28 (diff)
Merge remote-tracking branch 'upstream/master' into multi-output-hashDerivationModulo
Diffstat (limited to 'src/libutil/hash.hh')
-rw-r--r--src/libutil/hash.hh18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 180fb7633..0d9916508 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -10,7 +10,7 @@ namespace nix {
MakeError(BadHash, Error);
-enum HashType : char { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 };
+enum HashType : char { htMD5, htSHA1, htSHA256, htSHA512 };
const int md5HashSize = 16;
@@ -29,7 +29,7 @@ struct Hash
unsigned int hashSize = 0;
unsigned char hash[maxHashSize] = {};
- HashType type = htUnknown;
+ std::optional<HashType> type = {};
/* Create an unset hash object. */
Hash() { };
@@ -40,14 +40,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 htUnknown, then the hash type must be specified in the
+ is not present, then the hash type must be specified in the
string. */
- Hash(std::string_view s, HashType type = htUnknown);
+ Hash(std::string_view s, std::optional<HashType> type);
+ // type must be provided
+ Hash(std::string_view s, HashType type);
+ // hash type must be part of string
+ Hash(std::string_view s);
void init();
/* Check whether a hash is set. */
- operator bool () const { return type != htUnknown; }
+ operator bool () const { return (bool) type; }
/* Check whether two hash are equal. */
bool operator == (const Hash & h2) const;
@@ -95,7 +99,7 @@ struct Hash
};
/* Helper that defaults empty hashes to the 0 hash. */
-Hash newHashAllowEmpty(std::string hashStr, HashType ht);
+Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht);
/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
string printHash16or32(const Hash & hash);
@@ -118,6 +122,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);