summaryrefslogtreecommitdiff
path: root/include/modules/encryption.h
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2024-03-10 16:14:22 +0000
committerSadie Powell <sadie@witchery.services>2024-03-10 20:20:24 +0000
commitf919bb0748fe1ba09114f22841efb5af7c5bb37d (patch)
tree109cf55b73ad6c11d47233429d0c98723fea165a /include/modules/encryption.h
parent3b85a8071f4d7238088834b4924af79b57cb36d1 (diff)
Add self-tests to the encryption providers.
Diffstat (limited to 'include/modules/encryption.h')
-rw-r--r--include/modules/encryption.h19
1 files changed, 18 insertions, 1 deletions
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<Anope::string> &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. */