英文:
Dotnet Core - How to dispose of RNGCryptoServiceProvider()
问题
我收到了警告:
严重性	Code	描述	项目	文件	行	抑制状态
警告	CA2000	在所有引用超出范围之前调用System.IDisposable.Dispose对由'new RNGCryptoServiceProvider()'创建的对象。	JobsLedger.AUTHORISATION	C:\Users\simon\OneDrive\Documents\1.0 - AURELIA\1.0 - JobsLedgerSPA -ASPNET CORE 3.1\JobsLedger.AUTHORISATION\CryptoService.cs	96	活动
触发此警告的代码位于以下行:
new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
它的用法如下:
public string EncryptdatabaseName(string text)
{
byte[] salt;
new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
using var aesAlg = Aes.Create();
using var encryptor = aesAlg.CreateEncryptor(salt, aesAlg.IV);
using var msEncrypt = new MemoryStream();
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var swEncrypt = new StreamWriter(csEncrypt))
{
    swEncrypt.Write(text);
}
var iv = aesAlg.IV;
var decryptedContent = msEncrypt.ToArray();
var encryptedDatabaseName = new byte[iv.Length + decryptedContent.Length];
Buffer.BlockCopy(iv, 0, encryptedDatabaseName, 0, iv.Length);
Buffer.BlockCopy(decryptedContent, 0, encryptedDatabaseName, iv.Length, decryptedContent.Length);
// 使用额外信息格式化哈希
return string.Format(CultureInfo.CurrentCulture, "$DATABASENAME$V1using var aesAlg = Aes.Create();
using var encryptor = aesAlg.CreateEncryptor(salt, aesAlg.IV);
using var msEncrypt = new MemoryStream();
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(text);
}
var iv = aesAlg.IV;
var decryptedContent = msEncrypt.ToArray();
var encryptedDatabaseName = new byte[iv.Length + decryptedContent.Length];
Buffer.BlockCopy(iv, 0, encryptedDatabaseName, 0, iv.Length);
Buffer.BlockCopy(decryptedContent, 0, encryptedDatabaseName, iv.Length, decryptedContent.Length);
// 使用额外信息格式化哈希
return string.Format(CultureInfo.CurrentCulture, "$DATABASENAME$V1${0}${1}", Convert.ToBase64String(encryptedDatabaseName), salt);
", Convert.ToBase64String(encryptedDatabaseName), salt);
}
我正在使用Visual Studio 2019,是Roslyn编译器引发了这个警告。我想要摆脱这个警告。
如何处置这个问题?
英文:
I am getting the warning:
Severity	Code	Description	Project	File	Line	Suppression State
Warning	CA2000	Call System.IDisposable.Dispose on object created by 'new RNGCryptoServiceProvider()' before all references to it are out of scope.	JobsLedger.AUTHORISATION	C:\Users\simon\OneDrive\Documents.0 - AURELIA.0 - JobsLedgerSPA -ASPNET CORE 3.1\JobsLedger.AUTHORISATION\CryptoService.cs	96	Active
The code this warning is occuring is on this line:
new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
Its used as follows:
public string EncryptdatabaseName(string text)
        {
            byte[] salt;
            new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
            
            using var aesAlg = Aes.Create();
            using var encryptor = aesAlg.CreateEncryptor(salt, aesAlg.IV);
            using var msEncrypt = new MemoryStream();
            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            using (var swEncrypt = new StreamWriter(csEncrypt))
            {
                swEncrypt.Write(text);
            }
            var iv = aesAlg.IV;
            var decryptedContent = msEncrypt.ToArray();
            var encryptedDatabaseName = new byte[iv.Length + decryptedContent.Length];
            Buffer.BlockCopy(iv, 0, encryptedDatabaseName, 0, iv.Length);
            Buffer.BlockCopy(decryptedContent, 0, encryptedDatabaseName, iv.Length, decryptedContent.Length);
            // Format hash with extra information
            return string.Format(CultureInfo.CurrentCulture, "$DATABASENAME$V1public string EncryptdatabaseName(string text)
{
byte[] salt;
new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
using var aesAlg = Aes.Create();
using var encryptor = aesAlg.CreateEncryptor(salt, aesAlg.IV);
using var msEncrypt = new MemoryStream();
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(text);
}
var iv = aesAlg.IV;
var decryptedContent = msEncrypt.ToArray();
var encryptedDatabaseName = new byte[iv.Length + decryptedContent.Length];
Buffer.BlockCopy(iv, 0, encryptedDatabaseName, 0, iv.Length);
Buffer.BlockCopy(decryptedContent, 0, encryptedDatabaseName, iv.Length, decryptedContent.Length);
// Format hash with extra information
return string.Format(CultureInfo.CurrentCulture, "$DATABASENAME$V1${0}${1}", Convert.ToBase64String(encryptedDatabaseName), salt);
}
", Convert.ToBase64String(encryptedDatabaseName), salt);
        }
I am using Visual Studio 2019 and its the Roslyn compiler that is throwing this up.. I want to get rid of the warning..
How do I dispose of this?
答案1
得分: 2
The CA2000 means “Dispose objects before losing scope”. From official document, you could try this code:
使用(RNGCryptoServiceProvider test = new RNGCryptoServiceProvider())
{
test.GetBytes(salt = new byte[SaltSize]);
}
如果你想直接抑制它,你可以使用这段代码,类似于:
#pragma warning disable CA1062
new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
#pragma warning restore CA1062
英文:
The CA2000 means “Dispose objects before losing scope”. From official document, you could try this code:
using(RNGCryptoServiceProvider test = new RNGCryptoServiceProvider())
{
   test.GetBytes(salt = new byte[SaltSize]);
}
If you want to suppress it directly, you could use this code, which is like:
#pragma warning disable CA1062
        new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);
#pragma warning restore CA1062
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论