Skip to content

[FEATURE] Add SHA3 algorithms for .NET 8+#901

Merged
guibranco merged 1 commit into
guibranco:mainfrom
christopher-conley:net8-hash-algorithms
Apr 21, 2026
Merged

[FEATURE] Add SHA3 algorithms for .NET 8+#901
guibranco merged 1 commit into
guibranco:mainfrom
christopher-conley:net8-hash-algorithms

Conversation

@christopher-conley

@christopher-conley christopher-conley commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

πŸ“‘ Description

Introduce SHA3_256, SHA3_384 and SHA3_512 enum members to HashAlgorithmType, guarded by the NET8_0_OR_GREATER preprocessor symbol, and register their corresponding SHA3 implementations in CrispyWaffle.Cryptography.Security's algorithm map, also guarded by the NET8_0_OR_GREATER preprocessor symbol.

This PR selectively enables the use of .NET 8+ built-in SHA3 hash algorithms when targeting .NET 8 or later, while still compiling correctly for older supported frameworks.

βœ… Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

☒️ Does this introduce a breaking change?

  • Yes
  • No

β„Ή Additional Information

All added code is wrapped within a C# preprocessor #if NET8_0_OR_GREATER block, so this PR is backward-compatible with existing code, regardless of the target framework. Existing code targeting .NET 7 or below requires no changes; the new hash algorithms simply won't be available on those frameworks, nor appear in IntelliSense.

Summary by Sourcery

Add support for SHA3 hash algorithms when targeting .NET 8 or later.

New Features:

  • Introduce SHA3_256, SHA3_384, and SHA3_512 options in the HashAlgorithmType enum for .NET 8+ targets.
  • Register .NET 8+ built-in SHA3 implementations in the cryptography algorithm map to enable hashing with these algorithms.

Summary by CodeRabbit

  • New Features
    • Added support for SHA-3 cryptographic hash algorithms (256-bit, 384-bit, and 512-bit variants) for .NET 8 or later.

Introduce SHA3-256, SHA3-384 and SHA3-512 enum members to HashAlgorithmType under NET8_0_OR_GREATER and register their corresponding SHA3 implementations in Security's algorithm map. This enables use of .NET 8+ built-in SHA3 hashing when targeting .NET 8 or later.
@sourcery-ai

sourcery-ai Bot commented Apr 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

.NET 8+ only support for SHA3 hash algorithms is added by extending the HashAlgorithmType enum and wiring those new enum values into the Security hash algorithm map, all under NET8_0_OR_GREATER preprocessor guards for backward compatibility.

Sequence diagram for Security.Hash using SHA3 under .NET 8+

sequenceDiagram
    actor Developer
    participant Security
    participant HashAlgorithmMap
    participant SHA3_256

    Developer->>Security: Hash(value, Sha3_256)
    Security->>HashAlgorithmMap: lookup Sha3_256
    HashAlgorithmMap-->>Security: SHA3_256 instance
    Security->>SHA3_256: ComputeHash(value bytes)
    SHA3_256-->>Security: hash bytes
    Security-->>Developer: hex string hash
Loading

Class diagram for updated hash types and Security mapping

classDiagram
    class HashAlgorithmType {
        <<enumeration>>
        Md5
        Sha1
        Sha256
        Sha384
        Sha512
        Sha3_256
        Sha3_384
        Sha3_512
    }

    class Security {
        +static string Hash(string value, HashAlgorithmType type)
        -static readonly Dictionary<HashAlgorithmType, HashAlgorithm> _algorithms
    }

    class SHA3_256 {
        +static SHA3_256 Create()
    }

    class SHA3_384 {
        +static SHA3_384 Create()
    }

    class SHA3_512 {
        +static SHA3_512 Create()
    }

    Security --> HashAlgorithmType : uses
    Security --> SHA3_256 : maps Sha3_256
    Security --> SHA3_384 : maps Sha3_384
    Security --> SHA3_512 : maps Sha3_512
Loading

File-Level Changes

Change Details Files
Extend the hash algorithm enum with SHA3 members conditionally for .NET 8+.
  • Add Sha3_256, Sha3_384, and Sha3_512 enum members with XML documentation comments
  • Guard the new enum members with the NET8_0_OR_GREATER preprocessor symbol to avoid exposing them on earlier target frameworks
