简体   繁体   English

SHA256 是否被认为不安全?- SonarQube 质量门 - 我该如何解决?

[英]Is SHA256 considered insecure?- SonarQube Quality Gate - How can I fix it?

I have below piece of code我有下面的代码

        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 says SonarQube 说

Cryptographic hash functions are used to uniquely identify information without storing their original form.加密 hash 函数用于唯一标识信息而不存储其原始形式。 When not done properly, an attacker can steal the original information by guessing it (ex: with a rainbow table), or replace the original data with another one having the same hash.如果做得不好,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或者用另一个具有相同 hash 的数据替换原始数据。

Also

use only hashing algorithms which are currently known to be strong.仅使用目前已知的强大的散列算法。 Avoid using algorithms like MD5 and SHA1 completely in security contexts.避免在安全上下文中完全使用MD5SHA1等算法。

So question is, How can I improve my code so that it can be secure?所以问题是,我怎样才能改进我的代码以使其安全?

Any samples out thr?有样品吗?

You could try adding a salt to the hashed password since unsalted hashes tend to be more vulnerable to dictionary and rainbow table attacks.您可以尝试在散列密码中添加盐,因为未加盐的散列往往更容易受到字典和彩虹表攻击。

hash("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

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

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

The code above is an example of how salting usually works.上面的代码是加盐通常如何工作的示例。 You concatenate a fixed string to the password and then hash the result.您将固定字符串连接到密码,然后 hash 结果。 The salt should be of fixed length and should never be reused .盐应该是固定长度的,并且永远不应该被重复使用 This is how it could look like in your case:这就是您的情况:

        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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM