Use PrepareDigest and RSA-PSS to sign a SHA-256 hash without hashing it again.
dotnet add package RecursiveAI.OneCrypto.Keys --prereleaseRecursiveAI.OneCrypto provides RecursiveAI.OneCrypto.Policy and is installed automatically with RecursiveAI.OneCrypto.Keys.
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System.Security.Cryptography; using RecursiveAI.OneCrypto.Keys; using RecursiveAI.OneCrypto.Policy; using var rsa = RSA.Create(2048); using var key = PrivateKey.FromRsa(rsa); var spec = SignatureSpec.RsaPss( HashAlgorithmName.SHA256); byte[] digest = SHA256.HashData( "Approve draft"u8); var policy = CryptoPolicy.Standard; var now = DateTimeOffset.UtcNow; var prepared = PreparedSignature .PrepareDigest(key.PublicKey, spec, digest, policy, now); var request = PreparedSignature.Decode( prepared.Encode()); byte[] raw = key.SignDigest( request.ToSign.Span, request.Spec); byte[] signature = prepared.Complete(raw); Console.WriteLine( $"Verified: {signature.Length} bytes");
The example hashes Approve draft, encodes the signing request, and decodes it before signing. It simulates the handoff in one process with a fresh test key; nothing is sent over the network. Complete checks the raw signature against the public key and digest in the original request. If verification fails, it throws InvalidSignatureException.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
The local 2048-bit RSA example prints:
Verified: 256 bytesVariations
Keep signing and completion in the same process. Replace the code after PrepareDigest with:
byte[] signature = prepared.SignWith(key); Console.WriteLine( $"Verified: {signature.Length} bytes");
Related recipes
The short answer
Use PrepareDigest with the public key, the already computed digest, and a SignatureSpec that names its hash algorithm. Give the signer the prepared ToSign bytes and spec, call SignDigest, and verify the returned bytes with Complete. If you already have the digest from an upstream step, use it in place of the example’s SHA256.HashData call. Do not pass the digest to Sign, which takes message bytes rather than an already computed digest.
Why is a hash function used in digital signatures?
For the RSA-PSS flow above, SHA-256 reduces the input to a 32-byte digest. The process that reads the document computes that value; the process holding the key can sign without needing the original file. The digest must match the algorithm in the spec: the length alone does not tell the signer which hash function produced it. For example, do not label an arbitrary 32-byte value as a SHA-256 digest. The signature scheme and its hash must agree across preparation, signing, and verification.
How do you sign and verify an RSA digest in C#?
Keep the original prepared state with the process that requested the signature. The key holder can use the encoded request to recover the spec and digest, then return only the raw signature. The requesting process calls Complete on the state it kept, not on replacement state supplied with the reply. Verification checks that the signature belongs to the expected public key and digest; it does not establish who owns that key or who approved the digest. Get the public key from a source your application already trusts, and make the decision to authorize a signing request separately.
PrepareDigest is for the RSA and ECDSA forms that sign digests. The signature specs list pure Ed25519, Ed448, and ML-DSA as message-based forms. For those forms use Prepare with the message; replacing the message with its SHA-256 hash would sign a different input.
For message-based signing or Prepare rather than PrepareDigest, the key guide warns: “The state holds the data for message schemes and for Prepare: treat it like the document.”