Skip to content

Commit e7e7e33

Browse files
committed
Long PW
1 parent 42de08e commit e7e7e33

5 files changed

Lines changed: 59 additions & 8 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,6 @@ ModelManifest.xml
269269
/BrotBoxEngine/GlobalKeyboardDLL/EmbeddedDlls.cpp
270270
/BrotBoxEngine/GlobalKeyboardDLL/EmbeddedDlls.h
271271
imgui.ini
272+
ExamplePaintColorHistory.dat
273+
ExamplePaintDigitHotkeys.dat
274+
ExamplePaintFavoriteColors.dat

Examples/ExampleMother/ExampleMother.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ class MyGame : public bbe::Game
618618
#endif
619619

620620
bool terriActive = false;
621+
bool pwLongPassword = false;
621622

622623
bbe::TimePoint lastServerReach = bbe::TimePoint::epoch();
623624
bool serverUnreachableSilenced = false;
@@ -3504,6 +3505,10 @@ class MyGame : public bbe::Game
35043505

35053506
ImGui::BeginDisabled(generating);
35063507
bool newHashRequested = false;
3508+
if (ImGui::Checkbox("Long password (32 chars)", &pwLongPassword))
3509+
{
3510+
newHashRequested = true;
3511+
}
35073512
if (ImGui::bbe::InputText("Master PW", masterPw, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_Password))
35083513
{
35093514
newHashRequested = true;
@@ -3535,12 +3540,13 @@ class MyGame : public bbe::Game
35353540
bbe::String capturedNormServ = normServ;
35363541
bbe::String capturedNormUser = normUser;
35373542
bbe::List<bbe::String> capturedKnownHashes = passwordManager->knownHashes;
3543+
const bool capturedLongPassword = pwLongPassword;
35383544

35393545
pwGenExpectedHashes = capturedNormServ.isEmpty() ? 1 : 2;
35403546
pwGenStartTime = bbe::TimePoint();
35413547

35423548
pwGenFuture = std::async(std::launch::async,
3543-
[capturedMasterPw, capturedRepeat, capturedNormServ, capturedNormUser, capturedKnownHashes]() -> PwGenResult
3549+
[capturedMasterPw, capturedRepeat, capturedNormServ, capturedNormUser, capturedKnownHashes, capturedLongPassword]() -> PwGenResult
35443550
{
35453551
PwGenResult result;
35463552
result.masterPwHash = PasswordGenerator::generateHash(std::string(capturedMasterPw.getRaw())).c_str();
@@ -3554,7 +3560,7 @@ class MyGame : public bbe::Game
35543560
std::string hashableString = std::string(capturedMasterPw.getRaw()) + "|||" + capturedNormServ.getRaw();
35553561
if (!capturedNormUser.isEmpty())
35563562
hashableString += std::string("|||") + capturedNormUser.getRaw();
3557-
result.servicePw = PasswordGenerator::generateHash(hashableString).c_str();
3563+
result.servicePw = PasswordGenerator::generateHash(hashableString, capturedLongPassword ? 32 : 16).c_str();
35583564
result.hashCount = 2;
35593565

35603566
if (!isKnown)
@@ -4327,6 +4333,7 @@ int main(int argc, char **argv)
43274333
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF); // See: https://stackoverflow.com/questions/30413066/how-do-i-diagnose-heap-corruption-errors-on-windows
43284334
#else
43294335
(void)argc;
4336+
(void)argv;
43304337
#endif
43314338
MyGame *mg = new MyGame();
43324339
mg->setMsaaSamples(0);

Examples/ExampleMother/PasswordGenerator.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ std::string PasswordGenerator::normalizeUser(const std::string &user)
6060

6161
std::string PasswordGenerator::generateHash(const std::string &data)
6262
{
63-
unsigned char hash[16] = {};
64-
const int err = crypto_pwhash_argon2id(hash, sizeof(hash), data.c_str(), data.size(), (const unsigned char *)"BrotbEnginePWGv1", 5, static_cast<size_t>(256 * 1024 * 1024), crypto_pwhash_argon2id_ALG_ARGON2ID13);
63+
return generateHash(data, 16);
64+
}
65+
66+
std::string PasswordGenerator::generateHash(const std::string &data, size_t length)
67+
{
68+
if (length == 0) length = 16;
69+
std::vector<unsigned char> hash(length);
70+
const int err = crypto_pwhash_argon2id(hash.data(), hash.size(), data.c_str(), data.size(), (const unsigned char *)"BrotbEnginePWGv1", 5, static_cast<size_t>(256 * 1024 * 1024), crypto_pwhash_argon2id_ALG_ARGON2ID13);
6571
if (err != 0)
6672
{
6773
std::cerr << "FATAL: crypto_pwhash_argon2id failed." << std::endl;
@@ -73,7 +79,8 @@ std::string PasswordGenerator::generateHash(const std::string &data)
7379
const std::string special = "!@#%*-_=+,.?";
7480
const std::string all = lower + upper + digits + special;
7581
std::string retVal;
76-
for (size_t i = 0; i < sizeof(hash); i++)
82+
retVal.reserve(hash.size());
83+
for (size_t i = 0; i < hash.size(); i++)
7784
{
7885
const std::string *set = nullptr;
7986
// The first char is always lower, second always upper and so on. This is a very simple way to ensure
@@ -95,11 +102,16 @@ std::string PasswordGenerator::generateHash(const std::string &data)
95102
}
96103

97104
std::string PasswordGenerator::generateServicePassword(const std::string &masterPw, const std::string &service, const std::string &user)
105+
{
106+
return generateServicePassword(masterPw, service, user, 16);
107+
}
108+
109+
std::string PasswordGenerator::generateServicePassword(const std::string &masterPw, const std::string &service, const std::string &user, size_t length)
98110
{
99111
std::string normServ = normalizeService(service);
100112
std::string normUser = normalizeUser(user);
101113
std::string hashableString = masterPw + "|||" + normServ;
102114
if (!normUser.empty())
103115
hashableString += "|||" + normUser;
104-
return generateHash(hashableString);
116+
return generateHash(hashableString, length);
105117
}

Examples/ExampleMother/PasswordGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ namespace PasswordGenerator
66
std::string normalizeService(const std::string &service);
77
std::string normalizeUser(const std::string &user);
88
std::string generateHash(const std::string &data);
9+
std::string generateHash(const std::string &data, size_t length);
910
std::string generateServicePassword(const std::string &masterPw, const std::string &service, const std::string &user);
11+
std::string generateServicePassword(const std::string &masterPw, const std::string &service, const std::string &user, size_t length);
1012
}

Examples/ExamplePasswordGenerator/ExamplePasswordGenerator.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,35 @@ static bool setClipboard(const std::string &text)
103103
#endif
104104
}
105105

106-
int main()
106+
static void printUsage(const char *exe)
107107
{
108+
std::cout << "Usage: " << exe << " [--long-password]" << std::endl;
109+
std::cout << " --long-password Generate 32 character password (default: 16)" << std::endl;
110+
}
111+
112+
int main(int argc, char **argv)
113+
{
114+
bool longPassword = false;
115+
for (int i = 1; i < argc; i++)
116+
{
117+
const char *a = argv[i];
118+
if (std::strcmp(a, "--long-password") == 0)
119+
{
120+
longPassword = true;
121+
}
122+
else if (std::strcmp(a, "--help") == 0 || std::strcmp(a, "-h") == 0)
123+
{
124+
printUsage(argv[0]);
125+
return 0;
126+
}
127+
else
128+
{
129+
std::cerr << "Unknown argument: " << a << std::endl;
130+
printUsage(argv[0]);
131+
return 1;
132+
}
133+
}
134+
108135
std::string masterPw = readPassword("Master Password: ");
109136
std::string masterPwRepeat = readPassword("Repeat Password: ");
110137

@@ -124,7 +151,7 @@ int main()
124151
}
125152

126153
std::cout << "Generating..." << std::flush;
127-
std::string password = PasswordGenerator::generateServicePassword(masterPw, service, user);
154+
std::string password = PasswordGenerator::generateServicePassword(masterPw, service, user, longPassword ? 32 : 16);
128155
std::cout << " Done." << std::endl;
129156

130157
if (setClipboard(password))

0 commit comments

Comments
 (0)