Skip to content

Commit

Permalink
Data Protection minor tidying (#31512)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
martincostello committed Apr 6, 2021
1 parent 4d695c3 commit ab2704a
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.AspNetCore.Cryptography.Cng;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
Expand Down Expand Up @@ -85,9 +84,6 @@ public EphemeralKeyRing(ILoggerFactory loggerFactory)
DefaultAuthenticatedEncryptor = GetDefaultEncryptor(loggerFactory);
}

// Currently hardcoded to a 512-bit KDK.
private const int NUM_BYTES_IN_KDK = 512 / 8;

public IAuthenticatedEncryptor? DefaultAuthenticatedEncryptor { get; }

public Guid DefaultKeyId { get; } = default(Guid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ManagedAuthenticatedEncryptor(Secret keyDerivationKey, Func<SymmetricAlgo

private byte[] CreateContextHeader()
{
var EMPTY_ARRAY = new byte[0];
var EMPTY_ARRAY = Array.Empty<byte>();
var EMPTY_ARRAY_SEGMENT = new ArraySegment<byte>(EMPTY_ARRAY);

var retVal = new byte[checked(
Expand Down
5 changes: 3 additions & 2 deletions src/DataProtection/DataProtection/src/SimpleActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;
using Microsoft.AspNetCore.DataProtection.Internal;

namespace Microsoft.AspNetCore.DataProtection
Expand All @@ -13,6 +12,8 @@ namespace Microsoft.AspNetCore.DataProtection
/// </summary>
internal class SimpleActivator : IActivator
{
private static readonly Type[] _serviceProviderTypeArray = { typeof(IServiceProvider) };

/// <summary>
/// A default <see cref="SimpleActivator"/> whose wrapped <see cref="IServiceProvider"/> is null.
/// </summary>
Expand Down Expand Up @@ -42,7 +43,7 @@ public virtual object CreateInstance(Type expectedBaseType, string implementatio
}

// If an IServiceProvider was specified or if .ctor() doesn't exist, prefer .ctor(IServiceProvider) [if it exists]
var ctorWhichTakesServiceProvider = implementationType.GetConstructor(new Type[] { typeof(IServiceProvider) });
var ctorWhichTakesServiceProvider = implementationType.GetConstructor(_serviceProviderTypeArray);
if (ctorWhichTakesServiceProvider != null)
{
return ctorWhichTakesServiceProvider.Invoke(new[] { _services });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public XElement Decrypt(XElement encryptedElement)
// doesn't handle encrypting the root element all that well.
var xmlDocument = new XmlDocument();
xmlDocument.Load(new XElement("root", encryptedElement).CreateReader());
var elementToDecrypt = (XmlElement)xmlDocument.DocumentElement!.FirstChild!;

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

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

void IInternalEncryptedXmlDecryptor.PerformPreDecryptionSetup(EncryptedXml encryptedXml)
Expand Down
16 changes: 0 additions & 16 deletions src/Shared/WebEncoders/WebEncoders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,6 @@ private static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
}
#endif

private static int GetNumBase64PaddingCharsInString(string str)
{
// Assumption: input contains a well-formed base64 string with no whitespace.

// base64 guaranteed have 0 - 2 padding characters.
if (str[str.Length - 1] == '=')
{
if (str[str.Length - 2] == '=')
{
return 2;
}
return 1;
}
return 0;
}

private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
{
switch (inputLength % 4)
Expand Down

0 comments on commit ab2704a

Please sign in to comment.