aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-18 22:09:22 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-06-18 22:11:26 +0000
commit15abb2aa2ba7de06a86e05511f81633616e17d87 (patch)
tree077cb0b6fe627e04aee10c93ed45f402996ebe70 /src/libutil/hash.cc
parentbbbf3602a323538b8da38f1a2c7ce136a20f74c6 (diff)
Revert the `enum struct` change
Not a regular git revert as there have been many merges and things.
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc67
1 files changed, 34 insertions, 33 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 106b47ae3..7a8d091df 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -21,10 +21,10 @@ void Hash::init()
{
if (!type) abort();
switch (*type) {
- case HashType::MD5: hashSize = md5HashSize; break;
- case HashType::SHA1: hashSize = sha1HashSize; break;
- case HashType::SHA256: hashSize = sha256HashSize; break;
- case HashType::SHA512: hashSize = sha512HashSize; break;
+ case htMD5: hashSize = md5HashSize; break;
+ case htSHA1: hashSize = sha1HashSize; break;
+ case htSHA256: hashSize = sha256HashSize; break;
+ case htSHA512: hashSize = sha512HashSize; break;
}
assert(hashSize <= maxHashSize);
memset(hash, 0, maxHashSize);
@@ -101,7 +101,7 @@ static string printHash32(const Hash & hash)
string printHash16or32(const Hash & hash)
{
- return hash.to_string(hash.type == HashType::MD5 ? Base::Base16 : Base::Base32, false);
+ return hash.to_string(hash.type == htMD5 ? Base16 : Base32, false);
}
@@ -115,19 +115,19 @@ HashType assertInitHashType(const Hash & h) {
std::string Hash::to_string(Base base, bool includeType) const
{
std::string s;
- if (base == Base::SRI || includeType) {
+ if (base == SRI || includeType) {
s += printHashType(assertInitHashType(*this));
- s += base == Base::SRI ? '-' : ':';
+ s += base == SRI ? '-' : ':';
}
switch (base) {
- case Base::Base16:
+ case Base16:
s += printHash16(*this);
break;
- case Base::Base32:
+ case Base32:
s += printHash32(*this);
break;
- case Base::Base64:
- case Base::SRI:
+ case Base64:
+ case SRI:
s += base64Encode(std::string((const char *) hash, hashSize));
break;
}
@@ -241,29 +241,29 @@ union Ctx
static void start(HashType ht, Ctx & ctx)
{
- if (ht == HashType::MD5) MD5_Init(&ctx.md5);
- else if (ht == HashType::SHA1) SHA1_Init(&ctx.sha1);
- else if (ht == HashType::SHA256) SHA256_Init(&ctx.sha256);
- else if (ht == HashType::SHA512) SHA512_Init(&ctx.sha512);
+ if (ht == htMD5) MD5_Init(&ctx.md5);
+ else if (ht == htSHA1) SHA1_Init(&ctx.sha1);
+ else if (ht == htSHA256) SHA256_Init(&ctx.sha256);
+ else if (ht == htSHA512) SHA512_Init(&ctx.sha512);
}
static void update(HashType ht, Ctx & ctx,
const unsigned char * bytes, size_t len)
{
- if (ht == HashType::MD5) MD5_Update(&ctx.md5, bytes, len);
- else if (ht == HashType::SHA1) SHA1_Update(&ctx.sha1, bytes, len);
- else if (ht == HashType::SHA256) SHA256_Update(&ctx.sha256, bytes, len);
- else if (ht == HashType::SHA512) SHA512_Update(&ctx.sha512, bytes, len);
+ if (ht == htMD5) MD5_Update(&ctx.md5, bytes, len);
+ else if (ht == htSHA1) SHA1_Update(&ctx.sha1, bytes, len);
+ else if (ht == htSHA256) SHA256_Update(&ctx.sha256, bytes, len);
+ else if (ht == htSHA512) SHA512_Update(&ctx.sha512, bytes, len);
}
static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
{
- if (ht == HashType::MD5) MD5_Final(hash, &ctx.md5);
- else if (ht == HashType::SHA1) SHA1_Final(hash, &ctx.sha1);
- else if (ht == HashType::SHA256) SHA256_Final(hash, &ctx.sha256);
- else if (ht == HashType::SHA512) SHA512_Final(hash, &ctx.sha512);
+ if (ht == htMD5) MD5_Final(hash, &ctx.md5);
+ else if (ht == htSHA1) SHA1_Final(hash, &ctx.sha1);
+ else if (ht == htSHA256) SHA256_Final(hash, &ctx.sha256);
+ else if (ht == htSHA512) SHA512_Final(hash, &ctx.sha512);
}
@@ -344,10 +344,10 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
std::optional<HashType> parseHashTypeOpt(const string & s)
{
- if (s == "md5") return HashType::MD5;
- else if (s == "sha1") return HashType::SHA1;
- else if (s == "sha256") return HashType::SHA256;
- else if (s == "sha512") return HashType::SHA512;
+ if (s == "md5") return htMD5;
+ else if (s == "sha1") return htSHA1;
+ else if (s == "sha256") return htSHA256;
+ else if (s == "sha512") return htSHA512;
else return std::optional<HashType> {};
}
@@ -362,14 +362,15 @@ HashType parseHashType(const string & s)
string printHashType(HashType ht)
{
- string ret;
switch (ht) {
- case HashType::MD5: ret = "md5"; break;
- case HashType::SHA1: ret = "sha1"; break;
- case HashType::SHA256: ret = "sha256"; break;
- case HashType::SHA512: ret = "sha512"; break;
+ case htMD5: return "md5"; break;
+ case htSHA1: return "sha1"; break;
+ case htSHA256: return "sha256"; break;
+ case htSHA512: return "sha512"; break;
}
- return ret;
+ // illegal hash type enum value internally, as opposed to external input
+ // which should be validated with nice error message.
+ abort();
}
}