Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iot-svc): Cleanup and deprecation warning of code in CryptoKeyGenerator #2187

Merged
merged 5 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 44 additions & 38 deletions iothub/service/src/Common/Security/CryptoKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Linq;
using System;
using System.Text;
using System.Security.Cryptography;

#if NET451 || NET472

using System.Web.Security;

#endif

#if !NET451

using System.Security.Cryptography;
using System.Linq;

#else
using System.Web.Security;
using System.Security.Cryptography;
#endif

namespace Microsoft.Azure.Devices.Common
Expand All @@ -21,9 +24,9 @@ namespace Microsoft.Azure.Devices.Common
/// </summary>
static public class CryptoKeyGenerator
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
{
#if NET451
const int DefaultPasswordLength = 16;
const int GuidLength = 16;
#if NET451 || NET472
private const int DefaultPasswordLength = 16;
private const int GuidLength = 16;
#endif

/// <summary>
Expand All @@ -38,20 +41,16 @@ static public class CryptoKeyGenerator
/// <returns>Byte array representing the key.</returns>
public static byte[] GenerateKeyBytes(int keySize)
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
{
#if !NET451
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
var keyBytes = new byte[keySize];
using (var cyptoProvider = RandomNumberGenerator.Create())
{
while (keyBytes.Contains(byte.MinValue))
{
cyptoProvider.GetBytes(keyBytes);
}
}
#if NET451
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = new RNGCryptoServiceProvider();
cyptoProvider.GetNonZeroBytes(keyBytes);
#else
var keyBytes = new byte[keySize];
using (var cyptoProvider = new RNGCryptoServiceProvider())
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = RandomNumberGenerator.Create();
while (keyBytes.Contains(byte.MinValue))
{
cyptoProvider.GetNonZeroBytes(keyBytes);
cyptoProvider.GetBytes(keyBytes);
}
#endif
return keyBytes;
Expand All @@ -67,19 +66,18 @@ public static string GenerateKey(int keySize)
return Convert.ToBase64String(GenerateKeyBytes(keySize));
}

#if NET451
#if NET451 || NET472
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Generate a hexadecimal key of the specified size.
/// </summary>
/// <param name="keySize">Desired key size.</param>
/// <returns>A generated hexadecimal key.</returns>
/// <returns>A generated hexadecimal key.</returns>
public static string GenerateKeyInHex(int keySize)
{
var keyBytes = new byte[keySize];
using (var cyptoProvider = new RNGCryptoServiceProvider())
{
cyptoProvider.GetNonZeroBytes(keyBytes);
}
byte[] keyBytes = new byte[keySize];
using var cyptoProvider = new RNGCryptoServiceProvider();
cyptoProvider.GetNonZeroBytes(keyBytes);

return BitConverter.ToString(keyBytes).Replace("-", "");
}

Expand All @@ -90,20 +88,28 @@ public static string GenerateKeyInHex(int keySize)
public static Guid GenerateGuid()
{
byte[] bytes = new byte[GuidLength];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(bytes);
}
using var rng = new RNGCryptoServiceProvider();
rng.GetBytes(bytes);

var time = BitConverter.ToUInt32(bytes, 0);
var time_mid = BitConverter.ToUInt16(bytes, 4);
var time_hi_and_ver = BitConverter.ToUInt16(bytes, 6);
time_hi_and_ver = (ushort)((time_hi_and_ver | 0x4000) & 0x4FFF);
uint time = BitConverter.ToUInt32(bytes, 0);
ushort timeMid = BitConverter.ToUInt16(bytes, 4);
ushort timeHiAndVer = BitConverter.ToUInt16(bytes, 6);
timeHiAndVer = (ushort)((timeHiAndVer | 0x4000) & 0x4FFF);

bytes[8] = (byte)((bytes[8] | 0x80) & 0xBF);

return new Guid(time, time_mid, time_hi_and_ver, bytes[8], bytes[9],
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
return new Guid(
time,
timeMid,
timeHiAndVer,
bytes[8],
bytes[9],
bytes[10],
bytes[11],
bytes[12],
bytes[13],
bytes[14],
bytes[15]);
}

/// <summary>
Expand All @@ -123,7 +129,7 @@ public static string GeneratePassword()
/// <returns>A generated password.</returns>
public static string GeneratePassword(int length, bool base64Encoding)
{
var password = Membership.GeneratePassword(length, length / 2);
string password = Membership.GeneratePassword(length, length / 2);
if (base64Encoding)
{
password = Convert.ToBase64String(Encoding.UTF8.GetBytes(password));
Expand Down
1 change: 1 addition & 0 deletions iothub/service/src/Microsoft.Azure.Devices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@

<!-- net472 -->
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Reference Include="System.Web" Version="4.0.0.0" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
<Reference Include="System.Net.Http" />
</ItemGroup>
Expand Down