-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix test references by making a local copy for tests
- Loading branch information
David R. Williamson
committed
Oct 4, 2021
1 parent
e1d3f7c
commit e92a0dd
Showing
2 changed files
with
62 additions
and
2 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