-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(iot-svc): Cleanup and deprecation warning of code in CryptoKeyGen…
…erator (#2187) * fix(iot-svc): Add support for GeneratePassword to other targets * Don't add net472 support, but keep code cleanup * Add deprecated attribute * Fix test references by making a local copy for tests
- Loading branch information
David R. Williamson
authored
Oct 4, 2021
1 parent
57629ff
commit caf7850
Showing
5 changed files
with
174 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Security.Cryptography; | ||
|
||
#if !NET451 | ||
|
||
using System.Linq; | ||
|
||
#endif | ||
|
||
namespace Microsoft.Azure.Devices.E2ETests | ||
{ | ||
/// <summary> | ||
/// Utility methods for generating cryptographically secure keys and passwords. | ||
/// </summary> | ||
internal static class CryptoKeyGenerator | ||
{ | ||
#if NET451 | ||
private const int DefaultPasswordLength = 16; | ||
private const int GuidLength = 16; | ||
#endif | ||
|
||
/// <summary> | ||
/// Size of the SHA 512 key. | ||
/// </summary> | ||
internal const int Sha512KeySize = 64; | ||
|
||
/// <summary> | ||
/// Generate a key with a specified key size. | ||
/// </summary> | ||
/// <param name="keySize">The size of the key.</param> | ||
/// <returns>Byte array representing the key.</returns> | ||
internal static byte[] GenerateKeyBytes(int keySize) | ||
{ | ||
#if NET451 | ||
byte[] keyBytes = new byte[keySize]; | ||
using var cyptoProvider = new RNGCryptoServiceProvider(); | ||
cyptoProvider.GetNonZeroBytes(keyBytes); | ||
#else | ||
byte[] keyBytes = new byte[keySize]; | ||
using var cyptoProvider = RandomNumberGenerator.Create(); | ||
while (keyBytes.Contains(byte.MinValue)) | ||
{ | ||
cyptoProvider.GetBytes(keyBytes); | ||
} | ||
#endif | ||
return keyBytes; | ||
} | ||
|
||
/// <summary> | ||
/// Generates a key of the specified size. | ||
/// </summary> | ||
/// <param name="keySize">Desired key size.</param> | ||
/// <returns>A generated key.</returns> | ||
internal static string GenerateKey(int keySize) | ||
{ | ||
return Convert.ToBase64String(GenerateKeyBytes(keySize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Security.Cryptography; | ||
|
||
#if !NET451 | ||
|
||
using System.Linq; | ||
|
||
#endif | ||
|
||
namespace Microsoft.Azure.Devices.Tests | ||
{ | ||
/// <summary> | ||
/// Utility methods for generating cryptographically secure keys and passwords. | ||
/// </summary> | ||
internal static class CryptoKeyGenerator | ||
{ | ||
#if NET451 | ||
private const int DefaultPasswordLength = 16; | ||
private const int GuidLength = 16; | ||
#endif | ||
|
||
/// <summary> | ||
/// Size of the SHA 512 key. | ||
/// </summary> | ||
internal const int Sha512KeySize = 64; | ||
|
||
/// <summary> | ||
/// Generate a key with a specified key size. | ||
/// </summary> | ||
/// <param name="keySize">The size of the key.</param> | ||
/// <returns>Byte array representing the key.</returns> | ||
internal static byte[] GenerateKeyBytes(int keySize) | ||
{ | ||
#if NET451 | ||
byte[] keyBytes = new byte[keySize]; | ||
using var cyptoProvider = new RNGCryptoServiceProvider(); | ||
cyptoProvider.GetNonZeroBytes(keyBytes); | ||
#else | ||
byte[] keyBytes = new byte[keySize]; | ||
using var cyptoProvider = RandomNumberGenerator.Create(); | ||
while (keyBytes.Contains(byte.MinValue)) | ||
{ | ||
cyptoProvider.GetBytes(keyBytes); | ||
} | ||
#endif | ||
return keyBytes; | ||
} | ||
|
||
/// <summary> | ||
/// Generates a key of the specified size. | ||
/// </summary> | ||
/// <param name="keySize">Desired key size.</param> | ||
/// <returns>A generated key.</returns> | ||
internal static string GenerateKey(int keySize) | ||
{ | ||
return Convert.ToBase64String(GenerateKeyBytes(keySize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters