aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.hh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-06-26 08:46:46 +0200
committerEelco Dolstra <edolstra@gmail.com>2020-06-26 08:46:46 +0200
commitadf2fbbdc2c94644b0d1023d844c7dc0e485a20f (patch)
tree5999d2c223cb24402995747d79a3de6d6aaebb73 /src/libutil/hash.hh
parent09fc06daab280735dd2ec94276f00a9c5bffd9b2 (diff)
parentb7ccf7ae2af3d7eaf3696358ecbbce5a2bcfe652 (diff)
Merge remote-tracking branch 'origin/master' into flakes
Diffstat (limited to 'src/libutil/hash.hh')
-rw-r--r--src/libutil/hash.hh19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index c5724a321..ad6093fca 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 = 42, htSHA1, htSHA256, htSHA512 };
const int md5HashSize = 16;
@@ -31,7 +31,7 @@ struct Hash
unsigned int hashSize = 0;
unsigned char hash[maxHashSize] = {};
- HashType type = htUnknown;
+ std::optional<HashType> type = {};
/* Create an unset hash object. */
Hash() { };
@@ -42,14 +42,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;
@@ -97,7 +101,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);
@@ -121,6 +125,9 @@ 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);