【问题标题】:Is SHA256 considered insecure?- SonarQube Quality Gate - How can I fix it?SHA256 是否被认为不安全?- SonarQube 质量门 - 我该如何解决?
【发布时间】:2020-07-15 10:57:54
【问题描述】:

我有以下代码

        private static string sensitiveKey = "<REPLACE_WITH_KEY>"  

        public static string Encrypt(string input)
        {
            // Get the bytes of the string
            byte[] passwordBytes = Encoding.UTF8.GetBytes(sensitiveKey);
            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
            byte[] bytesEncrypted = EncryptStringToBytes_Aes(input, passwordBytes);
            string result = Convert.ToBase64String(bytesEncrypted);
            return result;
        }

SonarQube 说

加密散列函数用于唯一标识信息而不存储其原始形式。如果操作不当,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或者将原始数据替换为具有相同哈希值的另一个数据。

还有

仅使用目前已知的强大的散列算法。避免在安全环境中完全使用 MD5SHA1 等算法。

所以问题是,如何改进我的代码以确保其安全?

有样品吗?

【问题讨论】:

  • 该代码不使用 MD5 也不使用 SHA1。
  • 正如 Fildor 指出的那样,您的代码不使用 MD5,而是使用 SHA256。这并不比general-purpose hashes are obsolete for passwords 的MD5 好。相反,您应该使用专门用于密码散列的函数,例如 bcryptArgon2
  • 更新了问题的标题。让我尝试使用 bcrypt 或 Argon2 。一些样品将不胜感激。
  • @Fildor 你推荐任何其他代码扫描仪而不是 sonarqube 吗?
  • "如果操作不当,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或将原始数据替换为具有相同哈希值的另一个数据。”最后一部分仅适用于像 MD5 这样的损坏的哈希函数,只是“错误地”应用已知的良好哈希不会让你到达那里。实际上,整个 SonarQube 描述低于标准。但是,是的,在这个用例中使用密码哈希。

标签: c# security cryptography sonarqube md5


【解决方案1】:

您可以尝试在散列密码中添加盐,因为未加盐的散列往往更容易受到字典和彩虹表攻击。

hash("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

hash("hello" + "QxLUF1bgIAdeQX") = 
9e209040c863f84a31e719795b2577523954739fe5ed3b58a75cff2127075ed1

hash("hello" + "bv5PehSMfV11Cd") = 
d1d3ec2e6f20fd420d50e2642992841d8338a314b8ea157c9e18477aaef226ab

上面的代码是加盐通常如何工作的示例。您将固定字符串连接到密码,然后散列结果。盐应该是固定长度的,并且应该永远不要重复使用。这就是您的情况:

        private static string salt = "<REPLACE_WITH_FIXED_LENGTH_SALT>";
        private static string sensitiveKey = "<REPLACE_WITH_KEY>";  
        private static string salted_key = salt + sensitiveKey;

        public static string Encrypt(string input)
        {
            // Get the bytes of the string
            byte[] passwordBytes = Encoding.UTF8.GetBytes(salted_key);
            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
            byte[] bytesEncrypted = EncryptStringToBytes_Aes(input, passwordBytes);
            string result = Convert.ToBase64String(bytesEncrypted);
            return result;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2015-07-05
    • 2016-08-11
    • 2021-10-02
    • 1970-01-01
    • 2021-08-22
    • 2014-03-08
    相关资源
    最近更新 更多