Dotnet Core – 如何处理 RNGCryptoServiceProvider()

huangapple go评论93阅读模式
英文:

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$V1
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$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$V1
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$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

huangapple
  • 本文由 发表于 2020年1月6日 21:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定