Src/CrispyWaffle/Cryptography/HashAlgorithmType.cs
Register .NET 8+ SHA3 implementations in the cryptography algorithm map.
  • Extend the static hash algorithm dictionary to map the new SHA3 enum values to SHA3_256.Create(), SHA3_384.Create(), and SHA3_512.Create()
  • Guard the new dictionary entries with the NET8_0_OR_GREATER preprocessor symbol so the map remains unchanged on older frameworks
Src/CrispyWaffle/Cryptography/Security.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gstraccini
gstraccini Bot requested a review from guibranco April 21, 2026 12:32
@gstraccini gstraccini Bot added the 🚦 awaiting triage Items that are awaiting triage or categorization label Apr 21, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • If HashAlgorithmType values are ever serialized or persisted, consider assigning explicit numeric values to the new SHA3 entries, as the #if NET8_0_OR_GREATER block will change the enum layout between target frameworks.
  • It may be safer to guard the SHA3 registrations not only with NET8_0_OR_GREATER but also with a runtime check (e.g., CryptoConfig.AllowOnlyFipsAlgorithms or platform capability) in case some .NET 8+ platforms do not support these algorithms.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- If `HashAlgorithmType` values are ever serialized or persisted, consider assigning explicit numeric values to the new SHA3 entries, as the `#if NET8_0_OR_GREATER` block will change the enum layout between target frameworks.
- It may be safer to guard the SHA3 registrations not only with `NET8_0_OR_GREATER` but also with a runtime check (e.g., `CryptoConfig.AllowOnlyFipsAlgorithms` or platform capability) in case some .NET 8+ platforms do not support these algorithms.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click πŸ‘ or πŸ‘Ž on each comment and I'll use the feedback to improve your reviews.

@coderabbitai

coderabbitai Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The cryptography module is extended to support SHA-3 hashing algorithms (256, 384, and 512-bit variants) conditionally for .NET 8 or later. New enum members are added to HashAlgorithmType, and the internal hash algorithm registry is updated to map these types to their corresponding implementations.

Changes

Cohort / File(s) Summary
SHA-3 Algorithm Support
Src/CrispyWaffle/Cryptography/HashAlgorithmType.cs, Src/CrispyWaffle/Cryptography/Security.cs
Added three new SHA-3 enum members (Sha3_256, Sha3_384, Sha3_512) to HashAlgorithmType and registered their corresponding algorithm implementations in the _hashAlgorithms dictionary, both conditionally for .NET 8 or later.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Hop hop, the hashes grow,
SHA-3 algorithms now can flow,
With dotnet 8, three new ways,
To cipher secrets through our days! ✨

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly and specifically summarizes the main change: adding SHA3 hash algorithm support conditionally for .NET 8+, which matches the primary objective of the pull request.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check βœ… Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check βœ… Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
Src/CrispyWaffle/Cryptography/Security.cs (1)

153-165: ⚠️ Potential issue | πŸ”΄ Critical

Critical: SHA3_*.Create() throws on platforms without SHA-3 support, breaking access to the entire Security class.

On .NET 8, SHA3_256/384/512 are only supported on platforms with adequate crypto library support (e.g., Windows 11 build 26016+, OpenSSL 1.1.1+). On unsupported platforms, SHA3_xxx.Create() throws PlatformNotSupportedException.

Since _hashAlgorithms is a static readonly field initializer, a single failing Create() call will throw TypeInitializationException when Security is first accessedβ€”rendering all hashing (MD5/SHA1/SHA2) and all other static members of the class unusable on those platforms. This is a runtime regression for any consumer targeting .NET 8 on a system without SHA-3 support.

Gate each registration with the corresponding IsSupported probe so unsupported variants are simply omitted, matching the existing behavior of throwing ArgumentOutOfRangeException on line 129 for unknown types:

