Skip to content

Commit ab2704a

Browse files
Data Protection minor tidying (#31512)
* Remove unused code Remove unused constant, method and variable. * Remove array allocations Use Array.Empty() instead of allocating an empty array. Use a static array for activation type instead of creating a new one per invocation. * Rename field Address PR feedback.
1 parent 4d695c3 commit ab2704a

File tree

5 files changed

+5
-25
lines changed

5 files changed

+5
-25
lines changed

src/DataProtection/DataProtection/src/EphemeralDataProtectionProvider.cs

-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Diagnostics;
66
using System.Runtime.InteropServices;
7-
using System.Runtime.Versioning;
87
using Microsoft.AspNetCore.Cryptography.Cng;
98
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
109
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
@@ -85,9 +84,6 @@ public EphemeralKeyRing(ILoggerFactory loggerFactory)
8584
DefaultAuthenticatedEncryptor = GetDefaultEncryptor(loggerFactory);
8685
}
8786

88-
// Currently hardcoded to a 512-bit KDK.
89-
private const int NUM_BYTES_IN_KDK = 512 / 8;
90-
9187
public IAuthenticatedEncryptor? DefaultAuthenticatedEncryptor { get; }
9288

9389
public Guid DefaultKeyId { get; } = default(Guid);

src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public ManagedAuthenticatedEncryptor(Secret keyDerivationKey, Func<SymmetricAlgo
6969

7070
private byte[] CreateContextHeader()
7171
{
72-
var EMPTY_ARRAY = new byte[0];
72+
var EMPTY_ARRAY = Array.Empty<byte>();
7373
var EMPTY_ARRAY_SEGMENT = new ArraySegment<byte>(EMPTY_ARRAY);
7474

7575
var retVal = new byte[checked(

src/DataProtection/DataProtection/src/SimpleActivator.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Reflection;
65
using Microsoft.AspNetCore.DataProtection.Internal;
76

87
namespace Microsoft.AspNetCore.DataProtection
@@ -13,6 +12,8 @@ namespace Microsoft.AspNetCore.DataProtection
1312
/// </summary>
1413
internal class SimpleActivator : IActivator
1514
{
15+
private static readonly Type[] _serviceProviderTypeArray = { typeof(IServiceProvider) };
16+
1617
/// <summary>
1718
/// A default <see cref="SimpleActivator"/> whose wrapped <see cref="IServiceProvider"/> is null.
1819
/// </summary>
@@ -42,7 +43,7 @@ public virtual object CreateInstance(Type expectedBaseType, string implementatio
4243
}
4344

4445
// If an IServiceProvider was specified or if .ctor() doesn't exist, prefer .ctor(IServiceProvider) [if it exists]
45-
var ctorWhichTakesServiceProvider = implementationType.GetConstructor(new Type[] { typeof(IServiceProvider) });
46+
var ctorWhichTakesServiceProvider = implementationType.GetConstructor(_serviceProviderTypeArray);
4647
if (ctorWhichTakesServiceProvider != null)
4748
{
4849
return ctorWhichTakesServiceProvider.Invoke(new[] { _services });

src/DataProtection/DataProtection/src/XmlEncryption/EncryptedXmlDecryptor.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public XElement Decrypt(XElement encryptedElement)
5959
// doesn't handle encrypting the root element all that well.
6060
var xmlDocument = new XmlDocument();
6161
xmlDocument.Load(new XElement("root", encryptedElement).CreateReader());
62-
var elementToDecrypt = (XmlElement)xmlDocument.DocumentElement!.FirstChild!;
6362

6463
// Perform the decryption and update the document in-place.
6564
var encryptedXml = new EncryptedXmlWithCertificateKeys(_options, xmlDocument);
@@ -68,7 +67,7 @@ public XElement Decrypt(XElement encryptedElement)
6867
encryptedXml.DecryptDocument();
6968

7069
// Strip the <root /> element back off and convert the XmlDocument to an XElement.
71-
return XElement.Load(xmlDocument.DocumentElement.FirstChild!.CreateNavigator()!.ReadSubtree());
70+
return XElement.Load(xmlDocument.DocumentElement!.FirstChild!.CreateNavigator()!.ReadSubtree());
7271
}
7372

7473
void IInternalEncryptedXmlDecryptor.PerformPreDecryptionSetup(EncryptedXml encryptedXml)

src/Shared/WebEncoders/WebEncoders.cs

-16
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,6 @@ private static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
404404
}
405405
#endif
406406

407-
private static int GetNumBase64PaddingCharsInString(string str)
408-
{
409-
// Assumption: input contains a well-formed base64 string with no whitespace.
410-
411-
// base64 guaranteed have 0 - 2 padding characters.
412-
if (str[str.Length - 1] == '=')
413-
{
414-
if (str[str.Length - 2] == '=')
415-
{
416-
return 2;
417-
}
418-
return 1;
419-
}
420-
return 0;
421-
}
422-
423407
private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
424408
{
425409
switch (inputLength % 4)

0 commit comments

Comments
 (0)