@@ -99,22 +99,37 @@ struct DecodeNamePair
99
99
100
100
} // namespace
101
101
102
+ static DecodeNamePair baseExplicit (HashFormat format)
103
+ {
104
+ switch (format) {
105
+ case HashFormat::Base16:
106
+ return {base16::decode, " base16" };
107
+ case HashFormat::Nix32:
108
+ return {BaseNix32::decode, " nix32" };
109
+ case HashFormat::Base64:
110
+ return {base64::decode, " Base64" };
111
+ case HashFormat::SRI:
112
+ assert (false );
113
+ }
114
+ }
115
+
102
116
/* *
103
117
* Given the expected size of the message once decoded it, figure out
104
118
* which encoding we are using by looking at the size of the encoded
105
119
* message.
106
120
*/
107
- static DecodeNamePair baseFromSize (std::string_view rest, HashAlgorithm algo)
121
+ static HashFormat baseFromSize (std::string_view rest, HashAlgorithm algo)
108
122
{
109
123
auto hashSize = regularHashSize (algo);
124
+
110
125
if (rest.size () == base16::encodedLength (hashSize))
111
- return {base16::decode, " base16 " } ;
126
+ return HashFormat::Base16 ;
112
127
113
128
if (rest.size () == BaseNix32::encodedLength (hashSize))
114
- return {BaseNix32::decode, " nix32 " } ;
129
+ return HashFormat::Nix32 ;
115
130
116
131
if (rest.size () == base64::encodedLength (hashSize))
117
- return {base64::decode, " Base64" } ;
132
+ return HashFormat:: Base64;
118
133
119
134
throw BadHash (" hash '%s' has wrong length for hash algorithm '%s'" , rest, printHashAlgo (algo));
120
135
}
@@ -190,7 +205,7 @@ static Hash parseAnyHelper(std::string_view rest, auto resolveAlgo)
190
205
} else {
191
206
/* Otherwise, decide via the length of the hash (for the
192
207
given algorithm) what base encoding it is. */
193
- return baseFromSize (rest, algo);
208
+ return baseExplicit ( baseFromSize (rest, algo) );
194
209
}
195
210
}();
196
211
@@ -225,7 +240,12 @@ Hash Hash::parseAny(std::string_view original, std::optional<HashAlgorithm> optA
225
240
226
241
Hash Hash::parseNonSRIUnprefixed (std::string_view s, HashAlgorithm algo)
227
242
{
228
- return parseLowLevel (s, algo, baseFromSize (s, algo));
243
+ return parseExplicitFormatUnprefixed (s, algo, baseFromSize (s, algo));
244
+ }
245
+
246
+ Hash Hash::parseExplicitFormatUnprefixed (std::string_view s, HashAlgorithm algo, HashFormat format)
247
+ {
248
+ return parseLowLevel (s, algo, baseExplicit (format));
229
249
}
230
250
231
251
Hash Hash::random (HashAlgorithm algo)
0 commit comments