Refactor around translatePassword

Change types for passed password from wstring to string, which removes converting
back and forth in most cases. Move the narrow_to_wide conversion, where its neccessary,
closer to irrlicht. Remove trailing spaces in guiPasswordChange.cpp. Make parameters for
translatePassword passed as const reference.
This commit is contained in:
est31 2015-04-17 06:10:30 +02:00
parent 821d0025da
commit f0a1379e5a
8 changed files with 24 additions and 22 deletions

View File

@ -1062,8 +1062,8 @@ void Client::sendChatMessage(const std::wstring &message)
Send(&pkt);
}
void Client::sendChangePassword(const std::wstring &oldpassword,
const std::wstring &newpassword)
void Client::sendChangePassword(const std::string &oldpassword,
const std::string &newpassword)
{
Player *player = m_env.getLocalPlayer();
if(player == NULL)

View File

@ -407,8 +407,8 @@ public:
const std::map<std::string, std::string> &fields);
void sendInventoryAction(InventoryAction *a);
void sendChatMessage(const std::wstring &message);
void sendChangePassword(const std::wstring &oldpassword,
const std::wstring &newpassword);
void sendChangePassword(const std::string &oldpassword,
const std::string &newpassword);
void sendDamage(u8 damage);
void sendBreath(u16 breath);
void sendRespawn();

View File

@ -392,7 +392,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
else
playername = menudata.name;
password = translatePassword(playername, narrow_to_wide(menudata.password));
password = translatePassword(playername, menudata.password);
g_settings->set("name", playername);

View File

@ -79,7 +79,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
Remove stuff
*/
removeChildren();
/*
Calculate new sizes and positions
*/
@ -89,7 +89,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
screensize.X/2 + 580/2,
screensize.Y/2 + 300/2
);
DesiredRect = rect;
recalculateAbsolutePosition(false);
@ -112,7 +112,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
Environment->setFocus(e);
e->setPasswordBox(true);
@ -128,7 +128,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
e->setPasswordBox(true);
}
@ -143,7 +143,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
e->setPasswordBox(true);
}
@ -162,7 +162,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
core::rect<s32> rect(0, 0, 300, 20);
rect += topleft_client + v2s32(35, ypos);
text = wgettext("Passwords do not match!");
IGUIElement *e =
IGUIElement *e =
Environment->addStaticText(
text,
rect, false, true, this, ID_message);
@ -177,7 +177,7 @@ void GUIPasswordChange::drawMenu()
if (!skin)
return;
video::IVideoDriver* driver = Environment->getVideoDriver();
video::SColor bgcolor(140,0,0,0);
driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
@ -203,7 +203,8 @@ bool GUIPasswordChange::acceptInput()
e->setVisible(true);
return false;
}
m_client->sendChangePassword(oldpass, newpass);
m_client->sendChangePassword(wide_to_narrow(oldpass),
wide_to_narrow(newpass));
return true;
}

View File

@ -267,8 +267,8 @@ void Server::handleCommand_Auth(NetworkPacket* pkt)
DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD);
return;
}
std::wstring raw_default_password =
narrow_to_wide(g_settings->get("default_password"));
std::string raw_default_password =
g_settings->get("default_password");
std::string initial_password =
translatePassword(playername, raw_default_password);
@ -571,8 +571,8 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt)
L"disallowed. Set a password and try again.");
return;
}
std::wstring raw_default_password =
narrow_to_wide(g_settings->get("default_password"));
std::string raw_default_password =
g_settings->get("default_password");
std::string initial_password =
translatePassword(playername, raw_default_password);

View File

@ -255,8 +255,7 @@ int ModApiUtil::l_get_password_hash(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
std::string raw_password = luaL_checkstring(L, 2);
std::string hash = translatePassword(name,
narrow_to_wide(raw_password));
std::string hash = translatePassword(name, raw_password);
lua_pushstring(L, hash.c_str());
return 1;
}

View File

@ -181,12 +181,13 @@ std::string wide_to_narrow(const std::wstring &wcs)
// their password. (Exception : if the password field is
// blank, we send a blank password - this is for backwards
// compatibility with password-less players).
std::string translatePassword(std::string playername, std::wstring password)
std::string translatePassword(const std::string &playername,
const std::string &password)
{
if (password.length() == 0)
return "";
std::string slt = playername + wide_to_narrow(password);
std::string slt = playername + password;
SHA1 sha1;
sha1.addBytes(slt.c_str(), slt.length());
unsigned char *digest = sha1.getDigest();

View File

@ -43,7 +43,8 @@ wchar_t *narrow_to_wide_c(const char *str);
std::wstring narrow_to_wide(const std::string &mbs);
std::string wide_to_narrow(const std::wstring &wcs);
std::string translatePassword(std::string playername, std::wstring password);
std::string translatePassword(const std::string &playername,
const std::string &password);
std::string urlencode(std::string str);
std::string urldecode(std::string str);
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);