c++ - 自定义 C++ ASP .NET 成员(member)登录

标签 c++ hash asp.net-membership

有谁知道如何使用 ASP .NET 成员(member)资格提供的盐 key 对用户密码进行哈希处理?

我正在开发 C++ Linux 应用程序,我只能访问 SQL Server。

谢谢,

最佳答案

Here是用C#编写的ASP.Net Membership使用的编码算法。

它使用 System.Security,因此您可能需要查看 MONO如果你想在 Lunix 上运行。

注意:我不熟悉 MONO。

private string EncodePassword(string pass, int passwordFormat, string salt)
{
    if (passwordFormat == 0) // MembershipPasswordFormat.Clear
        return pass;

    byte[] bIn = Encoding.Unicode.GetBytes(pass);
    byte[] bSalt = Convert.FromBase64String(salt);
    byte[] bRet = null;

    if (passwordFormat == 1)
    { // MembershipPasswordFormat.Hashed
        HashAlgorithm hm = GetHashAlgorithm();
        if (hm is KeyedHashAlgorithm)
        {
            KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm;
            if (kha.Key.Length == bSalt.Length)
            {
                kha.Key = bSalt;
            }
            else if (kha.Key.Length < bSalt.Length)
            {
                byte[] bKey = new byte[kha.Key.Length];
                Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length);
                kha.Key = bKey;
            }
            else
            {
                byte[] bKey = new byte[kha.Key.Length];
                for (int iter = 0; iter < bKey.Length; )
                {
                    int len = Math.Min(bSalt.Length, bKey.Length - iter);
                    Buffer.BlockCopy(bSalt, 0, bKey, iter, len);
                    iter += len;
                }
                kha.Key = bKey;
            }
            bRet = kha.ComputeHash(bIn);
        }
        else
        {
            byte[] bAll = new byte[bSalt.Length + bIn.Length];
            Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
            Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
            bRet = hm.ComputeHash(bAll);
        }
    }
    else
    {
        byte[] bAll = new byte[bSalt.Length + bIn.Length];
        Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
        Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
        bRet = EncryptPassword(bAll, _LegacyPasswordCompatibilityMode);
    }

    return Convert.ToBase64String(bRet);
}

private string GenerateSalt()
{
    byte[] buf = new byte[SALT_SIZE];
    (new RNGCryptoServiceProvider()).GetBytes(buf);
    return Convert.ToBase64String(buf);
}

关于c++ - 自定义 C++ ASP .NET 成员(member)登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24345803/

相关文章:

C++ 多重运算符=()

C++ 初始化一个变量并用 std::cin 赋值

ruby - 填充数组并将哈希值转换为数组

python - 验证文件未被修改

c# - .Net 成员(member)提供者和名字和姓氏

c++ - cpp 使用命名空间时出现错误重复符号

c++ - 防止 Windows 在 native 代码未处理的异常上显示任何对话框

algorithm - 哈希字符串的强大功能并恢复它

c# - 指定的成员资格提供商名称无效。参数名称 : providerName

c# - 不使用自定义成员资格提供程序的 ASP.NET MVC 登录 Controller 方法?