Skip to content

Commit 095ac66

Browse files
committed
Introduce Hash::parseExplicitFormatUnprefixed
1 parent c6d06ce commit 095ac66

File tree

3 files changed

+87
-8
lines changed

3 files changed

+87
-8
lines changed

src/libutil-tests/hash.cc

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include <regex>
22

33
#include <gtest/gtest.h>
4+
#include <nlohmann/json.hpp>
45

56
#include "nix/util/hash.hh"
7+
#include "nix/util/tests/characterization.hh"
68

79
namespace nix {
810

9-
class BLAKE3HashTest : public virtual ::testing::Test
11+
class HashTest : public CharacterizationTest
1012
{
13+
std::filesystem::path unitTestData = getUnitTestData() / "hash";
14+
1115
public:
1216

1317
/**
@@ -16,8 +20,14 @@ class BLAKE3HashTest : public virtual ::testing::Test
1620
*/
1721
ExperimentalFeatureSettings mockXpSettings;
1822

19-
private:
23+
std::filesystem::path goldenMaster(std::string_view testStem) const override
24+
{
25+
return unitTestData / testStem;
26+
}
27+
};
2028

29+
class BLAKE3HashTest : public HashTest
30+
{
2131
void SetUp() override
2232
{
2333
mockXpSettings.set("experimental-features", "blake3-hashes");
@@ -137,6 +147,46 @@ TEST(hashString, testKnownSHA512Hashes2)
137147
"c7d329eeb6dd26545e96e55b874be909");
138148
}
139149

150+
/* ----------------------------------------------------------------------------
151+
* parsing hashes
152+
* --------------------------------------------------------------------------*/
153+
154+
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_correct)
155+
{
156+
// values taken from: https://tools.ietf.org/html/rfc4634
157+
auto s = "abc";
158+
159+
auto hash = hashString(HashAlgorithm::SHA256, s);
160+
ASSERT_EQ(
161+
hash,
162+
Hash::parseExplicitFormatUnprefixed(
163+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
164+
HashAlgorithm::SHA256,
165+
HashFormat::Base16));
166+
}
167+
168+
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_wrongAlgo)
169+
{
170+
// values taken from: https://tools.ietf.org/html/rfc4634
171+
ASSERT_THROW(
172+
Hash::parseExplicitFormatUnprefixed(
173+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
174+
HashAlgorithm::SHA1,
175+
HashFormat::Base16),
176+
BadHash);
177+
}
178+
179+
TEST(hashParseExplicitFormatUnprefixed, testKnownSHA256Hashes1_wrongBase)
180+
{
181+
// values taken from: https://tools.ietf.org/html/rfc4634
182+
ASSERT_THROW(
183+
Hash::parseExplicitFormatUnprefixed(
184+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
185+
HashAlgorithm::SHA256,
186+
HashFormat::Nix32),
187+
BadHash);
188+
}
189+
140190
/* ----------------------------------------------------------------------------
141191
* parseHashFormat, parseHashFormatOpt, printHashFormat
142192
* --------------------------------------------------------------------------*/

src/libutil/hash.cc

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,37 @@ struct DecodeNamePair
9999

100100
} // namespace
101101

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+
102116
/**
103117
* Given the expected size of the message once decoded it, figure out
104118
* which encoding we are using by looking at the size of the encoded
105119
* message.
106120
*/
107-
static DecodeNamePair baseFromSize(std::string_view rest, HashAlgorithm algo)
121+
static HashFormat baseFromSize(std::string_view rest, HashAlgorithm algo)
108122
{
109123
auto hashSize = regularHashSize(algo);
124+
110125
if (rest.size() == base16::encodedLength(hashSize))
111-
return {base16::decode, "base16"};
126+
return HashFormat::Base16;
112127

113128
if (rest.size() == BaseNix32::encodedLength(hashSize))
114-
return {BaseNix32::decode, "nix32"};
129+
return HashFormat::Nix32;
115130

116131
if (rest.size() == base64::encodedLength(hashSize))
117-
return {base64::decode, "Base64"};
132+
return HashFormat::Base64;
118133

119134
throw BadHash("hash '%s' has wrong length for hash algorithm '%s'", rest, printHashAlgo(algo));
120135
}
@@ -190,7 +205,7 @@ static Hash parseAnyHelper(std::string_view rest, auto resolveAlgo)
190205
} else {
191206
/* Otherwise, decide via the length of the hash (for the
192207
given algorithm) what base encoding it is. */
193-
return baseFromSize(rest, algo);
208+
return baseExplicit(baseFromSize(rest, algo));
194209
}
195210
}();
196211

@@ -225,7 +240,12 @@ Hash Hash::parseAny(std::string_view original, std::optional<HashAlgorithm> optA
225240

226241
Hash Hash::parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
227242
{
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));
229249
}
230250

231251
Hash Hash::random(HashAlgorithm algo)

src/libutil/include/nix/util/hash.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ struct Hash
9090
*/
9191
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo);
9292

93+
/**
94+
* Like `parseNonSRIUnprefixed`, but the hash format has been
95+
* explicitly given.
96+
*
97+
* @param explicitFormat cannot be SRI, but must be one of the
98+
* "bases".
99+
*/
100+
static Hash parseExplicitFormatUnprefixed(std::string_view s, HashAlgorithm algo, HashFormat explicitFormat);
101+
93102
static Hash parseSRI(std::string_view original);
94103

95104
public:

0 commit comments

Comments
 (0)