1
0
mirror of https://github.com/anope/anope.git synced 2026-07-10 04:23:13 +02:00

Move B64{Decode,Encode} to textproc and redesign their interface.

This commit is contained in:
Sadie Powell
2025-08-08 12:27:28 +01:00
parent e5c5689985
commit 70de2b7de2
9 changed files with 29 additions and 39 deletions
-12
View File
@@ -463,18 +463,6 @@ namespace Anope
extern CoreExport void Unhex(const string &src, string &dest);
extern CoreExport void Unhex(const string &src, char *dest, size_t sz);
/** Base 64 encode a string
* @param src The string to encode
* @param target Where the encoded string is placed
*/
extern CoreExport void B64Encode(const string &src, string &target);
/** Base 64 decode a string
* @param src The base64 encoded string
* @param target The plain text result
*/
extern CoreExport void B64Decode(const string &src, string &target);
/** Encrypts what is in 'src' to 'dest'
* @param src The source string to encrypt
* @param dest The destination where the encrypted string is placed
+1 -3
View File
@@ -228,9 +228,7 @@ public:
if (header.compare(0, 7, "Bearer ", 7) != 0)
return false; // No token provided.
Anope::string rawtoken;
Anope::B64Decode(header.substr(7), rawtoken);
auto rawtoken = Anope::B64Decode(header.substr(7));
for (const auto &token : tokens)
{
if (!CompareToken(token, rawtoken))
+10
View File
@@ -21,6 +21,16 @@
namespace Anope
{
/** Encode a string as base-64.
* @param str The string to encode as base-64 .
*/
extern CoreExport Anope::string B64Encode(const Anope::string &str);
/** Decode a string from base-64.
* @param str The string to decode from base-64.
*/
extern CoreExport Anope::string B64Decode(const Anope::string &str);
/** Calculates the levenshtein distance between two strings.
* @param s1 The first string.
* @param s2 The second string.
+4 -6
View File
@@ -443,10 +443,9 @@ private:
if (pass.compare(0, 18, "$anope$enc_sha256$", 18) == 0)
{
auto sep = pass.find('$', 18);
Anope::string iv, pass;
Anope::B64Decode(pass.substr(18, sep - 18), iv);
Anope::B64Decode(pass.substr(sep + 1), pass);
nc->pass = "sha256:" + Anope::Hex(pass) + ":" + Anope::Hex(iv);
auto iv = Anope::B64Decode(pass.substr(18, sep - 18));
auto pw = Anope::B64Decode(pass.substr(sep + 1));
nc->pass = "sha256:" + Anope::Hex(pw) + ":" + Anope::Hex(iv);
}
else if (pass.compare(0, 9, "$argon2d$", 9) == 0)
@@ -460,8 +459,7 @@ private:
else if (pass.compare(0, 8, "$base64$", 8) == 0)
{
Anope::string rawpass;
Anope::B64Decode(pass.substr(8), rawpass);
auto rawpass = Anope::B64Decode(pass.substr(8));
Anope::Encrypt(rawpass, nc->pass);
}
+1 -3
View File
@@ -35,9 +35,7 @@ public:
if (!hash_method.equals_cs("plain"))
return;
Anope::string b64pass;
Anope::B64Encode(req->GetPassword(), b64pass);
auto enc = "plain:" + b64pass;
auto enc = "plain:" + Anope::B64Encode(req->GetPassword());
if (nc->pass.equals_cs(enc))
{
// If we are NOT the first encryption module we want to re-encrypt
+2 -4
View File
@@ -100,8 +100,7 @@ public:
else if (m.type == "C")
{
// message = [authzid] UTF8NUL authcid UTF8NUL passwd
Anope::string message;
Anope::B64Decode(m.data[0], message);
auto message = Anope::B64Decode(m.data[0]);
size_t zcsep = message.find('\0');
if (zcsep == Anope::string::npos)
@@ -218,8 +217,7 @@ public:
}
else if (m.type == "C")
{
Anope::string decoded;
Anope::B64Decode(m.data[0], decoded);
auto decoded = Anope::B64Decode(m.data[0]);
auto user = sess->GetUserInfo();
if (!decoded.empty())
+1 -2
View File
@@ -1121,8 +1121,7 @@ struct IRCDMessageCapab final
if (challenge.empty() || !sha256)
return Config->Uplinks[Anope::CurrentUplink].password;
Anope::string b64challenge;
Anope::B64Encode(sha256->HMAC(Config->Uplinks[Anope::CurrentUplink].password, challenge), b64challenge);
auto b64challenge = Anope::B64Encode(sha256->HMAC(Config->Uplinks[Anope::CurrentUplink].password, challenge));
challenge.clear();
return "AUTH:" + b64challenge.rtrim('=');
+2 -4
View File
@@ -1233,8 +1233,7 @@ struct IRCDMessageNick final
Anope::string ip;
if (params[9] != "*")
{
Anope::string decoded_ip;
Anope::B64Decode(params[9], decoded_ip);
auto decoded_ip = Anope::B64Decode(params[9]);
sockaddrs ip_addr;
ip_addr.ntop(params[9].length() == 8 ? AF_INET : AF_INET6, decoded_ip.c_str());
@@ -1646,8 +1645,7 @@ struct IRCDMessageUID final
if (ip != "*")
{
Anope::string decoded_ip;
Anope::B64Decode(ip, decoded_ip);
auto decoded_ip = Anope::B64Decode(ip);
sockaddrs ip_addr;
ip_addr.ntop(ip.length() == 8 ? AF_INET : AF_INET6, decoded_ip.c_str());
+8 -5
View File
@@ -11,6 +11,7 @@
#include "services.h"
#include "anope.h"
#include "textproc.h"
static const Anope::string Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char Pad64 = '=';
@@ -78,12 +79,12 @@ static const char Pad64 = '=';
characters followed by one "=" padding character.
*/
void Anope::B64Encode(const Anope::string &src, Anope::string &target)
Anope::string Anope::B64Encode(const Anope::string &src)
{
size_t src_pos = 0, src_len = src.length();
unsigned char input[3] = { '\0', '\0', '\0' };
target.clear();
Anope::string target;
while (src_len - src_pos > 2)
{
@@ -112,6 +113,7 @@ void Anope::B64Encode(const Anope::string &src, Anope::string &target)
target += Base64[((input[1] & 0x0f) << 2) + (input[2] >> 6)];
target += Pad64;
}
return target;
}
/* skips all whitespace anywhere.
@@ -119,9 +121,9 @@ void Anope::B64Encode(const Anope::string &src, Anope::string &target)
src from base - 64 numbers into three 8 bit bytes in the target area.
*/
void Anope::B64Decode(const Anope::string &src, Anope::string &target)
Anope::string Anope::B64Decode(const Anope::string &src)
{
target.clear();
Anope::string target;
unsigned state = 0;
Anope::string::const_iterator ch = src.begin(), end = src.end();
@@ -135,7 +137,7 @@ void Anope::B64Decode(const Anope::string &src, Anope::string &target)
size_t pos = Base64.find(*ch);
if (pos == Anope::string::npos) /* A non-base64 character */
return;
return {};
switch (state)
{
@@ -160,4 +162,5 @@ void Anope::B64Decode(const Anope::string &src, Anope::string &target)
}
if (!target.empty() && !target[target.length() - 1])
target.erase(target.length() - 1);
return target;
}