Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 46 additions & 40 deletions include/tscore/CryptoHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,53 +111,42 @@ union CryptoHash {

extern CryptoHash const CRYPTO_HASH_ZERO;

/** Protocol class for a crypto hash context.

A hash of this type is used for strong hashing, such as for URLs.
*/
class CryptoContextBase
class CryptoContext
{
typedef CryptoContextBase self; ///< Self reference type.
public:
/// Destructor (force virtual)
virtual ~CryptoContextBase() {}
/// Update the hash with @a data of @a length bytes.
virtual bool update(void const *data, int length) = 0;
/// Finalize and extract the @a hash.
virtual bool finalize(CryptoHash &hash) = 0;
/** Protocol class for a crypto hash context.

/// Convenience overload.
bool finalize(CryptoHash *hash);
A hash of this type is used for strong hashing, such as for URLs.
*/
class Hasher
{
using self_type = Hasher; ///< Self reference type.
public:
/// Destructor (force virtual)
virtual ~Hasher() {}
/// Update the hash with @a data of @a length bytes.
virtual bool update(void const *data, int length) = 0;
/// Finalize and extract the @a hash.
virtual bool finalize(CryptoHash &hash) = 0;

/// Convenience overload.
bool finalize(CryptoHash *hash);

protected:
EVP_MD_CTX *_ctx = nullptr;
};

CryptoContext();
/// Update the hash with @a data of @a length bytes.
bool update(void const *data, int length);

/// Convenience - compute final @a hash for @a data.
/// @note This is just as fast as the previous style, as a new context must be initialized
/// every time this is done.
bool hash_immediate(CryptoHash &hash, void const *data, int length);

protected:
EVP_MD_CTX *_ctx = nullptr;
};

inline bool
CryptoContextBase::hash_immediate(CryptoHash &hash, void const *data, int length)
{
return this->update(data, length) && this->finalize(hash);
}

inline bool
CryptoContextBase::finalize(CryptoHash *hash)
{
return this->finalize(*hash);
}

class CryptoContext : public CryptoContextBase
{
public:
CryptoContext();
/// Update the hash with @a data of @a length bytes.
bool update(void const *data, int length) override;
/// Finalize and extract the @a hash.
bool finalize(CryptoHash &hash) override;
bool finalize(CryptoHash &hash);

enum HashType {
UNSPECIFIED,
Expand All @@ -168,23 +157,40 @@ class CryptoContext : public CryptoContextBase
}; ///< What type of hash we really are.
static HashType Setting;

~CryptoContext() { reinterpret_cast<CryptoContextBase *>(_base)->~CryptoContextBase(); }
~CryptoContext();

private:
static size_t constexpr OBJ_SIZE = 256;
char _base[OBJ_SIZE];
};

inline bool
CryptoContext::Hasher::finalize(CryptoHash *hash)
{
return this->finalize(*hash);
}

inline bool
CryptoContext::update(void const *data, int length)
{
return reinterpret_cast<CryptoContextBase *>(_base)->update(data, length);
return reinterpret_cast<Hasher *>(_base)->update(data, length);
}

inline bool
CryptoContext::finalize(CryptoHash &hash)
{
return reinterpret_cast<CryptoContextBase *>(_base)->finalize(hash);
return reinterpret_cast<Hasher *>(_base)->finalize(hash);
}

inline bool
CryptoContext::hash_immediate(CryptoHash &hash, void const *data, int length)
{
return this->update(data, length) && this->finalize(hash);
}

inline CryptoContext::~CryptoContext()
{
std::destroy_at(reinterpret_cast<Hasher *>(_base));
}

ts::BufferWriter &bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec, ats::CryptoHash const &hash);
Expand Down
4 changes: 2 additions & 2 deletions include/tscore/MD5.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <openssl/evp.h>
#endif

class MD5Context : public ats::CryptoContextBase
class MD5Context : public ats::CryptoContext::Hasher
{
public:
MD5Context()
Expand Down Expand Up @@ -90,4 +90,4 @@ class MD5Context : public ats::CryptoContextBase
#endif
};

typedef CryptoHash INK_MD5;
using INK_MD5 = CryptoHash;
2 changes: 1 addition & 1 deletion include/tscore/MMH.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int ink_code_MMH(unsigned char *input, int len, unsigned char *sixteen_byte_hash
cost.

*/
class MMHContext : public ats::CryptoContextBase
class MMHContext : public ats::CryptoContext::Hasher
{
protected:
MMH_CTX _ctx;
Expand Down
2 changes: 1 addition & 1 deletion include/tscore/SHA256.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <openssl/evp.h>
#endif

class SHA256Context : public ats::CryptoContextBase
class SHA256Context : public ats::CryptoContext::Hasher
{
public:
SHA256Context()
Expand Down