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

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

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

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

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

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

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

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

@ -43,7 +43,8 @@ wchar_t *narrow_to_wide_c(const char *str);
std::wstring narrow_to_wide(const std::string &mbs); std::wstring narrow_to_wide(const std::string &mbs);
std::string wide_to_narrow(const std::wstring &wcs); 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 urlencode(std::string str);
std::string urldecode(std::string str); std::string urldecode(std::string str);
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask); u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);