@@ -60,8 +60,14 @@ std::string PasswordGenerator::normalizeUser(const std::string &user)
6060
6161std::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
97104std::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}
0 commit comments