diff options
| author | Reepca Russelstein <reepca@russelstein.xyz> | 2026-06-21 07:14:01 -0500 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2026-06-24 15:20:16 +0200 |
| commit | 694785dc13840f36e9dda2fd413f0d6ac176f153 (patch) | |
| tree | af74dc2fbdd3c3f12ef0ba9c4bc0dc7b64e6c202 /nix | |
| parent | 64a0824955aa40ea8e59bd4328ecaaf8caaa2acc (diff) | |
daemon: libutil: add isHash32.
* nix/libutil/hash.hh (isHash32): new function.
* nix/libutil/hash.hh (isHash32): provide implementation.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'nix')
| -rw-r--r-- | nix/libutil/hash.cc | 9 | ||||
| -rw-r--r-- | nix/libutil/hash.hh | 7 |
2 files changed, 15 insertions, 1 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index 06753d19619..7b363b4f9e7 100644 --- a/nix/libutil/hash.cc +++ b/nix/libutil/hash.cc | |||
| @@ -194,6 +194,15 @@ bool isHash(std::string_view s) | |||
| 194 | return true; | 194 | return true; |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | |||
| 198 | bool isHash32(std::string_view s) | ||
| 199 | { | ||
| 200 | if (s.length() != 32) return false; | ||
| 201 | for (unsigned char c : s) | ||
| 202 | if (base32Values[c] == -1) return false; | ||
| 203 | return true; | ||
| 204 | } | ||
| 205 | |||
| 197 | /* The "hash context". */ | 206 | /* The "hash context". */ |
| 198 | struct Ctx | 207 | struct Ctx |
| 199 | { | 208 | { |
diff --git a/nix/libutil/hash.hh b/nix/libutil/hash.hh index fabe50c11c6..40d6b8725dd 100644 --- a/nix/libutil/hash.hh +++ b/nix/libutil/hash.hh | |||
| @@ -72,9 +72,14 @@ Hash parseHash32(HashType ht, std::string_view s); | |||
| 72 | /* Parse a base-16 or base-32 representation of a hash code. */ | 72 | /* Parse a base-16 or base-32 representation of a hash code. */ |
| 73 | Hash parseHash16or32(HashType ht, std::string_view s); | 73 | Hash parseHash16or32(HashType ht, std::string_view s); |
| 74 | 74 | ||
| 75 | /* Verify that the given string is a valid hash code. */ | 75 | /* Verify that the given string is a valid base16 hash code with 32 |
| 76 | characters. */ | ||
| 76 | bool isHash(std::string_view s); | 77 | bool isHash(std::string_view s); |
| 77 | 78 | ||
| 79 | /* Verify that the given string is a valid base32 hash code with 32 | ||
| 80 | characters. */ | ||
| 81 | bool isHash32(std::string_view s); | ||
| 82 | |||
| 78 | /* Compute the hash of the given string. */ | 83 | /* Compute the hash of the given string. */ |
| 79 | Hash hashString(HashType ht, const string & s); | 84 | Hash hashString(HashType ht, const string & s); |
| 80 | 85 | ||
