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

Improve Create #151

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions AesExtra/AesCmac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MIT

using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;

namespace Dorssel.Security.Cryptography;
Expand All @@ -14,18 +15,21 @@ public sealed class AesCmac
{
const int BLOCKSIZE = 16; // bytes

/// <inheritdoc cref="KeyedHashAlgorithm.Create()" />
/// <remarks>This static override defaults to <see cref="AesCmac" />.</remarks>
public static new KeyedHashAlgorithm Create()
/// <inheritdoc cref="KeyedHashAlgorithm.Create()" path="/summary" />
/// <returns>A new <see cref="AesCmac" /> instance.</returns>
public static new AesCmac Create()
{
return new AesCmac();
}

/// <inheritdoc cref="KeyedHashAlgorithm.Create(string)" />
[Obsolete("Cryptographic factory methods accepting an algorithm name are obsolete. Use the parameterless Create factory method on the algorithm type instead.")]
#if !NETSTANDARD2_0
[RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
#endif
public static new KeyedHashAlgorithm? Create(string algorithmName)
{
return algorithmName != null ? algorithmName == nameof(AesCmac) ? Create() : null
: throw new ArgumentNullException(nameof(algorithmName));
return algorithmName == nameof(AesCmac) ? Create() : KeyedHashAlgorithm.Create(algorithmName);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions AesExtra/AesCtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MIT

using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;

namespace Dorssel.Security.Cryptography;
Expand All @@ -25,10 +26,13 @@ public sealed class AesCtr
}

/// <inheritdoc cref="Aes.Create(string)" />
[Obsolete("Cryptographic factory methods accepting an algorithm name are obsolete. Use the parameterless Create factory method on the algorithm type instead.")]
#if !NETSTANDARD2_0
[RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
#endif
public static new Aes? Create(string algorithmName)
{
return algorithmName != null ? algorithmName == nameof(AesCtr) ? Create() : null
: throw new ArgumentNullException(nameof(algorithmName));
return algorithmName == nameof(AesCtr) ? Create() : Aes.Create(algorithmName);
}

AesCtr()
Expand Down
6 changes: 6 additions & 0 deletions UnitTests/AesCmac_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void Create()
[TestMethod]
public void Create_Name()
{
#pragma warning disable CS0618 // Type or member is obsolete
using var keyedHashAlgorithm = AesCmac.Create("AesCmac");
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNotNull(keyedHashAlgorithm);
}

Expand All @@ -26,14 +28,18 @@ public void Create_NullNameFails()
{
Assert.ThrowsException<ArgumentNullException>(() =>
{
#pragma warning disable CS0618 // Type or member is obsolete
using var keyedHashAlgorithm = AesCmac.Create(null!);
#pragma warning restore CS0618 // Type or member is obsolete
});
}

[TestMethod]
public void Create_OtherNameReturnsNull()
{
#pragma warning disable CS0618 // Type or member is obsolete
using var keyedHashAlgorithm = AesCmac.Create("SomeOtherName");
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNull(keyedHashAlgorithm);
}

Expand Down
6 changes: 6 additions & 0 deletions UnitTests/AesCtr_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void Create()
[TestMethod]
public void Create_Name()
{
#pragma warning disable CS0618 // Type or member is obsolete
using var aes = AesCtr.Create("AesCtr");
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNotNull(aes);
}

Expand All @@ -41,14 +43,18 @@ public void Create_NullNameFails()
{
Assert.ThrowsException<ArgumentNullException>(() =>
{
#pragma warning disable CS0618 // Type or member is obsolete
using var aes = AesCtr.Create(null!);
#pragma warning restore CS0618 // Type or member is obsolete
});
}

[TestMethod]
public void Create_OtherNameReturnsNull()
{
#pragma warning disable CS0618 // Type or member is obsolete
using var aes = AesCtr.Create("SomeOtherName");
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNull(aes);
}

Expand Down
Loading