Skip to content

Commit

Permalink
Merge pull request #151 from dorssel/improve_create
Browse files Browse the repository at this point in the history
Improve Create
  • Loading branch information
dorssel authored Jan 11, 2025
2 parents 87ea27f + 13a36f1 commit d2f3d37
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
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

0 comments on commit d2f3d37

Please sign in to comment.