πŸ›‘οΈ Proposed fix
     private static readonly Dictionary<HashAlgorithmType, HashAlgorithm> _hashAlgorithms = new()
     {
         { HashAlgorithmType.Md5, MD5.Create() },
         { HashAlgorithmType.Sha1, SHA1.Create() },
         { HashAlgorithmType.Sha256, SHA256.Create() },
         { HashAlgorithmType.Sha384, SHA384.Create() },
         { HashAlgorithmType.Sha512, SHA512.Create() },
-#if NET8_0_OR_GREATER
-        { HashAlgorithmType.Sha3_256, SHA3_256.Create() },
-        { HashAlgorithmType.Sha3_384, SHA3_384.Create() },
-        { HashAlgorithmType.Sha3_512, SHA3_512.Create() },
-#endif
     };
+
+#if NET8_0_OR_GREATER
+    static Security()
+    {
+        if (SHA3_256.IsSupported)
+        {
+            _hashAlgorithms[HashAlgorithmType.Sha3_256] = SHA3_256.Create();
+        }
+        if (SHA3_384.IsSupported)
+        {
+            _hashAlgorithms[HashAlgorithmType.Sha3_384] = SHA3_384.Create();
+        }
+        if (SHA3_512.IsSupported)
+        {
+            _hashAlgorithms[HashAlgorithmType.Sha3_512] = SHA3_512.Create();
+        }
+    }
+#endif

Alternatively, catch PlatformNotSupportedException at the call site, or document clearly that SHA-3 operations on unsupported platforms will throw.

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Src/CrispyWaffle/Cryptography/Security.cs` around lines 153 - 165, The static
initializer for _hashAlgorithms calls SHA3_256.Create/ SHA3_384.Create/
SHA3_512.Create unconditionally, which can throw PlatformNotSupportedException
on some .NET 8 platforms and causes TypeInitializationException for the Security
class; change the initializer to only add HashAlgorithmType.Sha3_256 / Sha3_384
/ Sha3_512 entries when their corresponding SHA3_*.IsSupported (or wrap each
Create() in a try/catch for PlatformNotSupportedException and skip registration
on failure), so the _hashAlgorithms dictionary is built safely without throwing
and retains MD5/SHA1/SHA2 functionality on platforms that don't support SHA-3.
πŸ€– Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@Src/CrispyWaffle/Cryptography/Security.cs`:
- Around line 153-165: The static initializer for _hashAlgorithms calls
SHA3_256.Create/ SHA3_384.Create/ SHA3_512.Create unconditionally, which can
throw PlatformNotSupportedException on some .NET 8 platforms and causes
TypeInitializationException for the Security class; change the initializer to
only add HashAlgorithmType.Sha3_256 / Sha3_384 / Sha3_512 entries when their
corresponding SHA3_*.IsSupported (or wrap each Create() in a try/catch for
PlatformNotSupportedException and skip registration on failure), so the
_hashAlgorithms dictionary is built safely without throwing and retains
MD5/SHA1/SHA2 functionality on platforms that don't support SHA-3.

ℹ️ Review info
βš™οΈ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 33aa5677-00c7-4ec2-9598-3999319a6f3f

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 6888d6d and 5f2d792.

πŸ“’ Files selected for processing (2)
  • Src/CrispyWaffle/Cryptography/HashAlgorithmType.cs
  • Src/CrispyWaffle/Cryptography/Security.cs

@AppVeyorBot

Copy link
Copy Markdown

@penify-dev

penify-dev Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Failed to generate code suggestions for PR

@github-advanced-security

Copy link
Copy Markdown

You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool.

What Enabling Code Scanning Means:

  • The 'Security' tab will display more code scanning analysis results (e.g., for the default branch).
  • Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results.
  • You will be able to see the analysis results for the pull request's branch on this overview once the scans have completed and the checks have passed.

For more information about GitHub Code Scanning, check out the documentation.

@guibranco
guibranco merged commit b3f8133 into guibranco:main Apr 21, 2026
36 of 39 checks passed
@guibranco

Copy link
Copy Markdown
Owner

Hi @christopher-conley πŸ‘‹,

Thank you so much for your pull request! πŸ™Œ

I appreciate the time and effort you put into this contribution.
I'll review it shortly, and if everything looks good, I'll approve it as soon as possible.

Thanks again for your valuable contribution! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚦 awaiting triage Items that are awaiting triage or categorization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants