-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sha256/Hash.cpp: Implement hash type for SHA256.
- Loading branch information
Showing
4 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include"Sha256/Hash.hpp" | ||
#include"Util/Str.hpp" | ||
#include<sodium/utils.h> | ||
#include<stdexcept> | ||
|
||
namespace { | ||
|
||
std::uint8_t const zero[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
|
||
} | ||
|
||
namespace Sha256 { | ||
|
||
bool Hash::valid_string(std::string const& s) { | ||
return s.size() == 64 && Util::Str::ishex(s); | ||
} | ||
Hash::Hash(std::string const& s) { | ||
auto bytes = Util::Str::hexread(s); | ||
if (bytes.size() != 32) | ||
throw std::invalid_argument("Hashes must be 32 bytes."); | ||
pimpl = std::make_shared<Impl>(); | ||
for (auto i = std::size_t(0); i < 32; ++i) | ||
pimpl->d[i] = bytes[i]; | ||
} | ||
|
||
Hash::operator std::string() const { | ||
if (!pimpl) | ||
return "0000000000000000000000000000000000000000000000000000000000000000"; | ||
|
||
return Util::Str::hexdump(pimpl->d, 32); | ||
} | ||
Hash::operator bool() const { | ||
if (!pimpl) | ||
return false; | ||
|
||
return 0 != sodium_memcmp(zero, pimpl->d, 32); | ||
} | ||
bool Hash::operator==(Hash const& i) const { | ||
auto a = pimpl ? pimpl->d : zero; | ||
auto b = i.pimpl ? i.pimpl->d : zero; | ||
return 0 == sodium_memcmp(a, b, 32); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef SHA256_HASH_HPP | ||
#define SHA256_HASH_HPP | ||
|
||
#include<cstdint> | ||
#include<iostream> | ||
#include<memory> | ||
#include<string> | ||
#include<utility> | ||
|
||
namespace Sha256 { class Hasher; } | ||
|
||
namespace Sha256 { | ||
|
||
class Hash { | ||
private: | ||
struct Impl { | ||
std::uint8_t d[32]; | ||
}; | ||
std::shared_ptr<Impl> pimpl; | ||
|
||
explicit | ||
Hash(std::uint8_t d[32]) { | ||
pimpl = std::make_shared<Impl>(); | ||
for (auto i = std::size_t(0); i < 32; ++i) | ||
pimpl->d[i] = d[i]; | ||
} | ||
|
||
friend class Sha256::Hasher; | ||
friend class std::hash<Hash>; | ||
|
||
public: | ||
Hash() =default; | ||
Hash(Hash const&) =default; | ||
Hash(Hash&&) =default; | ||
Hash& operator=(Hash const&) =default; | ||
Hash& operator=(Hash&&) =default; | ||
~Hash() =default; | ||
|
||
static | ||
bool valid_string(std::string const&); | ||
explicit | ||
Hash(std::string const&); | ||
|
||
explicit | ||
operator std::string() const; | ||
|
||
explicit | ||
operator bool() const; | ||
bool operator!() const { | ||
return !bool(*this); | ||
} | ||
|
||
bool operator==(Hash const&) const; | ||
bool operator!=(Hash const& i) const { | ||
return !(*this == i); | ||
} | ||
}; | ||
|
||
inline | ||
std::ostream& operator<<(std::ostream& os, Hash const& i) { | ||
return os << std::string(i); | ||
} | ||
inline | ||
std::istream& operator>>(std::istream& is, Hash& i) { | ||
auto s = std::string(); | ||
is >> s; | ||
i = Hash(s); | ||
return is; | ||
} | ||
|
||
} | ||
|
||
/* For use with std::unordered_map. */ | ||
namespace std { | ||
template<> | ||
struct hash<::Sha256::Hash> { | ||
std::size_t operator()(::Sha256::Hash const& i) const { | ||
if (!i.pimpl) | ||
return 0; | ||
/* Already a hash, so just read it as-is. */ | ||
return *reinterpret_cast<std::size_t*>(i.pimpl->d); | ||
} | ||
}; | ||
} | ||
|
||
#endif /* !defined(SHA256_HASH_HPP) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#undef NDEBUG | ||
#include"Sha256/Hash.hpp" | ||
#include<assert.h> | ||
#include<unordered_set> | ||
|
||
int main() { | ||
auto a = Sha256::Hash(); | ||
auto b = Sha256::Hash(); | ||
assert(a == b); | ||
b = Sha256::Hash("0123456789012345678901234567890123456789012345678901234567890123"); | ||
assert(a != b); | ||
assert(Sha256::Hash::valid_string("0123456789012345678901234567890123456789012345678901234567890123")); | ||
|
||
a = Sha256::Hash("3210987654321098765432109876543210987654321098765432109876543210"); | ||
assert(a != b); | ||
|
||
auto bag = std::unordered_set<Sha256::Hash>(); | ||
bag.insert(Sha256::Hash("0123456789012345678901234567890123456789012345678901234567890123")); | ||
assert(bag.find(b) != bag.end()); | ||
bag.insert(b); | ||
assert(bag.size() == 1); | ||
|
||
assert(std::string(b) == "0123456789012345678901234567890123456789012345678901234567890123"); | ||
|
||
assert(a); | ||
assert(b); | ||
a = Sha256::Hash(); | ||
assert(!a); | ||
b = Sha256::Hash("0000000000000000000000000000000000000000000000000000000000000000"); | ||
assert(!b); | ||
|
||
return 0; | ||
} |