Skip to content

Commit d7e5993

Browse files
committed
Resolve warnings
HashingAlgorithms left as they were because suggested by the dotnet .Create() is inefficient and with bugs.
1 parent 454b8d1 commit d7e5993

File tree

11 files changed

+91
-70
lines changed

11 files changed

+91
-70
lines changed

Extensions/Xtensive.Orm.Security/Cryptography/GenericHashingService.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.06.10
66

@@ -31,14 +31,7 @@ public abstract class GenericHashingService : IHashingService
3131
/// Gets the salt.
3232
/// </summary>
3333
/// <returns>The salt.</returns>
34-
protected byte[] GetSalt()
35-
{
36-
var salt = new byte[SaltSize];
37-
using (var rng = new RNGCryptoServiceProvider()) {
38-
rng.GetBytes(salt);
39-
return salt;
40-
}
41-
}
34+
protected byte[] GetSalt() => RandomNumberGenerator.GetBytes(SaltSize);
4235

4336
/// <summary>
4437
/// Computes the hash.

Extensions/Xtensive.Orm.Security/Cryptography/MD5HashingService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.05.22
66

@@ -17,10 +17,10 @@ namespace Xtensive.Orm.Security.Cryptography
1717
public class MD5HashingService : GenericHashingService
1818
{
1919
/// <inheritdoc/>
20-
protected override HashAlgorithm GetHashAlgorithm()
21-
{
22-
return new MD5CryptoServiceProvider();
23-
}
20+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
21+
// direct creation is more efficient than MD5.Create()
22+
protected override HashAlgorithm GetHashAlgorithm() => new MD5CryptoServiceProvider();
23+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
2424

2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="MD5HashingService"/> class.

Extensions/Xtensive.Orm.Security/Cryptography/SHA1HashingService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.05.22
66

@@ -16,10 +16,10 @@ namespace Xtensive.Orm.Security.Cryptography
1616
public class SHA1HashingService : GenericHashingService
1717
{
1818
/// <inheritdoc/>
19-
protected override HashAlgorithm GetHashAlgorithm()
20-
{
21-
return new SHA1Managed();
22-
}
19+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
20+
// direct creation is more efficient than SHA1.Create()
21+
protected override HashAlgorithm GetHashAlgorithm() => new SHA1Managed();
22+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="SHA1HashingService"/> class.

Extensions/Xtensive.Orm.Security/Cryptography/SHA256HashingService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.05.22
66

@@ -16,10 +16,10 @@ namespace Xtensive.Orm.Security.Cryptography
1616
public class SHA256HashingService : GenericHashingService
1717
{
1818
/// <inheritdoc/>
19-
protected override HashAlgorithm GetHashAlgorithm()
20-
{
21-
return new SHA256Managed();
22-
}
19+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
20+
// direct creation is more efficient than SHA256.Create()
21+
protected override HashAlgorithm GetHashAlgorithm() => new SHA256Managed();
22+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="SHA256HashingService"/> class.

Extensions/Xtensive.Orm.Security/Cryptography/SHA384HashingService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.05.22
66

@@ -16,10 +16,10 @@ namespace Xtensive.Orm.Security.Cryptography
1616
public class SHA384HashingService : GenericHashingService
1717
{
1818
/// <inheritdoc/>
19-
protected override HashAlgorithm GetHashAlgorithm()
20-
{
21-
return new SHA384Managed();
22-
}
19+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
20+
// direct creation is more efficient than SHA384.Create()
21+
protected override HashAlgorithm GetHashAlgorithm() => new SHA384Managed();
22+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="SHA384HashingService"/> class.

Extensions/Xtensive.Orm.Security/Cryptography/SHA512HashingService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2011.05.22
66

@@ -16,10 +16,10 @@ namespace Xtensive.Orm.Security.Cryptography
1616
public class SHA512HashingService : GenericHashingService
1717
{
1818
/// <inheritdoc/>
19-
protected override HashAlgorithm GetHashAlgorithm()
20-
{
21-
return new SHA512Managed();
22-
}
19+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
20+
// direct creation is more efficient than SHA512.Create()
21+
protected override HashAlgorithm GetHashAlgorithm() => new SHA512Managed();
22+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="SHA512HashingService"/> class.

Orm/Xtensive.Orm.Sqlite/Sql.Drivers.Sqlite/ProviderInitializer.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2012 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2012-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2012.08.21
66

@@ -53,13 +53,17 @@ private static Stream GetLibraryStream()
5353

5454
private static string GetLibraryHash()
5555
{
56-
using (var hashProvider = new SHA1Managed()) {
57-
hashProvider.Initialize();
58-
using (var stream = GetLibraryStream())
59-
hashProvider.ComputeHash(stream);
60-
var hash = hashProvider.Hash.Take(8).ToArray();
61-
return new StringBuilder().AppendHexArray(hash).ToString();
56+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
57+
// direct creation is more efficient than SHA1.Create()
58+
using (var hashProvider = new System.Security.Cryptography.SHA1Managed()) {
59+
//hashProvider.Initialize();
60+
ReadOnlySpan<byte> hashRaw;
61+
using (var stream = GetLibraryStream()) {
62+
hashRaw = hashProvider.ComputeHash(stream);
63+
}
64+
return new StringBuilder().AppendHexArray(hashRaw[..8]).ToString();
6265
}
66+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
6367
}
6468

6569
private static string GetLibraryFileName(string nativeLibraryCacheFolder, string moduleHash)

Orm/Xtensive.Orm.Tests/Storage/Randomized/RandomizedTest.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alexander Nikolaev
55
// Created: 2009.11.26
66

@@ -28,6 +28,7 @@ public sealed class RandomizedTest : AutoBuildTest
2828
private Random randomProvider;
2929
private bool isSettingUp;
3030
private readonly List<Key> entitySetCache = new List<Key>();
31+
3132
protected override DomainConfiguration BuildConfiguration()
3233
{
3334
var config = base.BuildConfiguration();
@@ -265,8 +266,9 @@ private void ThrowOrCompleteTransaction(TransactionScope tx)
265266

266267
private static int GetSeed()
267268
{
268-
var bytes = new byte[sizeof (int)];
269-
var seedProvider = new RNGCryptoServiceProvider();
269+
var bytes = new byte[sizeof(int)];
270+
271+
using var seedProvider = RandomNumberGenerator.Create();
270272
seedProvider.GetNonZeroBytes(bytes);
271273
return BitConverter.ToInt32(bytes, 0);
272274
}

Orm/Xtensive.Orm/Core/Extensions/StringBuilderExtensions.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2008-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alex Kofman
55
// Created: 2008.07.30
66

@@ -81,7 +81,27 @@ public static StringBuilder AppendHexArray(this StringBuilder builder, byte[] va
8181

8282
const string lowerHexChars = "0123456789abcdef";
8383
foreach (var item in values) {
84-
builder.Append(lowerHexChars[item >> 4])
84+
_ = builder.Append(lowerHexChars[item >> 4])
85+
.Append(lowerHexChars[item & 0xF]);
86+
}
87+
return builder;
88+
}
89+
90+
/// <summary>
91+
/// Appends the specified <see cref="byte"/> array in hexidecimal representation in lower case.
92+
/// These bytes are written from left to right, high part of byte is written first.
93+
/// For example {1,2,10} will be appended as 01020a.
94+
/// </summary>
95+
/// <param name="builder">The builder.</param>
96+
/// <param name="values">The values.</param>
97+
/// <returns>Original <paramref name="builder"/>.</returns>
98+
public static StringBuilder AppendHexArray(this StringBuilder builder, in ReadOnlySpan<byte> values)
99+
{
100+
ArgumentValidator.EnsureArgumentNotNull(builder, "builder");
101+
102+
const string lowerHexChars = "0123456789abcdef";
103+
foreach (var item in values) {
104+
_ = builder.Append(lowerHexChars[item >> 4])
85105
.Append(lowerHexChars[item & 0xF]);
86106
}
87107
return builder;

Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2007-2020 Xtensive LLC.
1+
// Copyright (C) 2007-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
@@ -577,10 +577,12 @@ public string ApplyNamingRules(string name)
577577
/// <returns>Computed hash.</returns>
578578
private static string GetHash(string name)
579579
{
580+
#pragma warning disable SYSLIB0021 // Type or member is obsolete
580581
using (var hashAlgorithm = new MD5CryptoServiceProvider()) {
581-
byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(name));
582+
var hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(name));
582583
return $"H{hash[0]:x2}{hash[1]:x2}{hash[2]:x2}{hash[3]:x2}";
583584
}
585+
#pragma warning restore SYSLIB0021 // Type or member is obsolete
584586
}
585587

586588
private static string FormatKeyGeneratorName(string database, string name)

0 commit comments

Comments
 (0)