SHA-1带2个字符串的连接

问题描述 投票:-1回答:1

我对C ++几乎一无所知,我确实需要一些帮助。我当前正在尝试编译游戏,现在游戏中的登录系统接受以下任一方法:

  • 普通密码
  • sha1加密的密码

我想做的是将我的密码加一个盐,然后对其进行sha1-加密。

所以我在数据库中添加了新列。现在我有这些专栏:

  • 密码
  • passwordSalt

并且要登录,我将在PHP中通过比较普通密码+ passwordsalt,并查看其是否与密码匹配。

PHP伪代码:if (plainPassword . passwordSalt == password) { // login }

我现在想在C ++中做同样的事情。

这是登录功能:

bool passwordTest(const std::string &plain, std::string &hash, std::string &salt)
{
    switch(g_config.getNumber(ConfigManager::PASSWORD_TYPE)){
    case PASSWORD_TYPE_PLAIN:
    {
        if(plain == hash){
            return true;
        }
        break;
    }
    case PASSWORD_TYPE_SHA1:
    {
        SHA1 sha1;
        unsigned sha1Hash[5];
        std::stringstream hexStream;

        sha1.Input((const unsigned char*)plain.c_str(), plain.length());
        sha1.Result(sha1Hash);

        hexStream.flags(std::ios::hex | std::ios::uppercase);
        for(uint32_t i = 0; i < 5; ++i){
            hexStream << std::setw(8) << std::setfill('0') << (uint32_t)sha1Hash[i];
        }

        std::transform(hash.begin(), hash.end(), hash.begin(), upchar);
        if(hexStream.str() == hash){
            return true;
        }

        break;
    }
    }
    return false;
}

我在参数中添加了salt。现在,我需要以某种方式将“普通”和“盐”串联起来,然后将其用作输入。现在,它只是使用“普通”作为输入,如此处所示:

sha1.Input((const unsigned char*)plain.c_str(), plain.length());

因此,我想完成的是在将其作为输入传递之前,先使“普通” =“普通” +“盐”。我该怎么办?

我尝试使用“ strcat”,但似乎做错了。我试过了:

const std::string &hashedPassword = strcat(plain + hashed);
sha1.Input((const unsigned char*)hashedPassword.c_str(), hashedPassword.length());

但是那不起作用。如何连接2个参数值“普通”和“散列”?

TL / DR:我的函数采用以下参数值:

bool passwordTest(const std::string &plain, std::string &hash, std::string &hashed)

我想将plainhashed合并为一个字符串,称为“ hashedPassword”。然后将其用作sha1加密中的输入,例如:

sha1.Input((const unsigned char*)hashedPassword.c_str(), hashedPassword.length());
c++ string encryption concatenation sha1
1个回答
0
投票

字面上只是将它们串联起来。 C ++ std::string具有+运算符。

const std::string hashedPassword = plain + hashed;

[strcatchar数组/缓冲区的C函数。

注意,我也拿走了&;没有必要。实际上,总的来说,我建议您查阅参考文献,因为我不能完全确定您是否一直按预期的方式使用它们。

© www.soinside.com 2019 - 2024. All rights reserved.