From f919bb0748fe1ba09114f22841efb5af7c5bb37d Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 10 Mar 2024 16:14:22 +0000 Subject: Add self-tests to the encryption providers. --- include/modules/encryption.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'include/modules') diff --git a/include/modules/encryption.h b/include/modules/encryption.h index b8ec8ff58..c475f4fbd 100644 --- a/include/modules/encryption.h +++ b/include/modules/encryption.h @@ -66,7 +66,18 @@ namespace Encryption /** Checks whether a plain text value matches a hash created by this provider. */ virtual bool Compare(const Anope::string &hash, const Anope::string &plain) { - return hash.equals_cs(plain); + return !hash.empty() && hash.equals_cs(ToPrintable(Encrypt(plain))); + } + + /** Called on initialising a encryption provider to check it works properly. */ + void Check(const Anope::map &checks) + { + for (const auto &[hash, plain] : checks) + { + if (!Compare(hash, plain)) + throw ModuleException("BUG: unable to generate " + this->name + " hashes safely! Please report this!"); + } + Log(LOG_DEBUG) << "The " << this->name << " encryption provider appears to be working correctly."; } /** Creates a new encryption context. */ @@ -102,6 +113,12 @@ namespace Encryption return Encrypt(hmac1); } + + /** Converts a hash to its printable form. */ + virtual Anope::string ToPrintable(const Anope::string &hash) + { + return Anope::Hex(hash); + } }; /** Helper template for creating simple providers of encryption contexts. */ -- cgit