Make hashLength32() a method of Hash

This commit is contained in:
Eelco Dolstra 2016-01-27 17:18:20 +01:00
parent 5b8c09c124
commit d45ad8fcf5
2 changed files with 15 additions and 12 deletions

View file

@ -96,19 +96,13 @@ Hash parseHash(HashType ht, const string & s)
}
unsigned int hashLength32(const Hash & hash)
{
return (hash.hashSize * 8 - 1) / 5 + 1;
}
// omitted: E O U T
const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
string printHash32(const Hash & hash)
{
unsigned int len = hashLength32(hash);
size_t len = hash.base32Len();
string s;
s.reserve(len);
@ -136,7 +130,7 @@ string printHash16or32(const Hash & hash)
Hash parseHash32(HashType ht, const string & s)
{
Hash hash(ht);
unsigned int len = hashLength32(ht);
size_t len = hash.base32Len();
assert(s.size() == len);
for (unsigned int n = 0; n < len; ++n) {
@ -163,7 +157,7 @@ Hash parseHash16or32(HashType ht, const string & s)
if (s.size() == hash.hashSize * 2)
/* hexadecimal representation */
hash = parseHash(ht, s);
else if (s.size() == hashLength32(hash))
else if (s.size() == hash.base32Len())
/* base-32 representation */
hash = parseHash32(ht, s);
else

View file

@ -40,6 +40,18 @@ struct Hash
/* For sorting. */
bool operator < (const Hash & h) const;
/* Returns the length of a base-16 representation of this hash. */
size_t base16Len() const
{
return hashSize * 2;
}
/* Returns the length of a base-32 representation of this hash. */
size_t base32Len() const
{
return (hashSize * 8 - 1) / 5 + 1;
}
};
@ -49,9 +61,6 @@ string printHash(const Hash & hash);
/* Parse a hexadecimal representation of a hash code. */
Hash parseHash(HashType ht, const string & s);
/* Returns the length of a base-32 hash representation. */
unsigned int hashLength32(const Hash & hash);
/* Convert a hash to a base-32 representation. */
string printHash32(const Hash & hash);