打架和睡觉会导致不同的结果
cout and sleep cause different results
我有一个接受用户输入(用户名/密码)的类,bcrypt对输入密码进行哈希,以检查它是否与存储在数据库中的哈希相匹配,如果成功,则登录用户。我遇到的问题是如果我调用 cout << "n"
,,,,去,,,,sleep(1)
在哈希之前,密码检查按预期工作,但如果我注释掉sleep
和cout
,哈希器总是失败,这导致用户得到错误的invalid credentials
消息。
我使用pqxx来读取数据库,使用rg3的bcrypt来散列/检查密码。
第一次发现问题的代码片段:
// pqxx::result
string storedPass = result.begin()["passwordBCrypt_12"].as<string>();
// Uncommenting either cout or sleep causes checkPassword to work as expected
//cout << "n"; // Confusingly, cout must contain "n" to have the effect
//sleep(1);
if (!checkPassword(inputPass, storedPass))
credError = true;
checkPassword()
代码:
bool DB::checkPassword(string& password, string& passwordHash){
char cpassword[password.length()];
char hashInDatabase[BCRYPT_HASHSIZE];
char outTestHash[BCRYPT_HASHSIZE];
for (size_t i = 0; i < password.length(); i++){
cpassword[i] = password[i];
}
for (size_t i = 0; i < BCRYPT_HASHSIZE; i++){
hashInDatabase[i] = passwordHash[i];
}
if (bcrypt_hashpw(cpassword, hashInDatabase, outTestHash) == 0){
if (strcmp(hashInDatabase, outTestHash) == 0) {
// password matches
return true;
}
// password does not match
}
return false;
}
字符串inputPass
从第一个代码片段没有传递给其他线程作为引用;
正如评论中提到的,你有一个同步问题。
根据经验,只要sleep或cout(或printf)解决了你的问题,那么你就没有在程序的某些语句中正确地实现互斥逻辑。
您考虑纠正checkPassword函数。您100%确定存储传递的方式不需要某种同步吗?
PS:令人困惑的"n"效果有刷新缓冲区的习惯,导致同步。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 算术运算的结果类似于:C浮点变量中的1/3
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 密码登录程序将永远循环并显示不正确的结果
- 如何让C++'tally up'结果并制定计划?
- 为什么这个程序的结果是3 "born"?和 4 死
- 打架和睡觉会导致不同的结果