mirror of
https://github.com/znc/znc.git
synced 2026-07-05 09:21:31 +02:00
Add clang-format configuration.
For now, it uses tabs like before, to make the diff easier to read/check. One of following commits will switch it to spaces.
This commit is contained in:
+141
-116
@@ -46,33 +46,34 @@ CUtils::~CUtils() {}
|
||||
#ifdef HAVE_LIBSSL
|
||||
// Generated by "openssl dhparam 2048"
|
||||
constexpr const char* szDefaultDH2048 =
|
||||
"-----BEGIN DH PARAMETERS-----\n"
|
||||
"MIIBCAKCAQEAtS/K3TMY8IHzcCATQSjUF3rDidjDDQmT+mLxyxRORmzMPjFIFkKH\n"
|
||||
"MOmxZvyCBArdaoCCEBBOzrldl/bBLn5TOeZb+MW7mpBLANTuQSOu97DDM7EzbnqC\n"
|
||||
"b6z3QgixZ2+UqxdmQAu4nBPLFwym6W/XPFEHpz6iHISSvjzzo4cfI0xwWTcoAvFQ\n"
|
||||
"r/ZU5BXSXp7XuDxSyyAqaaKUxquElf+x56QWrpNJypjzPpslg5ViAKwWQS0TnCrU\n"
|
||||
"sVuhFtbNlZjqW1tMSBxiWFltS1HoEaaI79MEpf1Ps25OrQl8xqqCGKkZcHlNo4oF\n"
|
||||
"cvUyzAEcCQYHmiYjp2hoZbSa8b690TQaAwIBAg==\n"
|
||||
"-----END DH PARAMETERS-----\n";
|
||||
"-----BEGIN DH PARAMETERS-----\n"
|
||||
"MIIBCAKCAQEAtS/K3TMY8IHzcCATQSjUF3rDidjDDQmT+mLxyxRORmzMPjFIFkKH\n"
|
||||
"MOmxZvyCBArdaoCCEBBOzrldl/bBLn5TOeZb+MW7mpBLANTuQSOu97DDM7EzbnqC\n"
|
||||
"b6z3QgixZ2+UqxdmQAu4nBPLFwym6W/XPFEHpz6iHISSvjzzo4cfI0xwWTcoAvFQ\n"
|
||||
"r/ZU5BXSXp7XuDxSyyAqaaKUxquElf+x56QWrpNJypjzPpslg5ViAKwWQS0TnCrU\n"
|
||||
"sVuhFtbNlZjqW1tMSBxiWFltS1HoEaaI79MEpf1Ps25OrQl8xqqCGKkZcHlNo4oF\n"
|
||||
"cvUyzAEcCQYHmiYjp2hoZbSa8b690TQaAwIBAg==\n"
|
||||
"-----END DH PARAMETERS-----\n";
|
||||
|
||||
void CUtils::GenerateCert(FILE *pOut, const CString& sHost) {
|
||||
EVP_PKEY *pKey = nullptr;
|
||||
X509 *pCert = nullptr;
|
||||
X509_NAME *pName = nullptr;
|
||||
void CUtils::GenerateCert(FILE* pOut, const CString& sHost) {
|
||||
EVP_PKEY* pKey = nullptr;
|
||||
X509* pCert = nullptr;
|
||||
X509_NAME* pName = nullptr;
|
||||
const int days = 365;
|
||||
const int years = 10;
|
||||
|
||||
unsigned int uSeed = (unsigned int)time(nullptr);
|
||||
int serial = (rand_r(&uSeed) % 9999);
|
||||
|
||||
RSA *pRSA = RSA_generate_key(2048, 0x10001, nullptr, nullptr);
|
||||
RSA* pRSA = RSA_generate_key(2048, 0x10001, nullptr, nullptr);
|
||||
if ((pKey = EVP_PKEY_new())) {
|
||||
if (!EVP_PKEY_assign_RSA(pKey, pRSA)) {
|
||||
EVP_PKEY_free(pKey);
|
||||
return;
|
||||
}
|
||||
|
||||
PEM_write_RSAPrivateKey(pOut, pRSA, nullptr, nullptr, 0, nullptr, nullptr);
|
||||
PEM_write_RSAPrivateKey(pOut, pRSA, nullptr, nullptr, 0, nullptr,
|
||||
nullptr);
|
||||
|
||||
if (!(pCert = X509_new())) {
|
||||
EVP_PKEY_free(pKey);
|
||||
@@ -82,13 +83,14 @@ void CUtils::GenerateCert(FILE *pOut, const CString& sHost) {
|
||||
X509_set_version(pCert, 2);
|
||||
ASN1_INTEGER_set(X509_get_serialNumber(pCert), serial);
|
||||
X509_gmtime_adj(X509_get_notBefore(pCert), 0);
|
||||
X509_gmtime_adj(X509_get_notAfter(pCert), (long)60*60*24*days*years);
|
||||
X509_gmtime_adj(X509_get_notAfter(pCert),
|
||||
(long)60 * 60 * 24 * days * years);
|
||||
X509_set_pubkey(pCert, pKey);
|
||||
|
||||
pName = X509_get_subject_name(pCert);
|
||||
|
||||
const char *pLogName = getenv("LOGNAME");
|
||||
const char *pHostName = nullptr;
|
||||
const char* pLogName = getenv("LOGNAME");
|
||||
const char* pHostName = nullptr;
|
||||
|
||||
if (!sHost.empty()) {
|
||||
pHostName = sHost.c_str();
|
||||
@@ -110,9 +112,13 @@ void CUtils::GenerateCert(FILE *pOut, const CString& sHost) {
|
||||
sEmailAddr += "@";
|
||||
sEmailAddr += pHostName;
|
||||
|
||||
X509_NAME_add_entry_by_txt(pName, "OU", MBSTRING_ASC, (unsigned char *)pLogName, -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(pName, "CN", MBSTRING_ASC, (unsigned char *)pHostName, -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(pName, "emailAddress", MBSTRING_ASC, (unsigned char *)sEmailAddr.c_str(), -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(pName, "OU", MBSTRING_ASC,
|
||||
(unsigned char*)pLogName, -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(pName, "CN", MBSTRING_ASC,
|
||||
(unsigned char*)pHostName, -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(pName, "emailAddress", MBSTRING_ASC,
|
||||
(unsigned char*)sEmailAddr.c_str(), -1, -1,
|
||||
0);
|
||||
|
||||
X509_set_subject_name(pCert, pName);
|
||||
X509_set_issuer_name(pCert, pName);
|
||||
@@ -134,14 +140,14 @@ void CUtils::GenerateCert(FILE *pOut, const CString& sHost) {
|
||||
|
||||
CString CUtils::GetIP(unsigned long addr) {
|
||||
char szBuf[16];
|
||||
memset((char*) szBuf, 0, 16);
|
||||
memset((char*)szBuf, 0, 16);
|
||||
|
||||
if (addr >= (1 << 24)) {
|
||||
unsigned long ip[4];
|
||||
ip[0] = addr >> 24 & 255;
|
||||
ip[1] = addr >> 16 & 255;
|
||||
ip[2] = addr >> 8 & 255;
|
||||
ip[3] = addr & 255;
|
||||
ip[2] = addr >> 8 & 255;
|
||||
ip[3] = addr & 255;
|
||||
sprintf(szBuf, "%lu.%lu.%lu.%lu", ip[0], ip[1], ip[2], ip[3]);
|
||||
}
|
||||
|
||||
@@ -153,13 +159,12 @@ unsigned long CUtils::GetLongIP(const CString& sIP) {
|
||||
char ip[4][4];
|
||||
unsigned int i;
|
||||
|
||||
i = sscanf(sIP.c_str(), "%3[0-9].%3[0-9].%3[0-9].%3[0-9]",
|
||||
ip[0], ip[1], ip[2], ip[3]);
|
||||
if (i != 4)
|
||||
return 0;
|
||||
i = sscanf(sIP.c_str(), "%3[0-9].%3[0-9].%3[0-9].%3[0-9]", ip[0], ip[1],
|
||||
ip[2], ip[3]);
|
||||
if (i != 4) return 0;
|
||||
|
||||
// Beware that atoi("200") << 24 would overflow and turn negative!
|
||||
ret = atol(ip[0]) << 24;
|
||||
ret = atol(ip[0]) << 24;
|
||||
ret += atol(ip[1]) << 16;
|
||||
ret += atol(ip[2]) << 8;
|
||||
ret += atol(ip[3]) << 0;
|
||||
@@ -191,9 +196,7 @@ CString CUtils::GetSaltedHashPass(CString& sSalt) {
|
||||
}
|
||||
}
|
||||
|
||||
CString CUtils::GetSalt() {
|
||||
return CString::RandomString(20);
|
||||
}
|
||||
CString CUtils::GetSalt() { return CString::RandomString(20); }
|
||||
|
||||
CString CUtils::SaltedMD5Hash(const CString& sPass, const CString& sSalt) {
|
||||
return CString(sPass + sSalt).MD5();
|
||||
@@ -233,7 +236,7 @@ bool CUtils::GetBoolInput(const CString& sPrompt, bool bDefault) {
|
||||
return CUtils::GetBoolInput(sPrompt, &bDefault);
|
||||
}
|
||||
|
||||
bool CUtils::GetBoolInput(const CString& sPrompt, bool *pbDefault) {
|
||||
bool CUtils::GetBoolInput(const CString& sPrompt, bool* pbDefault) {
|
||||
CString sRet, sDefault;
|
||||
|
||||
if (pbDefault) {
|
||||
@@ -251,15 +254,17 @@ bool CUtils::GetBoolInput(const CString& sPrompt, bool *pbDefault) {
|
||||
}
|
||||
}
|
||||
|
||||
bool CUtils::GetNumInput(const CString& sPrompt, unsigned int& uRet, unsigned int uMin, unsigned int uMax, unsigned int uDefault) {
|
||||
bool CUtils::GetNumInput(const CString& sPrompt, unsigned int& uRet,
|
||||
unsigned int uMin, unsigned int uMax,
|
||||
unsigned int uDefault) {
|
||||
if (uMin > uMax) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CString sDefault = (uDefault != (unsigned int) ~0) ? CString(uDefault) : "";
|
||||
CString sDefault = (uDefault != (unsigned int)~0) ? CString(uDefault) : "";
|
||||
CString sNum, sHint;
|
||||
|
||||
if (uMax != (unsigned int) ~0) {
|
||||
if (uMax != (unsigned int)~0) {
|
||||
sHint = CString(uMin) + " to " + CString(uMax);
|
||||
} else if (uMin > 0) {
|
||||
sHint = CString(uMin) + " and up";
|
||||
@@ -283,7 +288,8 @@ bool CUtils::GetNumInput(const CString& sPrompt, unsigned int& uRet, unsigned in
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CUtils::GetInput(const CString& sPrompt, CString& sRet, const CString& sDefault, const CString& sHint) {
|
||||
bool CUtils::GetInput(const CString& sPrompt, CString& sRet,
|
||||
const CString& sDefault, const CString& sHint) {
|
||||
CString sExtra;
|
||||
CString sInput;
|
||||
sExtra += (!sHint.empty()) ? (" (" + sHint + ")") : "";
|
||||
@@ -321,7 +327,8 @@ bool CUtils::GetInput(const CString& sPrompt, CString& sRet, const CString& sDef
|
||||
|
||||
void CUtils::PrintError(const CString& sMessage) {
|
||||
if (CDebug::StdoutIsTTY())
|
||||
fprintf(stdout, BOLD BLU "[" RED " ** " BLU "]" DFL NORM" %s\n", sMessage.c_str());
|
||||
fprintf(stdout, BOLD BLU "[" RED " ** " BLU "]" DFL NORM " %s\n",
|
||||
sMessage.c_str());
|
||||
else
|
||||
fprintf(stdout, "%s\n", sMessage.c_str());
|
||||
fflush(stdout);
|
||||
@@ -329,7 +336,8 @@ void CUtils::PrintError(const CString& sMessage) {
|
||||
|
||||
void CUtils::PrintPrompt(const CString& sMessage) {
|
||||
if (CDebug::StdoutIsTTY())
|
||||
fprintf(stdout, BOLD BLU "[" YEL " ?? " BLU "]" DFL NORM " %s: ", sMessage.c_str());
|
||||
fprintf(stdout, BOLD BLU "[" YEL " ?? " BLU "]" DFL NORM " %s: ",
|
||||
sMessage.c_str());
|
||||
else
|
||||
fprintf(stdout, "[ ?? ] %s: ", sMessage.c_str());
|
||||
fflush(stdout);
|
||||
@@ -338,11 +346,12 @@ void CUtils::PrintPrompt(const CString& sMessage) {
|
||||
void CUtils::PrintMessage(const CString& sMessage, bool bStrong) {
|
||||
if (CDebug::StdoutIsTTY()) {
|
||||
if (bStrong)
|
||||
fprintf(stdout, BOLD BLU "[" YEL " ** " BLU "]" DFL BOLD " %s" NORM "\n",
|
||||
sMessage.c_str());
|
||||
fprintf(stdout,
|
||||
BOLD BLU "[" YEL " ** " BLU "]" DFL BOLD " %s" NORM "\n",
|
||||
sMessage.c_str());
|
||||
else
|
||||
fprintf(stdout, BOLD BLU "[" YEL " ** " BLU "]" DFL NORM " %s\n",
|
||||
sMessage.c_str());
|
||||
sMessage.c_str());
|
||||
} else
|
||||
fprintf(stdout, "%s\n", sMessage.c_str());
|
||||
|
||||
@@ -351,7 +360,8 @@ void CUtils::PrintMessage(const CString& sMessage, bool bStrong) {
|
||||
|
||||
void CUtils::PrintAction(const CString& sMessage) {
|
||||
if (CDebug::StdoutIsTTY())
|
||||
fprintf(stdout, BOLD BLU "[ .. " BLU "]" DFL NORM " %s...\n", sMessage.c_str());
|
||||
fprintf(stdout, BOLD BLU "[ .. " BLU "]" DFL NORM " %s...\n",
|
||||
sMessage.c_str());
|
||||
else
|
||||
fprintf(stdout, "%s... ", sMessage.c_str());
|
||||
fflush(stdout);
|
||||
@@ -360,10 +370,15 @@ void CUtils::PrintAction(const CString& sMessage) {
|
||||
void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) {
|
||||
if (CDebug::StdoutIsTTY()) {
|
||||
if (bSuccess) {
|
||||
if (!sMessage.empty())
|
||||
fprintf(stdout, BOLD BLU "[" GRN " >> " BLU "]" DFL NORM " %s\n", sMessage.c_str());
|
||||
if (!sMessage.empty())
|
||||
fprintf(stdout,
|
||||
BOLD BLU "[" GRN " >> " BLU "]" DFL NORM " %s\n",
|
||||
sMessage.c_str());
|
||||
} else {
|
||||
fprintf(stdout, sMessage.empty() ? " failed\n" : BOLD BLU "[" RED " !! " BLU "]" DFL NORM BOLD RED " %s" DFL NORM "\n", sMessage.c_str());
|
||||
fprintf(stdout, sMessage.empty() ? " failed\n" : BOLD BLU
|
||||
"[" RED " !! " BLU "]" DFL NORM BOLD RED
|
||||
" %s" DFL NORM "\n",
|
||||
sMessage.c_str());
|
||||
}
|
||||
} else {
|
||||
if (bSuccess) {
|
||||
@@ -381,30 +396,30 @@ void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) {
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* Switch GMT-X and GMT+X
|
||||
*
|
||||
* See https://en.wikipedia.org/wiki/Tz_database#Area
|
||||
*
|
||||
* "In order to conform with the POSIX style, those zone names beginning
|
||||
* with "Etc/GMT" have their sign reversed from what most people expect.
|
||||
* In this style, zones west of GMT have a positive sign and those east
|
||||
* have a negative sign in their name (e.g "Etc/GMT-14" is 14 hours
|
||||
* ahead/east of GMT.)"
|
||||
*/
|
||||
inline CString FixGMT(CString sTZ) {
|
||||
if (sTZ.length() >= 4 && sTZ.StartsWith("GMT")) {
|
||||
if (sTZ[3] == '+') {
|
||||
sTZ[3] = '-';
|
||||
} else if (sTZ[3] == '-') {
|
||||
sTZ[3] = '+';
|
||||
}
|
||||
/* Switch GMT-X and GMT+X
|
||||
*
|
||||
* See https://en.wikipedia.org/wiki/Tz_database#Area
|
||||
*
|
||||
* "In order to conform with the POSIX style, those zone names beginning
|
||||
* with "Etc/GMT" have their sign reversed from what most people expect.
|
||||
* In this style, zones west of GMT have a positive sign and those east
|
||||
* have a negative sign in their name (e.g "Etc/GMT-14" is 14 hours
|
||||
* ahead/east of GMT.)"
|
||||
*/
|
||||
inline CString FixGMT(CString sTZ) {
|
||||
if (sTZ.length() >= 4 && sTZ.StartsWith("GMT")) {
|
||||
if (sTZ[3] == '+') {
|
||||
sTZ[3] = '-';
|
||||
} else if (sTZ[3] == '-') {
|
||||
sTZ[3] = '+';
|
||||
}
|
||||
return sTZ;
|
||||
}
|
||||
return sTZ;
|
||||
}
|
||||
}
|
||||
|
||||
CString CUtils::CTime(time_t t, const CString& sTimezone) {
|
||||
char s[30] = {}; // should have at least 26 bytes
|
||||
char s[30] = {}; // should have at least 26 bytes
|
||||
if (sTimezone.empty()) {
|
||||
ctime_r(&t, s);
|
||||
// ctime() adds a trailing newline
|
||||
@@ -432,7 +447,8 @@ CString CUtils::CTime(time_t t, const CString& sTimezone) {
|
||||
return CString(s).Trim_n();
|
||||
}
|
||||
|
||||
CString CUtils::FormatTime(time_t t, const CString& sFormat, const CString& sTimezone) {
|
||||
CString CUtils::FormatTime(time_t t, const CString& sFormat,
|
||||
const CString& sTimezone) {
|
||||
char s[1024] = {};
|
||||
tm m;
|
||||
if (sTimezone.empty()) {
|
||||
@@ -472,7 +488,9 @@ CString CUtils::FormatServerTime(const timeval& tv) {
|
||||
// TODO support message-tags properly
|
||||
struct tm stm;
|
||||
memset(&stm, 0, sizeof(stm));
|
||||
const time_t secs = tv.tv_sec; // OpenBSD has tv_sec as int, so explicitly convert it to time_t to make gmtime_r() happy
|
||||
const time_t secs = tv.tv_sec; // OpenBSD has tv_sec as int, so explicitly
|
||||
// convert it to time_t to make gmtime_r()
|
||||
// happy
|
||||
gmtime_r(&secs, &stm);
|
||||
char sTime[20] = {};
|
||||
strftime(sTime, sizeof(sTime), "%Y-%m-%dT%H:%M:%S", &stm);
|
||||
@@ -496,26 +514,30 @@ timeval CUtils::ParseServerTime(const CString& sTime) {
|
||||
}
|
||||
|
||||
namespace {
|
||||
void FillTimezones(const CString& sPath, SCString& result, const CString& sPrefix) {
|
||||
CDir Dir;
|
||||
Dir.Fill(sPath);
|
||||
for (CFile* pFile : Dir) {
|
||||
CString sName = pFile->GetShortName();
|
||||
CString sFile = pFile->GetLongName();
|
||||
if (sName == "posix" || sName == "right") continue; // these 2 dirs contain the same filenames
|
||||
if (sName.EndsWith(".tab") || sName == "posixrules" || sName == "localtime") continue;
|
||||
if (pFile->IsDir()) {
|
||||
if (sName == "Etc") {
|
||||
FillTimezones(sFile, result, sPrefix);
|
||||
} else {
|
||||
FillTimezones(sFile, result, sPrefix + sName + "/");
|
||||
}
|
||||
void FillTimezones(const CString& sPath, SCString& result,
|
||||
const CString& sPrefix) {
|
||||
CDir Dir;
|
||||
Dir.Fill(sPath);
|
||||
for (CFile* pFile : Dir) {
|
||||
CString sName = pFile->GetShortName();
|
||||
CString sFile = pFile->GetLongName();
|
||||
if (sName == "posix" || sName == "right")
|
||||
continue; // these 2 dirs contain the same filenames
|
||||
if (sName.EndsWith(".tab") || sName == "posixrules" ||
|
||||
sName == "localtime")
|
||||
continue;
|
||||
if (pFile->IsDir()) {
|
||||
if (sName == "Etc") {
|
||||
FillTimezones(sFile, result, sPrefix);
|
||||
} else {
|
||||
result.insert(sPrefix + sName);
|
||||
FillTimezones(sFile, result, sPrefix + sName + "/");
|
||||
}
|
||||
} else {
|
||||
result.insert(sPrefix + sName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCString CUtils::GetTimezones() {
|
||||
static SCString result;
|
||||
@@ -535,8 +557,10 @@ SCString CUtils::GetEncodings() {
|
||||
icu::ErrorCode e;
|
||||
for (int st = 0; st < ucnv_countStandards(); ++st) {
|
||||
const char* pStdName = ucnv_getStandard(st, e);
|
||||
icu::LocalUEnumerationPointer ue(ucnv_openStandardNames(pConvName, pStdName, e));
|
||||
while (const char* pStdConvNameEnum = uenum_next(ue.getAlias(), nullptr, e)) {
|
||||
icu::LocalUEnumerationPointer ue(
|
||||
ucnv_openStandardNames(pConvName, pStdName, e));
|
||||
while (const char* pStdConvNameEnum =
|
||||
uenum_next(ue.getAlias(), nullptr, e)) {
|
||||
ssResult.insert(pStdConvNameEnum);
|
||||
}
|
||||
}
|
||||
@@ -574,7 +598,7 @@ bool CTable::AddColumn(const CString& sName) {
|
||||
CTable::size_type CTable::AddRow() {
|
||||
// Don't add a row if no headers are defined
|
||||
if (m_vsHeaders.empty()) {
|
||||
return (size_type) -1;
|
||||
return (size_type)-1;
|
||||
}
|
||||
|
||||
// Add a vector with enough space for each column
|
||||
@@ -582,19 +606,19 @@ CTable::size_type CTable::AddRow() {
|
||||
return size() - 1;
|
||||
}
|
||||
|
||||
bool CTable::SetCell(const CString& sColumn, const CString& sValue, size_type uRowIdx) {
|
||||
if (uRowIdx == (size_type) ~0) {
|
||||
bool CTable::SetCell(const CString& sColumn, const CString& sValue,
|
||||
size_type uRowIdx) {
|
||||
if (uRowIdx == (size_type)~0) {
|
||||
if (empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uRowIdx = size() -1;
|
||||
uRowIdx = size() - 1;
|
||||
}
|
||||
|
||||
unsigned int uColIdx = GetColumnIndex(sColumn);
|
||||
|
||||
if (uColIdx == (unsigned int) -1)
|
||||
return false;
|
||||
if (uColIdx == (unsigned int)-1) return false;
|
||||
|
||||
(*this)[uRowIdx][uColIdx] = sValue;
|
||||
|
||||
@@ -632,13 +656,12 @@ VCString CTable::Render() const {
|
||||
|
||||
unsigned int CTable::GetColumnIndex(const CString& sName) const {
|
||||
for (unsigned int i = 0; i < m_vsHeaders.size(); i++) {
|
||||
if (m_vsHeaders[i] == sName)
|
||||
return i;
|
||||
if (m_vsHeaders[i] == sName) return i;
|
||||
}
|
||||
|
||||
DEBUG("CTable::GetColumnIndex(" + sName + ") failed");
|
||||
|
||||
return (unsigned int) -1;
|
||||
return (unsigned int)-1;
|
||||
}
|
||||
|
||||
void CTable::Clear() {
|
||||
@@ -648,38 +671,37 @@ void CTable::Clear() {
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBSSL
|
||||
CBlowfish::CBlowfish(const CString & sPassword, int iEncrypt, const CString & sIvec)
|
||||
: m_ivec((unsigned char *)calloc(sizeof(unsigned char), 8)),
|
||||
m_bkey(),
|
||||
m_iEncrypt(iEncrypt),
|
||||
m_num(0)
|
||||
{
|
||||
|
||||
CBlowfish::CBlowfish(const CString& sPassword, int iEncrypt,
|
||||
const CString& sIvec)
|
||||
: m_ivec((unsigned char*)calloc(sizeof(unsigned char), 8)),
|
||||
m_bkey(),
|
||||
m_iEncrypt(iEncrypt),
|
||||
m_num(0) {
|
||||
if (sIvec.length() >= 8) {
|
||||
memcpy(m_ivec, sIvec.data(), 8);
|
||||
}
|
||||
|
||||
BF_set_key(&m_bkey, (unsigned int)sPassword.length(), (unsigned char *)sPassword.data());
|
||||
BF_set_key(&m_bkey, (unsigned int)sPassword.length(),
|
||||
(unsigned char*)sPassword.data());
|
||||
}
|
||||
|
||||
CBlowfish::~CBlowfish() {
|
||||
free(m_ivec);
|
||||
}
|
||||
CBlowfish::~CBlowfish() { free(m_ivec); }
|
||||
|
||||
//! output must be freed
|
||||
unsigned char *CBlowfish::MD5(const unsigned char *input, u_int ilen) {
|
||||
unsigned char *output = (unsigned char *)malloc(MD5_DIGEST_LENGTH);
|
||||
unsigned char* CBlowfish::MD5(const unsigned char* input, u_int ilen) {
|
||||
unsigned char* output = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
|
||||
::MD5(input, ilen, output);
|
||||
return output;
|
||||
}
|
||||
|
||||
//! returns an md5 of the CString (not hex encoded)
|
||||
CString CBlowfish::MD5(const CString & sInput, bool bHexEncode) {
|
||||
CString CBlowfish::MD5(const CString& sInput, bool bHexEncode) {
|
||||
CString sRet;
|
||||
unsigned char *data = MD5((const unsigned char *)sInput.data(), (unsigned int)sInput.length());
|
||||
unsigned char* data =
|
||||
MD5((const unsigned char*)sInput.data(), (unsigned int)sInput.length());
|
||||
|
||||
if (!bHexEncode) {
|
||||
sRet.append((const char *)data, MD5_DIGEST_LENGTH);
|
||||
sRet.append((const char*)data, MD5_DIGEST_LENGTH);
|
||||
} else {
|
||||
for (int a = 0; a < MD5_DIGEST_LENGTH; a++) {
|
||||
sRet += g_HexDigits[data[a] >> 4];
|
||||
@@ -692,23 +714,26 @@ CString CBlowfish::MD5(const CString & sInput, bool bHexEncode) {
|
||||
}
|
||||
|
||||
//! output must be the same size as input
|
||||
void CBlowfish::Crypt(unsigned char *input, unsigned char *output, u_int uBytes) {
|
||||
BF_cfb64_encrypt(input, output, uBytes, &m_bkey, m_ivec, &m_num, m_iEncrypt);
|
||||
void CBlowfish::Crypt(unsigned char* input, unsigned char* output,
|
||||
u_int uBytes) {
|
||||
BF_cfb64_encrypt(input, output, uBytes, &m_bkey, m_ivec, &m_num,
|
||||
m_iEncrypt);
|
||||
}
|
||||
|
||||
//! must free result
|
||||
unsigned char * CBlowfish::Crypt(unsigned char *input, u_int uBytes) {
|
||||
unsigned char *buff = (unsigned char *)malloc(uBytes);
|
||||
unsigned char* CBlowfish::Crypt(unsigned char* input, u_int uBytes) {
|
||||
unsigned char* buff = (unsigned char*)malloc(uBytes);
|
||||
Crypt(input, buff, uBytes);
|
||||
return buff;
|
||||
}
|
||||
|
||||
CString CBlowfish::Crypt(const CString & sData) {
|
||||
unsigned char *buff = Crypt((unsigned char *)sData.data(), (unsigned int)sData.length());
|
||||
CString CBlowfish::Crypt(const CString& sData) {
|
||||
unsigned char* buff =
|
||||
Crypt((unsigned char*)sData.data(), (unsigned int)sData.length());
|
||||
CString sOutput;
|
||||
sOutput.append((const char *)buff, sData.length());
|
||||
sOutput.append((const char*)buff, sData.length());
|
||||
free(buff);
|
||||
return sOutput;
|
||||
}
|
||||
|
||||
#endif // HAVE_LIBSSL
|
||||
#endif // HAVE_LIBSSL
|
||||
|
||||
Reference in New Issue
Block a user