summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDaniel Vassdal <shutter@canternet.org>2014-07-02 20:55:33 +0200
committerAttila Molnar <attilamolnar@hush.com>2014-08-04 12:38:12 +0200
commit5c7db14a557d7f980dfaa8c10d40c1ccc554b35d (patch)
treed9d0b3161928f1ba9cbf0ced5f894c2d1c6a1886 /include
parent0c0a7b6404c5de51241cdaa3eb159d014ef51024 (diff)
Hashing: Redo API
* Don't assume the printable output of hashes is hex * Add virtual Compare() function, usable for KDFs like BCrypt Some changes and bugfixes are by @attilamolnar, original PR #767
Diffstat (limited to 'include')
-rw-r--r--include/modules/hash.h34
1 files changed, 24 insertions, 10 deletions
diff --git a/include/modules/hash.h b/include/modules/hash.h
index da04c45ba..7d46ee74a 100644
--- a/include/modules/hash.h
+++ b/include/modules/hash.h
@@ -26,24 +26,33 @@ class HashProvider : public DataProvider
public:
const unsigned int out_size;
const unsigned int block_size;
- HashProvider(Module* mod, const std::string& Name, int osiz, int bsiz)
- : DataProvider(mod, Name), out_size(osiz), block_size(bsiz) {}
- virtual std::string sum(const std::string& data) = 0;
- inline std::string hexsum(const std::string& data)
+ HashProvider(Module* mod, const std::string& Name, unsigned int osiz = 0, unsigned int bsiz = 0)
+ : DataProvider(mod, "hash/" + Name), out_size(osiz), block_size(bsiz)
{
- return BinToHex(sum(data));
}
- inline std::string b64sum(const std::string& data)
+ virtual std::string GenerateRaw(const std::string& data) = 0;
+
+ virtual std::string ToPrintable(const std::string& raw)
+ {
+ return BinToHex(raw);
+ }
+
+ virtual bool Compare(const std::string& input, const std::string& hash)
+ {
+ return InspIRCd::TimingSafeCompare(Generate(input), hash);
+ }
+
+ std::string Generate(const std::string& data)
{
- return BinToBase64(sum(data), NULL, 0);
+ return ToPrintable(GenerateRaw(data));
}
/** HMAC algorithm, RFC 2104 */
std::string hmac(const std::string& key, const std::string& msg)
{
std::string hmac1, hmac2;
- std::string kbuf = key.length() > block_size ? sum(key) : key;
+ std::string kbuf = key.length() > block_size ? GenerateRaw(key) : key;
kbuf.resize(block_size);
for (size_t n = 0; n < block_size; n++)
@@ -52,7 +61,12 @@ class HashProvider : public DataProvider
hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
}
hmac2.append(msg);
- hmac1.append(sum(hmac2));
- return sum(hmac1);
+ hmac1.append(GenerateRaw(hmac2));
+ return GenerateRaw(hmac1);
+ }
+
+ bool IsKDF() const
+ {
+ return (!block_size);
}
};